Basic VB macro help

R

Reuben

Hi - I am very new to macros and VB in particular and I wonder if
someone could help me? I have a basic VB macro set up that I would
like to get resolved and I seem to have drawn a blank. For this
project I am using the generic VB script used for making macros.

Essentially I am trying to automate a procedure for addressing letters
(I am a secretary so that is generally what I do), some of the names
are hyphenated and I know Word sees the hyphenated words as separate.
When I try to run the If statement on the area it comes up with a
"Runtime 424 error, object required" and although I'm sure it is
fairly easy to find a hyphen amongst selected text, it doesn't seem to
want to.

I copy below the part of the macro in question and hopefully someone
could let me know what I am doing wrong.

Selection.Find.ClearFormatting
With Selection.Find
.Text = "-"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
If Find.Found = True Then
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=3, Extend:=wdExtend
Selection.Copy
GoTo 2
Else:
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=3, Extend:=wdExtend
Selection.Copy
GoTo 2
End If

Hopefully this doesn't sound too rubbish, I really would appreciate a
hand here if someone could spare one. :)

Reuben
 
J

Jonathan West

Hi Reuben,

4 problems there

1. You have the Find object in th second half of your code, but without it
being attached there to the selection object as it is in the first half.

2. Having defined what you want to find, you have to tell the macro actually
to go and find it! This is done with the Execute method.

3. You don't want a colon after the Else statement. If you want the line
below to execute, Else should be by itself.

4. It seems that the code after the If statement is the same as the code
after the Else statement, so the same thing will be done whether or not
something is found. Is that your intention.

Also, you'll find it easier to keep track of the logic of the program if you
are consistent about indenting code within lops and branches. For more on
that general subject, you might like to take a look at this article

The art of defensive programming
http://word.mvps.org/FAQs/MacrosVBA/MaintainableCode.htm

Change your code so it looks like this

Selection.Find.ClearFormatting
With Selection.Find
.Text = "-"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
If .Found = True Then
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=3, Extend:=wdExtend
Selection.Copy
GoTo 2
Else
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=3, Extend:=wdExtend
Selection.Copy
GoTo 2
End If
End With
 
R

Reuben

Hi again

Sorry to be a pain but I am still getting problems with this script,
generally still getting "runtime 424 Object error", and the debugging
highlights
If Find.Found = False Then
but this only occurs on this text, whether the script states True or
False.

I tried to use your .Found line but it didn't like the code so I
changed it back to Find.Found.

Would it be easier to make the script see hyphens as part-words rather
than as separate words?

Thanks for your help so far though :)

if hyphen found, copy 3 words, if not then copy 1
Selection.Find.ClearFormatting
With Selection.Find
.Text = "-"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
If Find.Found = True Then
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=3, Extend:=wdExtend
Selection.Copy
GoTo 2
Else
Selection.EndKey Unit:=wdLine
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Copy
GoTo 2
End If
 
J

Jonathan West

Look more carefully at my code and at your own.

The Find object must be qualified, by what you are looking within, in this
case the selection.

Also, if you want people to be able to help, you need to give a more
specific description of a problem than "it didn't like the code". In what
way didn't it work? What exactly happened? What did you want to happen?

The machines unfortunately aren't psychic, and neither are we. Unless you
can describe your problem precisely, there's no way to code it.
 

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