#1
|
|||
|
|||
Add bookmark with vba in the header
Greetings friends of the forum, this time I raise the present problem. when opening the document I want to place a bookmark in the header with vba but from the second page and then from three spaces. HTML Code:
Sub addBM_Header() ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader ActiveDocument.ActivePane.View.NextHeaderFooter ActiveDocument.Sections(1).headers (wdHeaderFooterPrimary) Selection.MoveRight Unit:=wdCharacter, Count:=4 ActiveDocument.Bookmarks.Add Name:="NombreEncab", Range:=Selection.Range ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument End Sub this is HTML Code:
ActiveDocument.Sections(1).headers(wdHeaderFooterPrimary).Bookmarks.Add Range:=Selection.Range, Name:="NombreEncab" How can I solve this? Thanks in advance for the support |
#2
|
||||
|
||||
The following will put the bookmark in the main header of the first section after the third character if there is one. (If you want it to start from page 2, then you will either need a section break and make it Sections(2) or have a different first page header.
Code:
Sub addBM_Header() Dim oRng As Range 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" End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
Greetings, thanks for the solution you give me, and it works perfect, but I was inconvenienced that when I assign the text to the bookmark, it is added more and more, therefore I have seen the need for the document to close delete the bookmark so that when the document is opened again it is created and the text is assigned that works but is added. In other words, the Bookmark is deleted but the text is not deleted. Then the other way would be to search for the text and replace with nothing ""
But the macro does not work. Well, I think this is another issue. Practically with that I would have the document finished, I would prefer to send it so you can take a look and give me the definitive help. As they say, I'm having the wonderful thing. HTML Code:
Sub eliminarMiBM() ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader ' ActiveDocument.Sections(1).headers (wdHeaderFooterPrimary) If ActiveDocument.Bookmarks.Exists("NombreEncab") = True Then ActiveDocument.Bookmarks("NombreEncab").Delete End If ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 'If ActiveDocument.Bookmarks.Exists("NombreEncab") = True Then ' 'ActiveDocument.Bookmarks("NombreEncab").Select ' ActiveDocument.Bookmarks("NombreEncab").Delete ' End If End Sub HTML Code:
Sub DelTexHeader() ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader 'ActiveDocument.Sections(1).headers(wdHeaderFooterPrimary).Range 'ActiveWindow.ActivePane.View.NextHeaderFooter ActiveDocument.Find.ClearFormatting ActiveDocument.Find.Replacement.ClearFormatting With ActiveDocument.Find .Text = "Name Here" .Replacement.Text = "" End With 'Selection.Find.Execute ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument End Sub |
#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 |
#5
|
|||
|
|||
Awesome!! I have tried different ways to check cases where it stops working, but all tests work well. Excellent!! Please, a final question, how do I build a general variable at the beginning so that every subroutine that has the variable acquires the assigned value?
Will it be this way? HTML Code:
Option Explicit 'Dim oRng As String 'Set oRng.Text = "Student name" |
#6
|
||||
|
||||
If you are working with a constant string value you can do something like
Code:
Option Explicit Public Const strText as String = "Student Name" Sub ShowMe() MsgBox "The value is: " & strText End Sub Sub ShowMeToo() MsgBox "The value is still: " & strText End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#7
|
|||
|
|||
Thank you, what you sent me is just what I need, but it seems that the code does not fit my version. Because it turns red. The version of my Word is 2013.
Could you suggest any appropriate change for the code to work? this turns red color HTML Code:
Public Const strText as String = "Student Name" "Compilation error Constants, fixed length strings, matrices and instructions are not allowed. Declare as public members of object modules" |
#8
|
||||
|
||||
I guess you are trying to use that code in the ThisDocument module or in a Userform. Put the code in an ordinary module.
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#9
|
||||
|
||||
And constant declarations have to be at the top of a module before any subs appear.
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#10
|
|||
|
|||
Very true, Thank you for all your collaboration. Now I practically have my word template quite resolved. I hope it works for me with the students as I hope, although in practice things arise
|
#11
|
|||
|
|||
content
Thanks. very helpful
Last edited by digitalelise; 10-22-2019 at 10:59 PM. Reason: Accidental post |
Tags |
vba |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
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 |