Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-17-2019, 05:40 PM
Visor Visor is offline Add bookmark with vba in the header Windows 10 Add bookmark with vba in the header Office 2013
Advanced Beginner
Add bookmark with vba in the header
 
Join Date: Aug 2019
Posts: 38
Visor is on a distinguished road
Default 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
I have the macro that I attached but when executing it at the end it shows me an error because it doesn't find any more pages

this is
HTML Code:
ActiveDocument.Sections(1).headers(wdHeaderFooterPrimary).Bookmarks.Add Range:=Selection.Range, Name:="NombreEncab"
something like this does not work in bookmark but if I only put text in the header

How can I solve this?

Thanks in advance for the support
Reply With Quote
  #2  
Old 08-17-2019, 08:08 PM
gmayor's Avatar
gmayor gmayor is offline Add bookmark with vba in the header Windows 10 Add bookmark with vba in the header Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 08-18-2019, 07:09 PM
Visor Visor is offline Add bookmark with vba in the header Windows 10 Add bookmark with vba in the header Office 2013
Advanced Beginner
Add bookmark with vba in the header
 
Join Date: Aug 2019
Posts: 38
Visor is on a distinguished road
Default

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
If you approve that I send you the complete document, I would like you to tell me.
Reply With Quote
  #4  
Old 08-18-2019, 08:10 PM
gmayor's Avatar
gmayor gmayor is offline Add bookmark with vba in the header Windows 10 Add bookmark with vba in the header Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #5  
Old 08-21-2019, 06:22 PM
Visor Visor is offline Add bookmark with vba in the header Windows 10 Add bookmark with vba in the header Office 2013
Advanced Beginner
Add bookmark with vba in the header
 
Join Date: Aug 2019
Posts: 38
Visor is on a distinguished road
Default

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"
Reply With Quote
  #6  
Old 08-21-2019, 10:29 PM
Guessed's Avatar
Guessed Guessed is offline Add bookmark with vba in the header Windows 10 Add bookmark with vba in the header Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #7  
Old 08-22-2019, 01:37 PM
Visor Visor is offline Add bookmark with vba in the header Windows 10 Add bookmark with vba in the header Office 2013
Advanced Beginner
Add bookmark with vba in the header
 
Join Date: Aug 2019
Posts: 38
Visor is on a distinguished road
Default

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"
msg
"Compilation error
Constants, fixed length strings, matrices and instructions are not allowed. Declare as public members of object modules"
Reply With Quote
  #8  
Old 08-22-2019, 08:00 PM
gmayor's Avatar
gmayor gmayor is offline Add bookmark with vba in the header Windows 10 Add bookmark with vba in the header Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #9  
Old 08-23-2019, 05:32 AM
Guessed's Avatar
Guessed Guessed is offline Add bookmark with vba in the header Windows 10 Add bookmark with vba in the header Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

And constant declarations have to be at the top of a module before any subs appear.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #10  
Old 08-24-2019, 07:21 PM
Visor Visor is offline Add bookmark with vba in the header Windows 10 Add bookmark with vba in the header Office 2013
Advanced Beginner
Add bookmark with vba in the header
 
Join Date: Aug 2019
Posts: 38
Visor is on a distinguished road
Default

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
Reply With Quote
  #11  
Old 10-22-2019, 10:57 PM
digitalelise digitalelise is offline Add bookmark with vba in the header Mac OS X Add bookmark with vba in the header Office 2016
Novice
 
Join Date: Oct 2019
Posts: 2
digitalelise is on a distinguished road
Question content

Thanks. very helpful

Last edited by digitalelise; 10-22-2019 at 10:59 PM. Reason: Accidental post
Reply With Quote
Reply

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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:39 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft