Automatically move MS Word bookmark after an inser

2019-08-30 12:12发布

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条回答
ら.Afraid
2楼-- · 2019-08-30 12:41

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
查看更多
登录 后发表回答