 |
Search
IT Courses |
|
|
|
|
|
|
|
|
|
|
|
IT
dart News Letter
|
Get
ITdart.com weekly headlines before it's published on our site! Subscribe
and receive the articles delivered to your inbox!
|
|
|
Learning MS Word object model
As you must have realised by now, the programmability of Word is vast and extensive. To remember all the features of all objects is not only close to impossible but is impractical as well. Therefore, you need to have some quick method of learning more about the objects and their properties.
There are two methods to accomplish this:
Learn from the Help file of Word VBA. Learn from macros automatically recorded by Word. Learning from the Help file:
This would be simple if you knew which object or method you wanted to access. You can also traverse the object model and go deeper into objects, methods and properties. This provides you with syntax and sample code in most cases.
Unfortunately, the Help file is not always useful as some syntactical elements are not very intuitive. In addition some tasks require multiple steps which are not mentioned in the Help file in that particular order. Here the second method comes in handy.
Learning from recorded macros:
Word has a powerful macro feature allowing you to create macros by recording the actions you perform. The macro is actually a VBA program that is created by Word depending upon the recorded actions. This program can be read and edited by you.
This recorded program is of immense help in learning the VBA-equivalent code of complex Word tasks.
Let us say we have a document where we want to convert the word 'Fox' to 'fox'. We want to programmatically change the font size, colour and replace all instances of the word 'Fox' with the new format. Let us first create a recorded macro.
Choose the Macro - Record New Macro... option from Word's Tools menu Give a name to the macro, say, Sample. Press Ctrl-Home to go to the beginning of the document. Choose the Replace... option from the Edit menu. Click the More button in the Find and Replace dialog box. Type 'Fox' in the Find what: field. Type 'fox' in the Replace with: field. Now click the Format button. Select Font... from the menu and select the required font. Mark the font size as 14 and colour as blue. When done, click OK. Now click the Replace All button in the Find and Replace dialog box. All instances of the word 'Fox' are replaced with the new word 'fox'. Now let us see what was recorded by the Macro recorder: Click the Stop button on the recorder toolbar. Select Macro - Macros... from the Tools menu. Choose the Sample macro and click the Edit button. Here's what the recorded code should look like:
Sub Sample() '' Sample Macro ' Macro recorded 13/01/99 by Times Computing Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting Selection.Find.Replacement.Font.Size = 14 Selection.Find.Replacement.Font.ColorIndex = _ WdColorIndex.wdDarkBlue With Selection.Find .Text = "Fox" .Replacement.Text = "fox" .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll End Sub Now you can follow this code line by line and understand the exact methodology a lot easier than having to refer to the Help file a number of times. You can also modify this code and try various other variations to learn more about the features in a shorter time span
|
|
|