【问题标题】:Powershell - Add a new document to exisitng word file with page number of 2Powershell - 将新文档添加到页码为 2 的现有 word 文件
【发布时间】:2014-11-05 15:47:02
【问题描述】:

我正在尝试打开一个现有的 word 文件并向其中添加一个新页面。这个新页面会有一些内容,我总是希望这个页面位于第 2 位。因此,第一页将始终相同,并且添加的每个新内容都应该转到第二页。到目前为止,我的代码如下:

$day = get-date -Format "dd-MMM-yyyy"
$filedate = Get-Date -Format "dd/MMM/yy - H tt"
$word = New-Object -ComObject word.application
$word.visible = $false
$doc = $word.documents.open("$home\desktop\Network Report.docx")
$selection = $word.selection
$Selection.InsertNewPage() #So, this page should be added at number 2
$selection.Font.Size = 14 
$selection.Font.color ="wdColorBlack"
$selection.TypeText("Network Report for $filedate")
$selection.TypeParagraph()
$selectimage =  $selection.InlineShapes.AddPicture("$home\desktop\screenshot.jpeg")
$selection.TypeParagraph()
$Section = $Doc.Sections.Item(1);
$Header = $Section.Headers.Item(1);
$Footer = $Section.Footers.Item(1);
$Header.Range.Text = "$campus Network Report - $day "
$doc.save()

经过不同的表格后,我尝试了不同的以下内容:

$gotoPage1 = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage 
$gotoNext1 = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute
$gotoCount1 = null
$gotoName1 = 1
$Selection.GoTo([ref]$gotoPage1, [ref]$gotoNext1, [ref]$gotoCount1, [ref]$gotoName1)

但我没有运气。如何添加新页面作为第二页?

【问题讨论】:

    标签: powershell ms-word


    【解决方案1】:

    这是一个应该创建第二页并将光标放在那里的示例。无论文档中有多少页,这都应该有效。

    $word = New-Object -ComObject word.application
    $word.Visible = $true
    
    $doc = $word.documents.open("$home\desktop\Network Report.docx")
    
    [Void]$word.Selection.GoTo([Microsoft.Office.Interop.Word.WdGoToItem]::wdGoToPage,
                         [Microsoft.Office.Interop.Word.WdGoToDirection]::wdGoToAbsolute,
                         1 #page number
                         )
    $doc.Bookmarks.Item("\page").range.select()
    $word.Selection.Collapse([Microsoft.Office.Interop.Word.WdCollapseDirection]::wdCollapseEnd)
    $word.Selection.InsertNewPage()
    [Void]$word.Selection.GoTo([Microsoft.Office.Interop.Word.WdGoToItem]::wdGoToPage,
                     [Microsoft.Office.Interop.Word.WdGoToDirection]::wdGoToAbsolute,
                     2 #page number
                     )
    

    【讨论】:

      猜你喜欢
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-21
      相关资源
      最近更新 更多