PDA

View Full Version : Adding field in word in bookmark in shape with Excel vba Late Binding



Hdr
12-18-2012, 04:05 PM
Dear reader,

I want to add a field to a bookmark in a shape range in in a header in WORD.
Can someone help me? I have tried various types and I can access the bookmark with the the code I built, but I can't get a field added.
Mark that i have to do this with Late Binding.

Code I have until now:
--------------------------------------------------------
Const wdPaneNone = 0
Const wdOutlineView = 2
Const wdPrintView = 3
Const wdNormalView = 1
Const wdSeekCurrentPageHeader = 9
Const wdSeekCurrentPageFooter = 10
Const wdFieldEmpty = -1
Const wdSeekMainDocument = 0
Const wdAlignParagraphCenter = 1
Const wdAlignParagraphRight = 2


Set BMRange = WdDocument.Sections(1).Headers(1).Shapes(1).TextFrame.Textrange.bookmarks(B ookmarkToUpdate).Range

'BMRange.Text = TextToUse
BMRange.Select
MsgBox BMRange.Fields.Count
BMRange.Fields.Update
'Selection.Fields.Add Selection, wdFieldEmpty, TextToUse, False
MsgBox WdDocument.Sections(1).Headers(1).Shapes(1).TextFrame.Textrange.bookmarks(B ookmarkToUpdate).Range.Fields(1)
MsgBox WdDocument.Sections(1).Headers(1).Shapes(1).TextFrame.Textrange.bookmarks(B ookmarkToUpdate).Range 'BMRange.Fields(1).Text
MsgBox Selection
BMRange.Fields(1).Text = TextToUse
WdDocument.Selection.Fields.Add _
Range:=WdDocument.Selection, Type:=wdFieldEmpty, Text:= _
TextToUse, PreserveFormatting:=False
'WdDocument.Variables(BookmarkToUpdate).Fields.Add =


'MsgBox "Code = " & WdDocument.Shapes(1).TextFrame.Textrange.Fields(1).code & vbCr
Set BMRange = WdDocument.Sections(1).Headers(1)
WdDocument.Sections(1).Headers(2).Shapes(1).TextFrame.Textrange.Fields.Upda te
'BMRange.Fields.Add Range:=BMRange, Type:=wdFieldEmpty, Text:= _
TextToUse, PreserveFormatting:=False

'WdDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes(1).TextFrame. TextRange.Bookmarks(BookmarkToUpdate).Range.Fields.Add Range:=WdDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes(1).Text Frame.TextRange.Bookmarks(BookmarkToUpdate).Range, Type:=wdFieldEmpty, Text:= _
TextToUse, PreserveFormatting:=False
--------------------------------------------------------

Thanks in advance for all the help!

Hdr