如何使用Powershell创建文字书签

时间:2019-02-19 11:22:20

标签: powershell bookmarks

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

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

1 个答案:

答案 0 :(得分:0)

我不确定您尝试做的事情,但这可能对您尝试做的事情有用:

$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