Solved

Recreate bookmark

Posted on 2013-05-31
6
288 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 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
On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
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…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

624 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