Solved

Recreate bookmark

Posted on 2013-05-31
6
282 Views
Last Modified: 2013-06-15
I have a form that user complete and within that form I have a book mark that after the document is opened VBA nicely fills in the computer name so I can determine which/who completed the form.

All is working fine but if I have to edit the form when its opened the macro runs as it should and places my computer name correctly in the bookmark.

The issue is that when it fills in the bookmark with the computer name the books mark is lost which causes other issues for me not  related to this question.

Is there a way to either maintain the bookmark name or recreate with a macro?

Here is my document_open code: (document is protected, no password)

Sub Document_Open()
    
    On Error GoTo ErrHandler
    ActiveDocument.Unprotect
    ActiveDocument.Bookmarks("computer").Range = Environ$("computername")
    ActiveDocument.Protect wdAllowOnlyFormFields
    

    With ActiveWindow
        .DisplayHorizontalScrollBar = True
        .DisplayVerticalScrollBar = True
        .DisplayLeftScrollBar = False
        .StyleAreaWidth = InchesToPoints(0)
        .DisplayVerticalRuler = True
        .DisplayRightRuler = False
        .DisplayScreenTips = False
        With .View
            .ShowBookmarks = True
            .FieldShading = wdFieldShadingAlways
        End With
    End With

'MsgBox "Please complete form on computer and email to:" & vbCr & vbCr & _
        "bla@bla.com" & vbCr & vbCr, vbOKOnly, "Order Submission"
    Application.DisplayStatusBar = True
    Application.ShowWindowsInTaskbar = True
    Application.ShowStartupDialog = False
    ActiveWindow.EnvelopeVisible = True
    
    
ErrHandler:

End Sub

Open in new window

0
Comment
Question by:thandel
  • 3
  • 3
6 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 39211648
These are the steps

Capture the bookmark range
Fill the range with the text
Re-add the bookmark to the range

Here is a procedure that you might want to use if this is a frequent requirement
Sub FillBookmark(bmkBookMark As Bookmark, strText As String)
    Dim strBookmarkName As String
    Dim rng As Range
    
    strBookmarkName = bmkBookMark.Name 
    Set rng = bmkBookMark.Range
    rng.Text = strText
    rng.Document.Bookmarks.Add strBookmarkName, rng
End Sub

Open in new window

0
 

Author Comment

by:thandel
ID: 39227923
I'm sorry I had not responded sooner... can you elaborate on this?  what is strText used for?  I would like to just populate/recreate the  bookmark "Computer" with nothing.   Perhaps prior to save.  How would I use your solution?  Can you provide an example?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39228024
You use it in the standard way:
FillBookMark doc.BookMarks("Computer"), ""

Open in new window

where doc is an object variable for the target document. If you don't have a separate variable, then probably ActiveDocument will do.
FillBookMark ActiveDocument.BookMarks("Computer"), ""

Open in new window

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:thandel
ID: 39231794
So should FillBookMark be a function or sub routine?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39231948
As you will see, it's a Sub, as opposed to a Function which must be used if a return value is needed.
Copy it into your code module. To use it in your code, you would need to change the line:
 ActiveDocument.Bookmarks("computer").Range = Environ$("computername")

Open in new window

to
FillBookmark  ActiveDocument.Bookmarks("computer"), Environ$("computername")

Open in new window

0
 

Author Comment

by:thandel
ID: 39250867
Ah now I see, thanks.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now