Share via

Word VBA and Strings/Bookmarks

Anonymous
2017-08-03T17:56:45+00:00

Hi, I'm struggling with strings and bookmarks and could use advice.  I have a macro that stores the bookmarked text in a string, then replaces the cover page with a new cover page that has the same bookmarks.  The text is then inserted into the new cover page.  My problem is when I go to the next document, the saved text from the first document is inserted into the new one.  I was wondering if there is a way to clear the memory after the text is entered into the new cover page.  Here's a sample of the code that saves the text (there are more bookmarks than shown below):

If ActiveDocument.Bookmarks.Exists("bknbr") Then

        strWord1 = ActiveDocument.Bookmarks("bknbr").Range

    End If

    If ActiveDocument.Bookmarks.Exists("bkuse") Then

        strWord2 = ActiveDocument.Bookmarks("bkuse").Range

    End If

    If ActiveDocument.Bookmarks.Exists("bkRevision") Then

        strWord3 = ActiveDocument.Bookmarks("bkRevision").Range

    End If

Then it goes to this subroutine.

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

And back to actually fill in the bookmarks on the new cover page.

FillBookmark "bknbr", strWord1

FillBookmark "bknbr1", strWord1

FillBookmark "bkRevision", strWord3

Thanks in advance for any help!

Cindy

***Post moved by moderator to appropriate forum category***

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Jay Freedman 207.6K Reputation points Volunteer Moderator
    2017-08-14T21:25:44+00:00

    Yes, that's the kind of problem Peter mentioned. The cure is to move the declaration of the string variables inside the procedure that contains the assignments of the strings. You'll have to replace the keyword Private in the declaration with the keyword Dim (because a Private declaration is not allowed inside a procedure).

    As long as all the other procedures (such as FillBookmark) get their string values as arguments that are passed from the first procedure, all will be well. If any procedures are supposed to refer to the variables declared outside any procedure, those procedures must be changes to get the variables as arguments. This article may help to understand better: http://wordmvp.com/FAQs/MacrosVBA/ProcArguments.htm

    1 person found this answer helpful.
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-08-14T19:22:20+00:00

    This is what's at the top of the module: (it wouldn't let me change below to code format)

    Private strWord1 As String, strWord2 As String, strWord3 As String, strWord4 As String, _

        strWord5 As String, strWord6 As String, strWord7 As String, strWord8 As String, _

        strWord9 As String, strWord10 As String, strWord11 As String, strWord12 As String, _

        strWord13 As String, strWord14 As String

    Thank you for your insight. 

    I'll keep trying to figure this out!

    Cindy

    0 comments No comments
  2. Anonymous
    2017-08-08T08:44:03+00:00

    > it should reassign the string, shouldn't it?

    One scenario where this would go wrong is where the second (newly opened) document, does not have a bookmark called "bknbr" (say) and you declared strWord1 globally, e.g. this might work

    'top of the module

    Sub mysub1()

    Dim strWord1 As String

    If ActiveDocument.Bookmarks.Exists("bknbr") Then

      strWord1 = ActiveDocument.Bookmarks("bknbr").Range

    End If

    End Sub

    but this will not

    'top of the module

    Dim strWord1 As String

    Sub mysub1()

    If ActiveDocument.Bookmarks.Exists("bknbr") Then

      strWord1 = ActiveDocument.Bookmarks("bknbr").Range

    End If

    End Sub

    The problem in the second case is that when the bookmark does not exist, the existing value is retained. That may not be your scenario, but that's why, as Jay Freedman says, it's difficult to see what's going on without seeing all the relevant code.

    0 comments No comments
  3. Anonymous
    2017-08-07T20:18:04+00:00

    Thanks for your assistance!  When I go to the next document, that just means I have closed the previous one as you normally would in word, either saving or not, then I open another existing document from word and start the macro over again.  I'm pretty sure I have written a jumbled mess of the code but since I am reinitiating the macro by clicking a button each time I open another document , it should reassign the string, shouldn't it? 

    thanks,

    Cindy

    0 comments No comments
  4. Jay Freedman 207.6K Reputation points Volunteer Moderator
    2017-08-04T16:23:02+00:00

    When you "go to the next document," however you do that, your macro logic must again run the part of the code that assigns the bookmark contents to the strings. For example, if you're running a loop that opens each document in a folder, then the string assignments must be inside the loop.

    Without seeing the complete logic path of the macro, I can't be more specific.

    0 comments No comments