Link to home
Start Free TrialLog in
Avatar of thandel
thandel

asked on

Recreate bookmark

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

ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thandel
thandel

ASKER

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?
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

Avatar of thandel

ASKER

So should FillBookMark be a function or sub routine?
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

Avatar of thandel

ASKER

Ah now I see, thanks.