Microsoft Word - Help me in defininging macro in MS Word 2003

Asked By Emebet
30-Nov-10 07:50 AM

I need help.  Here is the case.

I want a code or a macro which finds and replace acronym in a document only on the first occurrence if it not mentioned. For example in a big MS Word 2003 document to check the first occurrence of USA acronym  and check if it has United States of America (USA) it has to stop the search if not to replace USA with United States of America (USA).



Can anyone help me in writing the macro  (a code in html or any other language which is comparable with VB) to perform the above task?

Many thanks
Emebet
  olvin j replied to Emebet
30-Nov-10 07:24 PM
hi


  • Open a new Word document
  • Type in a few words of text. Anything you like
  • Highlight your text, or just a single word of the text
  • From the menu bar, click on Tools
  • From the drop down menu click on Macro
  • A sub menu appears
  • Click on Record New Macro

Click Macro > Record New Macro

When you click on Record New Macro, you get the Macro dialogue box popping up. It looks like the one below:

The Record Macro dialogue box

There are four areas to the Record Macro dialogue box: Macro Name, Assign macro to, Store macro in, and Description.

The first thing to do is to give your macro a name. At the moment it is called "Macro1". That's not a very descriptive name for what we want our macro to do. We'll call our macro FontChanger. Notice that we haven't put a space between the two words. This is because macros don't like having spaces between words. You have to put the name of your macro as all one word.

So go ahead and type in FontChanger as the Macro name, instead of Macro1.

The next section is "Assign macro to". You are given the choice of either Toolbars or Keyboard. Ignore this section for now. We'll see later how to assign our macro to a toolbar, and to the keyboard.

The next section is "Store macro in". At the moment it reads All Documents (Normal.dot). Remember what Normal.dot is? That's the basic template for all Microsoft Word documents. In other words, the macro we create will be available to any new Word document. You can change this so that the macro is created in only the document you're working on. Click the black down arrow to see the title of the document you have open.

The final section is Description. This is just a record of when the macro was created, and who created it. You can change this to anything you like. Malicious folk creating nasty macros will definitely change these details.

Once you have given your macro a name, click the OK button. Your dialogue box will look like this:

Change your Record Macro details to these

When you click the OK button, you are returned to your Word document. But now you will see a strange toolbar floating over your page. And the mouse pointer will be different. Your page will look like this:

The Macro Recorder

The reason the little floating toolbar says Stop is because your macro is already being recorded. The process has started. You don't have to panic, and hurry it along. Take your time and think about what you're going to do.

The floating toolbar, though, has only two buttons. The square one is the stop button; the two lines and the circle is the pause button. Notice that the mouse pointer has changed, with a little cassette tape on the end of it. This indicates that you are recording a macro.

One thing you can't do with a macro is record mouse movements. If you want to move your cursor somewhere in your text, you'll have to use the arrow keys on your keyboard. But the movement of the cursor then becomes part of your macro.

For our macro, because we highlighted the text before recording, we don't need to move the cursor. Whatever we do from now on will become part of our macro.

  • So click on Format from the menu bar
  • From the drop down menu, click on Font
  • The Font dialogue box appears
  • Select Arial as your font
  • Select Bold
  • Select size 16
  • Click the OK button on the Font dialogue box
  • You are returned to your page
  • Click the Stop button on your floating Macro Recorder
  • The macro will stop recording, and the process is finished: You have recorded the macro

If you make a mistake during the recording of your macro, and everything goes horrendously and badly wrong, you can abort the recording and try again. To abort your recording and try again, do the following:

  • Click the Stop button on your recorder

Click the Stop Button

  • The recorder disappears
  • Click Tools > Macro > Record New Macro
  • In the Macro name part of the dialogue box that pops up, type in the name of the macro that went wrong. In our case that would be FontChanger.
  • Click OK
  • Word displays a message box telling you that a macro with that name already exists. It asks if you want to replace it
  • Click Yes
  • You are returned to your document, and the recorder is displayed
  • You can now try again
  • If things go wrong again, repeat these instructions

If nothing went wrong, you will now have a macro. So how do you get at it? To use your new macro, do the following

  • Type in some new text and Highlight it
  • Click on Tools from the menu bar
  • From the drop down menu, click on Macro
  • From the sub menu that appears, click on Macros
  • The Macros dialogue box appears:

Our Macro is available to Run

Select the Macro that you want, then click the Run button at the top right of the dialogue box. As you can see, the macro we created is in the list, and already highlighted. When the Run button is clicked, the dialogue box will disappear, and the highlighted text will change to Arial, Bold 16 points.

You might argue that by clicking on Tools > Macro > Macros, and then fiddling about with the dialogue box is not exactly a shortcut. Surely it's just as easy to click on Format > Font, and make your changes from there?

  Rolf Jaeger replied to Emebet
30-Nov-10 08:18 PM
Hi Emebet:

the macro listed below should do the trick for you.

Hope this helped,
Rolf

Const NUMBER_OF_ACRONYMS As Integer = 2
  
Private Type Acronyms
  acronym As Variant
  definition As Variant
End Type
Private aList() As Acronyms
Private a() As String
  
Sub InitializeAcronymCollection()
  ReDim aList(NUMBER_OF_ACRONYMS)
  aList(0).acronym = "USA"
  aList(0).definition = "United States of America"
  aList(1).acronym = "SAT"
  aList(1).definition = "Standard Aptitude Test"
End Sub
  
Sub ReplaceAcronyms()
  InitializeAcronymCollection
  Dim i As Integer
  Dim aPos As Integer, aDefinitionPos As Integer
  For i = 0 To UBound(aList) - 1
    aDefinitionPos = PositionOfSearchString(aList(i).definition)
    aPos = PositionOfSearchString(aList(i).acronym)
    If aDefinitionPos < 0 Then
      Call ReplaceAcronymWithDefinition(aList(i).acronym, aList(i).definition)
    Else
      If aPos < aDefinitionPos Then Call ReplaceAcronymWithDefinition(aList(i).acronym, aList(i).definition)
    End If
  Next i
End Sub
  
Function PositionOfSearchString(s As Variant) As Integer
  Selection.HomeKey Unit:=wdStory
  With Selection.Find
    .Text = s
  End With
  If Selection.Find.Execute = True Then
    PositionOfSearchString = Selection.Start
  Else
    PositionOfSearchString = -1
  End If
End Function
  
Sub ReplaceAcronymWithDefinition(acr As Variant, definition As Variant)
  Selection.HomeKey Unit:=wdStory
  With Selection.Find
    .Text = acr
    .Replacement.Text = definition & " (" & acr & ")"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
  End With
  Selection.Find.Execute
  With Selection
    If .Find.Forward = True Then
      .Collapse Direction:=wdCollapseStart
    Else
      .Collapse Direction:=wdCollapseEnd
    End If
    .Find.Execute Replace:=wdReplaceOne
    If .Find.Forward = True Then
      .Collapse Direction:=wdCollapseEnd
    Else
      .Collapse Direction:=wdCollapseStart
    End If
    .Find.Execute
  End With
End Sub
Create New Account
help
What is the capital of the united states Word whatis the capital of the united states Word Document Management Discussions Suzanne S. Barnhill (1) Publisher 2007 (1) Word (1) Microsoft Publisher (1) Emerald City (1) OP (1) Google (1) Capital of the united states (1) Hooplehead Alert. Everyone knows it is Emerald City. Google. - - Suzanne S. Barnhill Microsoft MVP
Count names and occurrences Excel I have a long list of cities (Col. A) and states (Col. B). I would like to count the number of times each city occurs without hundred after they have been sorted by state and city. Thanks for bothering with this. United States Birmingham Alabama 3 United States Birmingham Alabama 3 United States Birmingham Alabama 3 United States Decatur Alabama 2 United States Dothan Alabama 1 United
Left or Like function? SQL Server GroupName - -- -- -- -- -- -- - Fedex.United States.Texas.Dallas Fedex.United States.New York.New York Fedex.United States.Texas Fedex.Australia.New South Wales.Sydney . . . . . . I want a SELECT query to show all
If so what function do I use? Excel My data is: A B C TX United States MA NY United States NJ NH United States London United Kingdom Paris France Column A has states that are almost always populated, but
12 / 02 / 2006 06:55:55" UPTO_LINK_DATE = "12 / 02 / 2006 06:55:55" VER_LANGUAGE = "English (United States) [0x409]" / > BIN_FILE_VERSION = "8.0.50727.762" BIN_PRODUCT_VERSION = "8.0.50727.762" PRODUCT_VERSION = "8.00.50727.762 12 / 02 / 2006 10:08:33" UPTO_LINK_DATE = "12 / 02 / 2006 10:08:33" VER_LANGUAGE = "English (United States) [0x409]" / > BIN_FILE_VERSION = "14.0.50727.762" BIN_PRODUCT_VERSION = "8.0.50727.762" PRODUCT_VERSION = "8.00.50727.762 12 / 02 / 2006 06:57:57" UPTO_LINK_DATE = "12 / 02 / 2006 06:57:57" VER_LANGUAGE = "English (United States) [0x409]" / > BIN_FILE_VERSION = "14.0.50727.762" BIN_PRODUCT_VERSION = "8.0.50727.762" PRODUCT_VERSION = "8.00.50727