VB on a Mac

S

Steve Cronin

Folks
I'm trying to do Find/Replace within textBoxes on Mac using Word vX and

'do Visual Basic'.
The version of Word shouldn't really matter right? Since I'm using
'do Visual Basic'....
I am asking in the Mac forums as well but here is where the real
VBGurus seem to hang...

I've tried so many different constructs I cannot remember them all.
But no syntax I have come up with allows me to get to the text.
BTW: These are textBoxes created by doing Insert/Text Box off the menu.

No userForms...
The vb I'm using is below.
I'm convinced that the 'With' statement inside the Do Until is the key.

Here's my results:
With pTextBox.Text.Select --> invalid qualifier
With pTextBox.TextFrame.Select -->method or data member not found
With pTextBox.TextRange.Select -->method or data member not found
With pTextBox.TextFrame.TextRange.Select -->method or data member not
found

Can somebody boost me over this particular hump?
Thanks SO MUCH!
This is the last issue in implementation for me
BTW: Where is the best documentation source where I could have found
the answer myself?

Steve
__________________________
set vbScript to "
Dim pTextBox as Word.Range
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Forward = True
Selection.Find.MatchCase =True
Selection.Find.Format = False
Selection.Find.Wrap = wdFindContinue
Set pTextBox = ActiveDocument.StoryRanges(wdTextFrameStory)
on error goto 0
Do until pTextBox is nothing
With pTextBox.Text.Select <--- this is the statement
Selection.Find.Text = \"ABC\"
Selection.Find.Replacement.Text = \"XYZ\"
Selection.Find.Execute Replace:=wdReplaceAll
End With
set pTextBox = pTextBox.NextStoryRange
Loop
 
J

Jezebel

Your problem is not PC vs Mac, but lousy syntax.

You can't put a Select within the With construction like this. The basic
syntax is

With [Object]
.[Property] ...
.[Method] ...
End with

So you could use

With TextBox
.Select
...
End with

Although for your current purposes, you shouldn't be using the selection at
all. Range object are better than the Selection for nearly all purposes (and
a StoryRange is a kind of Range, so in the code you've got you can apply
Find directly). To do a Find and Replace on each textbox individually
(although as previously posted, that too is unnecessary), use --

Dim pTextBox as Word.Range

On error resume next
Set pTextBox = ActiveDocument.StoryRanges(wdTextFrameStory)
on error goto 0

Do until pTextBox is nothing
With pTextBox.Find
.Text = \"ABC\"
.Replacement.Text = \"XYZ\"
.Execute Replace:=wdReplaceAll
End With
set pTextBox = pTextBox.NextStoryRange
Loop
 
W

Word Heretic

G'day "Steve Cronin" <[email protected]>,

You dont have to select the range to find within it. Also, I'd be
using a For Each pTextBox in ActiveDocument.StoryRanges to govern the
outside loop.


Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Steve Cronin reckoned:
 
J

Jezebel

The task is to iterate the textboxes, not all the storyranges, so

For Each pTextBox in ActiveDocument.StoryRanges

isn't going to help.
 

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

Top