Automatically move MS Word bookmark after an insertion at this point

会有一股神秘感。 提交于 2019-12-12 04:40:00

问题


H ey folks,

I've assembled the following code, which copies the first table in my Word document and inserts it at a bookmark position and also adds a formated heading above it via a second bookmark.

To fully automate my Excel application however, I need an advanced functionality of my code. After an insertion was done, the bookmarks have to be relocated to a position directly above the newly inserted table / heading.

Is it possible to relocate these bookmarks programmatically? Any help is much appreciated.

Best regards, daZza

Code:

Sub Main()

Dim doc As Word.document
Set doc = GetObject("xxxx.docx")

doc.Tables(1).Range.Copy
doc.bookmarks("AH_Tab").Range.Paste

doc.bookmarks("AH_Header").Range.Text = "Test"
doc.bookmarks("AH_Header").Range.Style = wdStyleHeading1

End Sub

回答1:


Add the following code before End Sub

Dim tmpRng As Range
Set tmpRng = doc.Bookmarks("AH_Header").Range
doc.Bookmarks.Add "AH_Header", ActiveDocument.Range(tmpRng.Start - 1, tmpRng.Start - 1)

Additional information:

  • do the same for second bookmark
  • by changing -1 values you can expand & move the range where exactly the new bookmark should be placed


来源:https://stackoverflow.com/questions/22476059/automatically-move-ms-word-bookmark-after-an-insertion-at-this-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!