![]() |
#4
|
||||
|
||||
![]()
Clearly you are not adding text to the bookmark, but next to it.
There is no need to open the header view to write to it if you use ranges, nor is there any need to delete the bookmark. The following will create the bookmark if not present already and then add the text to it and reset the bookmark to the added text. See also the FillBM function on my web site for filling existing bookmarks. Code:
Sub addBM_Header() Dim oRng As Range Dim oBM As Bookmark 'Check if the bookmark exists For Each oBM In ActiveDocument.Bookmarks If oBM.Name = "NombreEncab" Then GoTo FillBM Exit For End If Next oBM 'Bookmark not present so add-it Set oRng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range If Len(oRng) > 3 Then oRng.Start = oRng.Start + 3 oRng.Collapse 1 End If ActiveDocument.Bookmarks.Add Range:=oRng, Name:="NombreEncab" FillBM: 'Write to the bookmark Set oRng = ActiveDocument.Bookmarks("NombreEncab").Range oRng.Text = "bookmark text" 'Reset the bookmark ActiveDocument.Bookmarks.Add Range:=oRng, Name:="NombreEncab" End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
Tags |
vba |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Different Odd/Even Header with no header on first page of new chapters | bbecker24 | Word | 4 | 05-09-2018 08:17 PM |
Header Info: Connecting seclected items for 1st page Header into Following Pages | SMHa | Word | 1 | 08-24-2016 10:00 AM |
No Header | bullsfan586 | Word | 1 | 08-29-2015 07:55 PM |
How do I get this header? | veed | Word | 7 | 04-10-2013 12:25 AM |
How to get this header? | msvis | Word | 1 | 12-04-2011 07:45 PM |