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

how to create bookmark in word using powershell

我必须在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,???)"

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

我不是 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