如何使用powershell在word中创建书签

问题描述 投票:0回答:1

我必须在word文档中创建书签,但不了解如何设置定义范围

$Word = [Runtime.Interopservices.Marshal]::GetActiveObject('Word.Application')
$Selection = $Word.Selection
$Selection.TypeParagraph()
$Selection.Style =$Title
$Selection.TypeText($someText2)
$Selection.TypeParagraph()
$doc.Bookmarks.Add($bN,???)"

我想在文档中添加一些文本,然后将此文本作为书签。最好的方法是什么?谢谢/

powershell bookmarks
1个回答
0
投票

我不是100%确定你想要做什么,但这可能适用于你试图做的事情:

$someText2 = "Some text to add"

#Open Active Word Document    
$Word = [Runtime.Interopservices.Marshal]::GetActiveObject('Word.Application')
$Selection = $Word.Selection
$Selection.TypeParagraph()
$Selection.Style = "Title"
$selection.TypeText("This is the Title")
$Selection.TypeParagraph()

#Add the bookmark to the Selection
$Selection.Bookmarks.Add("Bookmark1")
#Load the bookmark from the Selection
$bookmark = $Selection.Bookmarks._NewEnum | Where-Object {$_.Name -eq "Bookmark1"}
#add the text to the selection
$Selection.TypeText($someText2)
#set the end of the bookmark to the length of the added text
$bookmark.End = $bookmark.End + $someText2.Length

UPD

$Word = New-Object -ComObject Word.Application
$Word.Visible = $True
$Document = $Word.Documents.Add()
$Selection = $Word.Selection
$Selection.TypeText($someText)
$Selection.TypeParagraph()

$Word = [Runtime.Interopservices.Marshal]::GetActiveObject('Word.Application')
$Selection = $Word.Selection
$Selection.Bookmarks.Add($bN)
$bookmark = $Selection.Bookmarks._NewEnum | Where-Object {$_.Name -eq $bN}
$Selection.TypeText($someText2)
$bookmark.End = $bookmark.End + $someText2.Length
© www.soinside.com 2019 - 2024. All rights reserved.