0
Dim AppWD As Object
Dim objDoc As Object
Dim objDocProdTP As Object
...
Set AppWD = CreateObject("Word.Application") 'Word als Object starten
AppWD.Visible = True
Set objDocProdTP = AppWD.documents.Open(workPath & "\vorlagen\LFPostTemplate.docx")
Set objDoc = AppWD.documents.Open(workPath & "\vorlagen\LFTemplate2.docx")
...
MsgBox (objDoc.Bookmarks.Count)
objDoc.Goto what:=wdGoToBookmark, Name:="lblSFirma"

MSGBox 返回 11 个书签(与 LFPostTemplate.docx 中的书签总数匹配)

但是我尝试的每个书签名称都会返回“此书签不存在”

我怀疑这个问题很简单......但谷歌似乎没有为我提供任何解决方案

4

2 回答 2

2

您通过后期绑定(从 Excel?)启动 Word,因此我假设可能未定义单词常量。尝试使用 -1 而不是wdGotoBookmark.

objDoc.Goto what:=-1, Name:="lblSFirma"

(或者您可以设置对Microsoft Word xx.xx Object Library的引用,然后常量可用)

于 2013-07-21T11:47:51.800 回答
0

我得到下面的代码在 Excel 2010 中工作。我切换窗口视图模式以使我的书签所在的页面出现在窗口顶部(我的书签所在的位置)。

Public Sub OpenWordDocAndGotoBookmark(strDocPathAndName As String, strBookmark As String)
    Dim AppWD As Object
    Dim objDoc As Object
    Dim objBookmark As Object

    Set AppWD = CreateObject("Word.Application") ' Start Word
    AppWD.Visible = True

    Set objDoc = AppWD.Documents.Open(strDocPathAndName)

    Set objBookmark = objDoc.Bookmarks(strBookmark)

    ' Move cursor to left of Bookmark
    objDoc.Range(objBookmark.Start, objBookmark.Start).Select

    ' Set window to make top of page with bookmark appear at top of window
    AppWD.ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitFullPage
    AppWD.ActiveWindow.ActivePane.View.Zoom.PageFit = wdPageFitBestFit
End Sub
于 2015-01-28T18:54:04.577 回答