0
votes

I am trying to find a way to lookup and replace contents within an MS Word Doc based on certain content within the same document. I have system generated Word Documents that are one page each in length, but the number of pages can vary from one to 100 (or more). Each document is formatted exactly the same. One phrase with each page of the document (such as "Type of Charge" may or may not vary from one page to the next. I need to be able to insert the actual amount of the charge on each page based on the type of charge reflected on that given page.

I have been trying to use the bookmark approach and I am fairly new to using VBA in MS Word. I was taking the approach of setting bookmark ranges that would be used to search for the phrase, and then setting a bookmark that would indicate where to insert the value. Here is what I have so far:

Sub bmAmtDue() ' ' bmAmtDue ' ' Dim rng As Range Dim iBookmarkSuffix As Integer Dim strBookMarkPrefix

 strBookMarkPrefix = "BM"

 Set rng = ActiveDocument.Range
 With rng.Find
    .Text = "Please see fee chart, with additional requirements, on reverse side"
    Do While .Execute
        rng.Text = "" 'clear the "XXX" (optional)
        iBookmarkSuffix = iBookmarkSuffix + 1
        ActiveDocument.Bookmarks.Add strBookMarkPrefix & iBookmarkSuffix, rng
    Loop
End With

End Sub

Sub bmStartPermitType() ' ' bmStartPermitType ' ' Dim rng2 As Range Dim iBookmarkSuffix As Integer Dim strBookMarkPrefix

 strBookMarkPrefix = "BMStartPermitType"

 Set rng = ActiveDocument.Range
 With rng.Find
    .Text = "Type:"
    Do While .Execute
        iBookmarkSuffix = iBookmarkSuffix + 1
        ActiveDocument.Bookmarks.Add strBookMarkPrefix & iBookmarkSuffix, rng
    Loop
End With

End Sub

Sub bmEndPermitType() ' ' bmEndPermitType ' ' Dim rng2 As Range Dim iBookmarkSuffix As Integer Dim strBookMarkPrefix

 strBookMarkPrefix = "BMEndPermitType"

 Set rng = ActiveDocument.Range
 With rng.Find
    .Text = "Amount due:"
    Do While .Execute
        iBookmarkSuffix = iBookmarkSuffix + 1
        ActiveDocument.Bookmarks.Add strBookMarkPrefix & iBookmarkSuffix, rng
    Loop
End With

End Sub

I would appreciate any help and I don't mind taking a different approach if there is an easy one. I am kind of stuck with using the MS Word Docs. We do this manually every month and it is sometimes 100s of documents.

Thank You

1

1 Answers

0
votes

Bookmarks are OK, but might be "too flexible" - They can even start in the middle of a table cell and end in a the middle of another paragraph. I suggest you to try doing it with Content Controls - their appearance might also be more suitable for your scenario. Check this link.

If you can write a simple .NET application, there is mail merge toolkit that will make your task much more easy. It will allow you to create word document that will act as a template (it also uses Content Controls for tagging) which you will be able to populate with data from your .NET application. And it demands only couple of lines of code to write.