PDA

View Full Version : How to insert a bookmark in a table



anhaase
01-10-2024, 05:26 AM
Hi there,

I'm trying to insert a previosly defined Bookmark in a specific Cell in the Table of my Header by using VBA.

here's what im working with:


ActiveDocument.Sections(2).Headers(wdHeaderFooterPrimary).Range.Tables(1).C ell(3, 4).Range.InsertCrossReference ReferenceType:="Textmarke", ReferenceKind:=wdContentText, ReferenceItem:="Date", InsertAsHyperlink:=True

Strangely this command adds the Reference at the Cell 1,3 and deletes the content of Cell 3,4.

First sreenshot shows the wanted result. The second shows what the macro is doing.

What am I doing wrong?

Vladimir
01-10-2024, 06:50 AM
Hi! I think the mistake is because of the merged cells in column 3. The cell that is supposed to be (1,3) is actually (3,4). Try replacing (1,3) with (3,4) in your code and the problem will go.

Aussiebear
01-10-2024, 07:10 AM
Welcome to VBAX anhaase. Not sure I've ever seen a table within a header before... Perhaps I have but never recognised it as such.

anhaase
01-10-2024, 07:50 AM
Thanks for your Answer. I also thought about that, but I tried every combination of column and row, but the macro keeps ignoring the Column. it always ends up filling the first column.

Vladimir
01-10-2024, 09:20 AM
Frankly, I've never seen tables in headers either. Anyway, this code:
selection.range.Tables(1).cell(3, 4).range = "Test"
works properly. If not, it would be nice to see your table. Maybe, it is merged/split differently from mine.

Aussiebear
01-10-2024, 04:24 PM
According to this it's more than possible to have a table within a header. https://answers.microsoft.com/en-us/msoffice/forum/all/why-is-there-a-table-in-my-header-and-how-do-i/805bf3a8-ad3c-486b-abd2-6f7434b78f89

Aussiebear
01-10-2024, 04:44 PM
Greg Maxey makes a good fist of inserting text with at or in a Bookmark. Particularly the point about vba destroying a Bookmark. Have a read here to see if this helps.https://gregmaxey.com/word_tip_pages/insert_text_at_or_in_bookmark.html.

anhaase
01-11-2024, 12:13 AM
Yes, Text works. I'm already using that command to enter Text in the footer:

ActiveDocument.Sections(SecNr).Footers(wdHeaderFooterPrimary).Range.Tables( 1).Cell(row, col).Range.InsertAfter Text:=".E"

Unfortunalty I don't know how to use the this structure to insert a bookmark.

I attached my word file in case you guys want to try.
https://easyupload.io/lsi02f