PDA

View Full Version : Find properties not sticking in recorded macro



ashley
10-19-2006, 03:17 PM
I need to replace the first number in each row with not superscript - bold numbers, then add a space immediately after the number. That first number could be superscript, not s-script, bold, italic, have space(s) immediately after, a combination or none of the above. See samples and recorded macro below.

**I just noticed the superscript examples show up as regular script on the site. sorry!

Sample original text:
1New York University School Of Medicine, 10010
2New York University School Of Medicine, 10010
3 New York University School Of Medicine, 10010
4New York University School Of Medicine, 10010


How they all should look:
1 New York University School Of Medicine, etc.


How they look instead:
1 New York University School Of Medicine, 1 0 0 1 0
2 New York University School Of Medicine, 1 0 0 1 0
3 New York University School Of Medicine, 1 0 0 1 0
4 New York University School Of Medicine, 1 0 0 1 0

Here's the macro I recorded - worked great the first time (my test group only had superscript numbers). But when I run the macro again, none of the character formatting works - just adds the extra space. I tried also toggling between cases using Selection.Font.Superscript = wdToggle, but I ended up superscripting stuff that shouldn't be s-scripted. I can't figure out how to test for case. Ideas? Thanks a lot.


Sub FixAffil_3()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "^#"
.Replacement.text = "^& "
.Forward = True
.Wrap = wdFindAsk
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

fumei
10-20-2006, 12:17 AM
Unfortunately, recording macros does not generally allow for sophisticated logic. And if you are testing for such varied conditions:

- there may be a space...there may not be
- it may be bold...it might not be
- it may be bold WITH a space
- it may be bold with NO space
- it may be bold WITH a space, AND italics
- it may be bold WITH a space, and NO italics
- it may be superscript, italics, with a space
- it may be superscript, bold, with NO space

and on and on.

So. What to do? The following does what you want. It works with the Selection - so you have to actually select text. It could be done with non-selected text, but you would have to define by what logic it would operate.

It also assume - and this is important - that each "line" is a separate paragraph. That is, each "line" has a paragraph mark. It also assumes this is not in a table. It could be done for a table, but again, you need to explicit define your requirements.Sub DoStuff()
Dim oPara As Word.Paragraph
Dim oRange As Word.Range
For Each oPara In Selection.Paragraphs
Set oRange = oPara.Range
' collapse Range and make it the first
' two characters
oRange.Collapse Direction:=wdCollapseStart
oRange.MoveEnd unit:=wdCharacter, Count:=2
' check if last character is a space or character
If Right(oRange.Text, 1) = " " Then
' there IS a space
' do format
With oRange.Font
.Bold = True
.Superscript = False
.Subscript = False
.Italic = False
End With
Else ' there is NOT a space - it is a character
' move the range back one character
oRange.MoveEnd unit:=wdCharacter, Count:=-1
' do format
With oRange.Font
.Bold = True
.Superscript = False
.Subscript = False
.Italic = False
End With
' and add the space
oRange.InsertAfter " "
End If
Set oRange = Nothing
Next
' following could be removed - it just collapses
' the selection so it is no longer highlighted
Selection.Collapse Direction:=wdCollapseStart
End SubWhat does this do?

It takes the selection and loops through making a Range of each paragraph. It collapses the Range to the start of the paragraph, then expands to include only the first two characters.

It then checks to see if the last character of the new Range (ie. the first two characters) is a space. It is IS a space, it then explicitly sets the font attributes for the Range. It makes it bold, and explicitly makes sure superscript is OFF, italics is OFF, subscript is OFF.

If the last character of the new Range (that is, the first two characters of the paragraph) is NOT a space - it is a character - the code then resizes the range to just the first character, and applies the explicit format. It then adds a space after it.

It looks complicated, but it really is not. By making explicit formats there is no need to do check on what the character is (bold/not bold, superscript/not superscript etc) - the code just says, I don't give a crap what you are now...you are THIS format now.

Which is - if you really state the logic - what you want to do. I want:

The first character to be bold (and only bold, regardless of what it is now) and followed by a space (which it may or may not have currently).

mdmackillop
10-20-2006, 09:52 AM
the code just says, I don't give a crap what you are now...you are THIS format now. :rotlaugh:
Hi Ashley
Welcome to VBAX.
I see your solution is well in hand.

A couple of pointers:
If you select your code and click the VBA button, the code gets formatted making it more readable. Remember to split long lines of code with linebreaks or they run off the screen.
You can post sample files (you may need to zip them) using Manage Attachments in the Go Advanced section.
Regards
MD

ashley
10-20-2006, 12:07 PM
Gerry, thanks! The code works like a charm. Thanks too for summing up how/why it works, especially the explicitly setting font attributes. I'm gonna try to apply the same thinking to styling paragraphs "x" if the selection is NOT styled "a," "b," or "c."

I'm really relieved I found this board. I work for a small non-profit medical/scientific publishing company (Public Library of Science) (My job is to crank out all the metadata and tagging for every manuscript before I turn it over to our copyeditors. I manage documents from all around the world, all different kinds of platforms, and all kinds of word processing s/w. Oh yeah, and all kinds of different (ouch!) author interpretations of how their manuscripts should be constructed.

Anyway, interest in being published in our journals is growing by leaps and bounds, as is the volume streaming across my desk. I'm trying to super-size my macros so I can handle as much as possible before I have to hire a freelancer for the overload. That said, I'm definitely a "word" person, more comfortable with prepositions than properties. So bear with me - you'll see a lot of me on the board!