Solved

Recreate bookmark

Posted on 2013-05-31
6
283 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
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 Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

930 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

14 Experts available now in Live!

Get 1:1 Help Now