Hi, Henry,
Since Howard and Anne have given you reassurances, I'll just throw in answers to
a couple of your specific questions.
First, let's talk about the amount of code you get from the recorder versus the
amount of code you actually need to get something done in Word VBA. There's a
huge difference, especially when you record one of Word's built-in dialogs. For
instance, record the Replace dialog, and you'll get a mess like this:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 5/11/2002 by Jay Freedman
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "find me"
.Replacement.Text = "found it"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
This lists every setting in the entire dialog, whether or not you changed it.
No, you don't need all of that! Probably all you really need is this:
Sub Macro2()
With Selection.Find
.Text = "find me"
.Replacement.Text = "found it"
.Execute Replace:=wdReplaceAll
End With
End Sub
(Now the small print: The settings in the Find and Replace dialogs are
"sticky" -- they keep their values from one execution to the next -- so if you
change one of the settings in a manual Replace and then run the macro, that
setting will still be in effect. The first Find or Replace in each macro should
change all of the settings to a known condition.)
The other dialogs show similar behavior when you record them. Just open up the
Help topic for the command (such as Find.Execute), read the list of parameters,
and omit the optional ones unless you need them for something specific.
The question about "navigating around the page" is a little tougher to answer.
Many times you don't have to "navigate" at all. You can use the Find method to
go directly to some text of interest (and remember that you can find styles or
formatting as well as words, and you can use "wildcard searching"). You can
change formatting by applying or changing styles. You can work with bookmarks,
fields, or other objects that are already in the text, usually because they were
in the document's template. If you need to process every character, word,
paragraph, or section in the document, you can use the For Each ... Next
construct (although this gets terribly slow when you have to look at each
character or word).
Browse through the articles on the Macros/VBA tab at
http://www.mvps.org/word/FAQs/index.html to get a sense of what's available, and
then post questions here. We're happy to help!
--
Regards,
Jay Freedman
Microsoft Word MVP Word MVP FAQ site: http://www.mvps.org/word
Quote:
> Hi everyone,
> I am fairly new to VBA and was wondering how different the
> two versions of VBA are. I learnt Excel VBA by recording a
> few macros and learning from that; to the point where I
> could write my own code.
> I had a quick look at Word VBA and the amount of code that
> it generates for some simple actions seems quite daunting.
> Also, without cell reference, how does one navigate around
> the page?
> I am almost considering putting 'Learning Word VBA' into
> the too hard basket.
> I'm just seeking some comments on how hard/easy it is to
> learn Word VBA and how much of the stuff that I have
> learnt from Excel VBA can be transferred across onto this
> platform.
> Feel free to contact me via my email.
> Henry.