Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Recreate bookmark

Posted on 2013-05-31
6
Medium Priority
?
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 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
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

704 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