Bookmark question

L

LEU

I have the following macro that saves the data from my form to my document.
Not all of my documents will have a bookmark ‘bknbr4’. How do I write it to
say the following:
If ‘bknbr4’ exist in ActiveDocument then
Set oRng = oBMs("bknbr4").Range
oRng.Text = Text1.Text
oBMs.Add "bknbr4", oRng
Else
Do nothing
End If


Option Explicit
Private oBMs As Bookmarks
Private oRng As Word.Range
Dim oFF As FormFields


Private Sub cmdOK_Click()
Set oFF = ActiveDocument.FormFields
Set oRng = oBMs("bknbr").Range
oRng.Text = Text1.Text
oBMs.Add "bknbr", oRng
Set oRng = oBMs("bknbr2").Range
oRng.Text = Text1.Text
oBMs.Add "bknbr2", oRng
Set oRng = oBMs("bknbr3").Range
oRng.Text = Text1.Text
oBMs.Add "bknbr3", oRng
Set oRng = oBMs("bknbr4").Range
oRng.Text = Text1.Text
oBMs.Add "bknbr4", oRng
ActiveDocument.Saved = True
Me.Hide
End Sub
 
J

Jay Freedman

The function ActiveDocument.Bookmarks.Exists(bookmarkname) will return
true if the named bookmark exists in the document, or false if not.

Your code has some other problems. For example, you define the
FormFields collection oFF but you never use it, while you never define
the collection oBMs but then try to use it. This will result in an
error. Besides that, if the "bookmarks" you're trying to fill are
really the ones associated with formfields, you shouldn't be using the
range.Text to fill them -- you should be assigning the .Result of the
formfield:

ActiveDocument.FormFields("bknbr").Result = Text1.Text

Finally, it seems odd that you're assigning the same text to every
bookmark, but I'll assume that's a typo in the post and not in the
real code.

Assuming you really are filling bookmarks and not formfields, you can
collapse all the repetitive code into a subroutine and just pass the
name and value in parameters:

Private Sub cmdOK_Click()
FillBookmark "bknbr", Text1.Text
FillBookmark "bknbr2", Text2.Text
FillBookmark "bknbr3", Text3.Text
FillBookmark "bknbr4", Text4.Text
End Sub

Private Sub FillBookmark(bkName As String, bkVal As String)
Dim oRg As Range

With ActiveDocument.Bookmarks
If .Exists(bkName) Then
Set oRg = .Item(bkName).Range
oRg.Text = bkVal
.Add Name:=bkName, Range:=oRg
End If
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
L

LEU

Hi Jay,

This worked great. Thanks again for all your help.

I have a second question that I would like some help on. I am trying to
write a macro that will replace the first page of my old procedures with the
first page of my new Master Template which is on my C Drive. What I have so
far is below. It deletes the first page but it inserts all the pages from the
new template into the old document. How do I get just the first page?

Selection.Bookmarks("\Page").Range.Delete
Set objRange = ActiveDocument.Range
objRange.End = objRange.Start
objRange.InsertFile FileName:="C:\Master Procedure1.dot", _
Range:="", ConfirmConversions:=False, Link:=False, _
Attachment:=False

LEU
 
J

Jay Freedman

The VBA Help topic for the InsertFile method says this about the Range
parameter in the method syntax:
~~~~~~
Range Optional Variant. If the specified file is a Word document,
this parameter refers to a bookmark. If the file is another type (for
example, a Microsoft Excel worksheet), this parameter refers to a
named range or a cell range (for example, R1C1:R3C4).
~~~~~~

So what you need to do is edit the Master Procedure1.dot file to
insert a bookmark that covers exactly the amount of information you
want to bring into the other documents. (If the template contains a
page break between page 1 and page 2, you may have to experiment to
see whether the bookmark should or should not include the page break.)

Then alter your macro to put the name of the new bookmark into the
InsertFile call. For example, if you called the bookmark FirstPage,
then the macro should say

objRange.InsertFile FileName:="C:\Master Procedure1.dot", _
Range:="FirstPage", ConfirmConversions:=False, Link:=False, _
Attachment:=False

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

User Form Issues 2
Clear button for text box 1
Capturing a Content Control Range 0
User Form Problems 4
Content Control text formatting 3
Listbox not working 2
ListBox problem 2
Clear Button 5

Top