PDA

View Full Version : Troubleshoot code please?



blaze2012
04-14-2012, 01:47 AM
Hey everyone,
I'm hoping someone can help me out with this please ?

I'm trying to set up a macro to :
remove double spacing between words
remove double paragraph spacing
change body text font to garamond 12pt

I'm using the macro recorder and find / replace to do it.

I can get it to work for the first 2 action but it's not changing the font.
Can you take a look at the code and see if there's any way to tweek it ?

Thanks.

macropod
04-15-2012, 12:50 AM
Hi Blaze,

First off, when posting code, please post the code - not an image of it. And, when doing, so, please use the VBA tags.

Rather than trying to hard-format the text in 12pt Garamond, you should define a Style with those attributes and apply the Style. The following code should achieve what you're after:
Sub Reformat()
Application.ScreenUpdating = False
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
'Clean up double spacing
.Execute Replace:=wdReplaceAll
.Text = "[ ]{2,}"
.Replacement.Text = " "
'Clean up multiple paragraph breaks
.Execute Replace:=wdReplaceAll
.Text = "[^13]{2,}"
.Replacement.Text = "^p"
'Validate all paragraph breaks and apply the required Style
.Execute Replace:=wdReplaceAll
.Text = " ^13"
.Replacement.Style = "SomeStyle"
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
You will, of course, need to rename 'SomeStyle' to whatever your 12pt Garamond Style is. And, if you want the Find/Replace to work on a specific selection, change 'ActiveDocument.Content.Find' to 'Selection.Find'.

fumei
04-15-2012, 01:00 AM
Nice, clean, well articulated response.

macropod
04-15-2012, 02:21 AM
Cross-posted at: http://www.msofficeforums.com/vba/12217-changing-font-vba.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184

blaze2012
04-15-2012, 02:40 AM
Sorry Paul,
That's what lack of sleep will get you, I thought I'd joined 2 different forums.

Thank you so much for the response, I really appreciate you taking the time to put that together for me.

Now I just have to sit down with it and work out what it all means.

Thanks again.

macropod
04-15-2012, 03:06 AM
I thought I'd joined 2 different forums.
That is indeed what you did. There is no objection to cross-posting per se. However, you posted essentially the same question in both forums, without giving any indication in either of them that's what you'd done.