Results 1 to 4 of 4

Thread: Anyone good at MS Word Visual Basic macros? [resolved]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Posts
    183

    Anyone good at MS Word Visual Basic macros? [resolved]

    I have a very ingraned, hard to stop habit of skimming and filling in the blanks while I read. I have to give a history presentation in a few days and one thing that helps me a lot is to have every second word in bold.

    Normally I wouldnt make a request like this, but Ive helped a lot of people on this forum, and I believe this would be quick and easy for those who already know how to do it.

    If its not too much trouble for you, could someone please make me a simple macro that makes every second word bold?

    Id appreciate it very much!
    Last edited by Evil_Cowgod; Apr 24th, 2004 at 01:41 AM.

  2. #2
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    Am I any good at Word Macros? Never laid eyes on the beasts until about 30 minutes ago....but being the curious sort that I am, I opened Word and came up with the following:

    VB Code:
    1. Sub Macro1()
    2.     Dim i As Integer
    3.     Dim bDone As Boolean
    4.     Selection.HomeKey Unit:=wdStory
    5.    
    6.     For i = 1 To Application.ActiveDocument.Words.Count Step 2
    7.         Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
    8.         bDone = False
    9.         Do While Not bDone
    10.             If Selection.Text = "," Or Selection.Text = "." Or Selection.Text = " " Then
    11.                 Selection.MoveRight Unit:=wdWord, Count:=1
    12.             Else
    13.                 bDone = True
    14.             End If
    15.         Loop
    16.         Selection.Font.Bold = wdToggle
    17.         Selection.MoveRight Unit:=wdWord, Count:=2
    18.     Next i
    19.  
    20. End Sub

    I started by recording my actions, then looking at the code. Made some changes, added a bit of logic to omit punctuation like commas and periods (you might want look at skipping the double quotes as well) and voila - it bolds every second word. Best of luck with your presentation. cheers.
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

  3. #3
    Lively Member
    Join Date
    Jul 2002
    Posts
    78
    Okay, this one is a bit more difficult than it sounds.

    Using the word.count does not count as you think it would. As indicated, it does count things like commas and periods as words... don't forget semi-colons, colons, apostrophes, and the whole gamut of symbols!!! NOT TO MENTION PARAGRAPHS (you can see them by toggling show/hide on the toolbar). You can get a more accurate word count using/automating the word count dialog box... however, then the complication comes when navigating through the document... because Word will still go through these items that you and I would not consider words (commas, periods, paragraphs, etc.). Hyphens of course cause the worst problem, because you have to account for these as being in the middle of what you would consider a word in this instance (you wouldn't want half bold).

    Through trial-and-error you can modify your code as you find non-words cropping up... you will need to determine how much time you really want to spend on getting this just right, or close enough. If you find a modification you need to make in the code, you can simply undo the previous macro bold assignment, revise your case selection, then run the code again!!

    I tried several ways last night to accomplish this with "simple" code, but to no avail. Following is what I've settled into. It does not account for everything, but I don't have a need to do anything like this so I'll let you decide how much more time to invest.

    VB Code:
    1. Private Sub BoldEOWord()
    2. 'Declare variable for count of words in active document
    3.     Dim iDocWordCount As Integer
    4. 'If you want to use the word count dialog box use the following code, but the rest
    5. 'of my code uses the active document's word.count property
    6.     'Dim DialogWordCount As Word.Dialog
    7.     'Set DialogWordCount = Dialogs(wdDialogToolsWordCount)
    8.     'DialogWordCount.Execute
    9.    ' iDocWordCount = DialogWordCount.Words
    10.    
    11. 'Declare variable for active document
    12.     Dim DocToUse As Word.Document
    13.     'Set document to currently active document
    14.     Set DocToUse = Word.ActiveDocument
    15.     Let iDocWordCount = DocToUse.Words.Count
    16. 'Declare variable for word to bold
    17.     Dim iWordToBold As Integer
    18.     'Set value to 1, or set to 2 if you want to start on the second word
    19.     Let iWordToBold = 1
    20. 'Declare toggle variable for bold or not bold
    21.     Dim EvenWord As Boolean
    22. 'Start the process
    23. With DocToUse
    24.       'Go to first word
    25.       .Words(iWordToBold).Select
    26.         With Selection
    27.             .Font.Bold = True
    28.         End With
    29.        EvenWord = False
    30.         iWordToBold = iWordToBold + 1
    31. 'Start loop on second word
    32.     Do Until iWordToBold >= iDocWordCount
    33.       .Words(iWordToBold).Select
    34.       Select Case Selection.Characters.Count
    35.         Case Is = 1
    36.                     If Selection.Text = "-" Then
    37.                         If EvenWord = False Then
    38.                             Selection.Font.Bold = True
    39.                             EvenWord = True
    40.                         Else
    41.                             EvenWord = False
    42.                         End If
    43.                     End If
    44.             iWordToBold = iWordToBold + 1
    45.        
    46.         Case Is = 2
    47.             Select Case Selection.Text
    48.                 Case Is = ". "
    49.                      iWordToBold = iWordToBold + 1
    50.                 Case Is = ", "
    51.                      iWordToBold = iWordToBold + 1
    52.                 Case Is = ": "
    53.                      iWordToBold = iWordToBold + 1
    54.                 Case Is = "; "
    55.                      iWordToBold = iWordToBold + 1
    56.                 Case Else
    57.                     'small words like "a" "I", this is where number lists
    58.                     'and bullets may cause issues
    59.                     With Selection
    60.                         If EvenWord = True Then
    61.                             .Font.Bold = True
    62.                             EvenWord = False
    63.                         Else
    64.                             EvenWord = True
    65.                         End If
    66.                     End With
    67.                 iWordToBold = iWordToBold + 1
    68.             End Select
    69.        
    70.         Case Is = 3
    71.             If Selection.Text = ".  " Then
    72.                 iWordToBold = iWordToBold + 1
    73.             Else
    74.                     With Selection
    75.                         If EvenWord = True Then
    76.                             .Font.Bold = True
    77.                             EvenWord = False
    78.                         Else
    79.                             EvenWord = True
    80.                         End If
    81.                     End With
    82.                 iWordToBold = iWordToBold + 1
    83.             End If
    84.         Case Is > 3
    85.                 With Selection
    86.                     If EvenWord = True Then
    87.                         .Font.Bold = True
    88.                         EvenWord = False
    89.                     Else
    90.                         EvenWord = True
    91.                     End If
    92.                 End With
    93.             iWordToBold = iWordToBold + 1
    94.       End Select
    95.            
    96.     Loop
    97. End With
    98.  
    99. End Sub

    You will need to add error trapping as you see fit.

    Also, this uses direct formatting... if your document contains any special formatting that includes bold, you may get unexpected results (I didn't use styles, bullets, numbers, etc. during my testing, so you might want to account for that if other people will need to use this macro later).

    Save the macro in your Normal.dot, add it to your toolbar and it's there and ready for you at the click of a button!

    Good luck!
    Mary

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2002
    Posts
    183
    Thanks a lot for this! Its going to be a big help! Im impressed how much you two did.

    p.s. Sorry for not replying sooner.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width