|
-
Apr 22nd, 2004, 09:03 PM
#1
Thread Starter
Addicted Member
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.
-
Apr 23rd, 2004, 04:50 AM
#2
Fanatic Member
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:
Sub Macro1()
Dim i As Integer
Dim bDone As Boolean
Selection.HomeKey Unit:=wdStory
For i = 1 To Application.ActiveDocument.Words.Count Step 2
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
bDone = False
Do While Not bDone
If Selection.Text = "," Or Selection.Text = "." Or Selection.Text = " " Then
Selection.MoveRight Unit:=wdWord, Count:=1
Else
bDone = True
End If
Loop
Selection.Font.Bold = wdToggle
Selection.MoveRight Unit:=wdWord, Count:=2
Next i
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
-
Apr 23rd, 2004, 10:22 AM
#3
Lively Member
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:
Private Sub BoldEOWord()
'Declare variable for count of words in active document
Dim iDocWordCount As Integer
'If you want to use the word count dialog box use the following code, but the rest
'of my code uses the active document's word.count property
'Dim DialogWordCount As Word.Dialog
'Set DialogWordCount = Dialogs(wdDialogToolsWordCount)
'DialogWordCount.Execute
' iDocWordCount = DialogWordCount.Words
'Declare variable for active document
Dim DocToUse As Word.Document
'Set document to currently active document
Set DocToUse = Word.ActiveDocument
Let iDocWordCount = DocToUse.Words.Count
'Declare variable for word to bold
Dim iWordToBold As Integer
'Set value to 1, or set to 2 if you want to start on the second word
Let iWordToBold = 1
'Declare toggle variable for bold or not bold
Dim EvenWord As Boolean
'Start the process
With DocToUse
'Go to first word
.Words(iWordToBold).Select
With Selection
.Font.Bold = True
End With
EvenWord = False
iWordToBold = iWordToBold + 1
'Start loop on second word
Do Until iWordToBold >= iDocWordCount
.Words(iWordToBold).Select
Select Case Selection.Characters.Count
Case Is = 1
If Selection.Text = "-" Then
If EvenWord = False Then
Selection.Font.Bold = True
EvenWord = True
Else
EvenWord = False
End If
End If
iWordToBold = iWordToBold + 1
Case Is = 2
Select Case Selection.Text
Case Is = ". "
iWordToBold = iWordToBold + 1
Case Is = ", "
iWordToBold = iWordToBold + 1
Case Is = ": "
iWordToBold = iWordToBold + 1
Case Is = "; "
iWordToBold = iWordToBold + 1
Case Else
'small words like "a" "I", this is where number lists
'and bullets may cause issues
With Selection
If EvenWord = True Then
.Font.Bold = True
EvenWord = False
Else
EvenWord = True
End If
End With
iWordToBold = iWordToBold + 1
End Select
Case Is = 3
If Selection.Text = ". " Then
iWordToBold = iWordToBold + 1
Else
With Selection
If EvenWord = True Then
.Font.Bold = True
EvenWord = False
Else
EvenWord = True
End If
End With
iWordToBold = iWordToBold + 1
End If
Case Is > 3
With Selection
If EvenWord = True Then
.Font.Bold = True
EvenWord = False
Else
EvenWord = True
End If
End With
iWordToBold = iWordToBold + 1
End Select
Loop
End With
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
-
Apr 24th, 2004, 01:40 AM
#4
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|