Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Word 2007: specialized macro possible? (i.e., do my desired functions


  • Please log in to reply

#1
soupshoe

soupshoe

    New Member

  • Member
  • Pip
  • 5 posts
I'm experienced at recording macros in Word (but not in programming in Visual Basic), and want to know if the following is possible to do in Word 2007.

I have frequent need for automated functions like this:
Find item A in a document, then find item B, but then have all text between item A and item B bolded or italicized.

Using the search function to find items A and B is self-explanatory, but it's the highlighting and italicizing that's the problem. Can a macro take care of this kind of thing? Ideally, the macro should go like this: Find item A. Engage Shift button or do something else to turn on highlighting, search for item B so that when it's found, everything behind it is highlighted. Then italicize selection.

How to do? Using "Shift + End" or "Shift + Home" to try to highlight usually either highlights too much or too little text. The function needs to be more precise than that.
  • 0

Advertisements


#2
Jonesey

Jonesey

    Member

  • Member
  • PipPipPip
  • 335 posts
This should be easy if you're used to recording macros

Within VB, this line will toggle bold on/off

Selection.Font.Bold = wdToggle

Likewise this will toggle italics on/off

Selection.Font.Italic = wdToggle

Note that it only works for text already selected - hence the 'selection' keyword

If you only ever want to switch formatting on, use = True instead of = wdToggle


If I were you, I'd select some text, then go Tools > Macro > Record New Macro

Then manually select bold & italic, then stop recording.

Then go tools > Macro > Macros

Highlight the macro you just recorded, which may well be the only one there, click Edit and you can now view the code you've just created.

Substitute the toggle commands for True if that suits your workflow better.

Hope this helps

Edited by Jonesey, 24 September 2008 - 11:55 AM.

  • 0

#3
soupshoe

soupshoe

    New Member

  • Topic Starter
  • Member
  • Pip
  • 5 posts
Thanks for your quick reply, Jonesey.

Your answer addresses how to get a macro to format text that has already been highlighted. But what I'm actually trying to do is get the macro to combine everything in one:
automatically search for text on either side of a targeted area, highlight/select the targeted area as it goes from left side to right side (without manual intervention from me), and then format that selected area.
  • 0

#4
Jonesey

Jonesey

    Member

  • Member
  • PipPipPip
  • 335 posts
Hmmm..

Setting up a search function in VB is equally easy.

Using the results of the search to generate a highlighted, or target area is a bit tricky in Word VB.

I'll work on it and get back to you, if anyone else wants to chip in, feel free.

This might be useful:

http://help.lockergn...pict989163.html

Edited by Jonesey, 25 September 2008 - 02:28 AM.

  • 0

#5
soupshoe

soupshoe

    New Member

  • Topic Starter
  • Member
  • Pip
  • 5 posts
I have accessed http://help.lockergn...pict989163.html and for testing purposes, I have recreated Jonathan's document text and macro coding. All is precisely as presented in the link, but I keep getting a syntax error when I run the macro.

It looks like this is precisely the kind of solution I need, though. If it would only *work!*
  • 0

#6
Jonesey

Jonesey

    Member

  • Member
  • PipPipPip
  • 335 posts
Hi there.

I've had a look at the sameple VB code in the above link and can confirm that there is indeed a syntax error.

I've modified it slightly and the following code should now work for you.

All you have to do is to substitute the words "Start" and "End" (Highlighted) with your actual search criteria and it should work.

Sub Macro1()
Dim myrng As Range, endrng As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Do While Selection.Find.Execute(findText:="Start", MatchCase:=True, Forward:=True, Wrap:=wdFindStop) = True
Set myrng = Selection.Range
Loop
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
Do While Selection.Find.Execute(findText:="End", MatchCase:=True, Forward:=True, Wrap:=wdFindStop) = True
Set endrng = Selection.Range
Loop
myrng.End = endrng.End
myrng.Select
Selection.Font.Bold = wdToggle
Selection.Font.Italic = wdToggle
End Sub



To make this even more user friendly, it's possible to have a couple of boxes pop up in which you type in the start and end words, making it unnecesary to even go into the VB editor.

I'm at work right now, but if I get a chance to have a play with this enhancement, I'll let you know.
  • 0

#7
soupshoe

soupshoe

    New Member

  • Topic Starter
  • Member
  • Pip
  • 5 posts
Getting there ... right direction ... but not quite.

I tried your macro coding and it runs without any syntax errors, but the desired result is not quite there yet. Specifically, when I run the macro it loops through the entire document without selecting or formatting anything, then it loops through again, skipping over every instance of appropriate targeted text until it reaches the very last example of text, which it then selects and formats properly.

One more tweak?
  • 0

#8
Jonesey

Jonesey

    Member

  • Member
  • PipPipPip
  • 335 posts

Getting there ... right direction ... but not quite.

I tried your macro coding and it runs without any syntax errors, but the desired result is not quite there yet. Specifically, when I run the macro it loops through the entire document without selecting or formatting anything, then it loops through again, skipping over every instance of appropriate targeted text until it reaches the very last example of text, which it then selects and formats properly.

One more tweak?



That's really strange.

I've just copied the code into a Word document of my own, set the start and end search strings and it works perfectly.

Are you sure that:

1 - you've substituted your own start/end words into the body of the code
2- they're the right way round? i.e. your starting word goes into the first loop, and the end word into the second loop
3 - you're spelled them correctly? Bearing in mind that the search is case sensitive.
  • 0

#9
Jonesey

Jonesey

    Member

  • Member
  • PipPipPip
  • 335 posts
Ok. This works perfectly for me.

Try it and let me know what happens.

------------------------------------------------------------------------------

Sub Macro1()
Dim myrng As Range
Dim endrng As Range
Dim boxtitle As String
Dim message1 As String
Dim message2 As String
Dim starttext As String
Dim endtext As String

boxtitle = "Format Selected Text"
message1 = "Enter the START text in the box below"
message2 = "Enter the END text in the box below"
starttext = InputBox(message1, boxtitle)
endtext = InputBox(message2, boxtitle)

ActiveDocument.Words.First.Select
Selection.HomeKey wdStory
Selection.Find.ClearFormatting

Do While Selection.Find.Execute(findText:=starttext, MatchCase:=True, Forward:=True, Wrap:=wdFindStop) = True
Set myrng = Selection.Range
If myrng = starttext Then Exit Do
Loop

ActiveDocument.Words.First.Select
Selection.HomeKey wdStory
Selection.Find.ClearFormatting

Do While Selection.Find.Execute(findText:=endtext, MatchCase:=True, Forward:=True, Wrap:=wdFindStop) = True
Set endrng = Selection.Range
If endrng = endtext Then Exit Do
Loop

myrng.End = endrng.End
myrng.Select
Selection.Font.Bold = wdToggle
Selection.Font.Italic = wdToggle
End Sub

------------------------------------------------------------------------------

This has been quite a fun exercise - I've never written VB for Word before, only Excel, but they're so similar. it's just a question of getting used to the different keywords in Word.

Hope this works for you.

EDIT: higlighted word above s/be endrng, not myrng

Edited by Jonesey, 02 October 2008 - 02:57 AM.

  • 0

#10
soupshoe

soupshoe

    New Member

  • Topic Starter
  • Member
  • Pip
  • 5 posts
Hi Jonesey,

Success!! Thanks so much for your time and work on this. It will help me save TONS of time on a very LARGE editorial project I'm working on right now.

What a valuable resource this forum is. Glad I found you!
  • 0

#11
Jonesey

Jonesey

    Member

  • Member
  • PipPipPip
  • 335 posts
No problem my friend.

It was good experience for me also.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP