Word VBA - Loop and Create Bookmark

MikeL

Active Member
Joined
Mar 17, 2002
Messages
488
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have a question related to ultimately getting Word bookmark info into XL.

To begin, I need to loop thru a Word Document and bookmark every instance of the string "
ABC" and then Selection.Extend 6 characters to the right.

Any way to accomplish this? (I did create a Tools-Reference to Excel 12.0)

Thanks in advance,
MikeL
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Is there a particular reason you want to BOOKMARK it?

Or are you importing the bookmark plus 6?

Or do you just want to import the search string + 6?

Are Word tables involved?
 
Upvote 0
Tinbender,

I don't have a need to bookmark, actually creating multiple bookmarks in VBA seems to be an issue for me.

As for the 2nd question, I honestly don't know. I exported a RTF from a 3rd party application (only option) then saved as .DOCM

HTH, Mike
 
Upvote 0
What is your GOAL? What are you trying to accomplish? Are you pulling this into Excel?
 
Upvote 0
Yes, I am ultimately pulling this into XL. VBA in Word to find the string "ABC" plus 6 characters to the right. Should be a good 100+ occurrences. Then I need to export all this into a Blank Worksheet and list in Column A.

Thanks
 
Upvote 0
Will you be searching for text from a column?

Or is this something random that you will enter on the fly?
 
Upvote 0
Not sure of the exact answer to that. It seems like I will be searching for test from a column.

Here is the current VBA. I guess it is obvious that it only finds the first instance of "ABC" and create a bookmark for just that instance. I have the code Extending to the "-" symbol but I really need the code to select 6 characters to the right of the "C" in "ABC" including blanks

Selection.Find.ClearFormatting
With Selection.Find
.Text = "ABC"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "-"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
End With
Selection.Find.Execute

'Create Bookmark
Selection.Bookmarks.Add Name:="Test", Range:=Selection.Range
 
Upvote 0
Get search word from Word

Try this.
Code:
Sub SearchFile4Keyword()
Dim WB As Workbook
Dim WS As Worksheet
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim wrdRng As Word.Range
Dim Counter As Long

'Set the Word object reference in Tools

Set WB = Workbooks.Add
Set WS = WB.Sheet1
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = False
    Set wrdDoc = wrdApp.Documents.Open("C:\Documents and Settings\Owner\My Documents\VBA\MrExcel\On the Insert tab.docm")
    With wrdDoc

        Set wrdRng = .Range

        SearchString = "insert"

        Do
            With wrdRng.Find
                .ClearFormatting
                .Text = SearchString
                .Replacement.Text = ""
                .Format = False
                .MatchWildcards = False
                .Wrap = wdFindStop
                .Execute
            End With

            If wrdRng.Find.Found Then
                Set wrdRng2 = wrdRng.Duplicate
                wrdRng2.MoveEnd wdCharacter, 6
                Counter = Counter + 1
                WS.Range("A" & Counter).Value = wrdRng2.Text
                Debug.Print wrdRng2.Text, wrdRng2.Start
            End If
        Loop Until Not wrdRng.Find.Found
End With
        wrdDoc.Close savechanges:=False
        wrdApp.Quit
    End Sub
 
Upvote 0
Thanks for the code. I am trying to run now. A 438 error so I made the following change.

Set WS = WB.Sheet1

to

Set WS = Worksheets("Sheet1")


I will try to run now.
 
Upvote 0
Tinberder,

Code worked like a charm.
Thanks for your great help on this.

Mike
 
Upvote 0

Forum statistics

Threads
1,203,600
Messages
6,056,204
Members
444,850
Latest member
dancasta7

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top