# MS Word Macro (text formatting)



## flipsix3 (Jan 10, 2010)

Hi folks, hoping I can get some assistance here before my PC goes out the window! I used to use a macro that a friend created for placing coding tags into a Word document - I write large documents in Word and format them as I type in the usual way - the macro then goes through the document and converts all *bold* text to normal text with bold tags (same for italics and also creates code tags around anything in Courier New font, although this latter isn't really important)

I used to use this without problem, but following a reinstall and hardware upgrade I noticed that the macro was missing - having tried to put it back into effect I now get errors when trying to run it. 

This is the macro, but with square brackets replaced by { and } to enable me to post it into the forum:


Sub FMS_Markup()

Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Bold = False
With Selection.Find
.Text = ""
.Replacement.Text = "{b}^&{/b}"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Font.Italic = True
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Italic = False
With Selection.Find
.Text = ""
.Replacement.Text = "{i}^&{/i}"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Font.Name = "Courier New"
With Selection.Find
.Text = ""
.Replacement.Text = "{code}^&{/code}"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

End Sub 

Now, as I say, this used to work just fine but since I reapplied it (literally a copy/paste job, per last time) I now get a "Compile Error: Invalid Outside Prodecure" when I try to run it, Word takes me into the VB edit/debug screen and shows the first 'Selection' in the macro highlighted

So, either something has mysteriously changed in Word since I last used the macro (unlikely), or my brain has broken and I've 'created' the macro incorrectly? Really don't understand where it's gone wrong?

Any thoughts?


----------



## macropod (Apr 11, 2008)

Hi flipsix3,

The error suggests you've got some code lines outside the sub. Having said that, your code could be simplified. Try:

```
Sub FMS_Markup()
With Selection.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Text = ""
  .Font.Bold = True
  .Replacement.Text = "{b}^&{/b}"
  .Forward = True
  .Wrap = wdFindContinue
  .Format = True
  .MatchCase = False
  .MatchWholeWord = False
  .MatchWildcards = False
  .MatchSoundsLike = False
  .MatchAllWordForms = False
  .Execute Replace:=wdReplaceAll
  .ClearFormatting
  .Font.Italic = True
  .Replacement.Text = "{i}^&{/i}"
  .Execute Replace:=wdReplaceAll
  .ClearFormatting
  .Font.Font.Name = "Courier New"
  .Replacement.Text = "{code}^&{/code}"
  .Execute Replace:=wdReplaceAll
End With
End Sub
```


----------



## flipsix3 (Jan 10, 2010)

Thanks macropod, understand what you're suggesting - but nothing outside the sub 'tags'. 

Just to check I deleted the macro completely, and tried out your simplified version - now it works, but I'm seeing a new issue which appears to be limited to my existing (extremely large) document. 

In these exampls I've again subbed in { and } for square brackets. 


This is a random piece of text I’ve typed in Word, *{b}these words in bold{/b}*, _{i}and these in italics{/i}_. Works OK (although leaves the formatting in place as well – not a big deal, although the old macro I used didn’t do this​

Now, this is what happens when I macro the existing document I have...


Warren was ready to push for a place, but that he certainly wasn’t likely to threaten either of the *Italians {b}{/b}*any time soon. 

_“I’ll see what we can do,” _he told Warren a couple of days later. “_Rest assured you will make the first team if you want to, but I need to see a little patience from you.”
{i}{/i}_

He didn’t tell Warren that he might not have to wait too long.​

Any thoughts on what's going on there?


----------



## macropod (Apr 11, 2008)

Hi flipsix3,

For me, if I select the wole block of text, the tags are correctly placed either side of the relevant phrases.

FWIW, if you change Selection.Find to ActiveDocument.Content.Find, the whole document will be processed without the need to select anything.


----------



## flipsix3 (Jan 10, 2010)

Thanks for the input - definitely something hokey with my base document I guess - still putting the tags at the end of the formatted sections, but not on any new text I type out. Guess I'll have to live with it on this one

(Tried the switch to 'Active Document' but that does all sorts of werid stuff and cuts my 200 page doc down to about 20!)

Thanks again, macro is working fine (which is what I was after) - just need to sort the document out.



\edit: copy/pasted the whole thing into a new word doc and it's working fine now. Thanks once again.


----------



## GGalloway (Jan 14, 2010)

*Bug with Word Macros*

For some reason I can not use a Macro twice in a row. If I use it it pastes in the text and pictures. If I delete what was pasted from the macro and try to run the macro again I get an error that says hit can not find the file where the content is located.

Any help would be appreciated.

THANKS -- Gordon

Here is the macro:

ub BaseCombo()
'
' BaseCombo Macro
' Macro recorded 1/14/2010 by gallowg
'
Documents.Open FileName:="BaseCombo.doc", ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Copy
ActiveDocument.Close
Selection.PasteAndFormat (wdPasteDefault)
End Sub


----------



## macropod (Apr 11, 2008)

Hi Gordon,

You should start a new thread instead of piggybacking onto an existing one.


----------

