Ooh yes, Larry! -- that would be valuable for another context I have been
mulling over.
Just post it here, please.
Okay, Clive. You asked for it. It's not the prettiest code but it gets
the job done. In looking at it I realise that I haven't bothered with
smallcaps or allcaps -- you could add those easily enough. Also, at
the comment "Make stuff look bold, italic, etc. once again", you may
not want to do that; that is, you might want to just leave off the
character attributes of text once it's been tagged. Finally, it's set
to operate on the whole document; again, it's an easy adjustment to
make it work on the current selection or range.
~~~~~~~~~~~~~~~~~~~~~~~~~~~ L O N G ~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub TagEmphasisedText()
'
' Turn off change-tracking
ActiveDocument.AcceptAllRevisions
With ActiveDocument
.TrackRevisions = False
.PrintRevisions = False
.ShowRevisions = False
End With
Selection.HomeKey Unit:=wdStory
' Remove any formatting from tab characters
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Superscript = False
.Subscript = False
End With
With Selection.Find
.text = "^t"
.Replacement.text = "^t"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Clean up XML-invalid characters and markers
' Remove white-space at end of paragraph.
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = ""
.Replacement.text = "^&"
.Replacement.Font.Bold = False
.Replacement.Font.Italic = False
.Replacement.Font.Underline = wdUnderlineNone
.Replacement.Font.Superscript = False
.Replacement.Font.Subscript = False
.Forward = True
.Wrap = wdFindContinue
.Format = True
.Font.Name = "Symbol"
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.text = "[ ^t]@^13"
.Replacement.text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = " @^13"
.Replacement.text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
' Pre-prep baseline shifts masquerading as subscripts and
superscripts
Selection.Find.ClearFormatting
Selection.Find.Font.Position = -3
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
.Superscript = False
.Subscript = True
End With
With Selection.Find
.text = ""
.Replacement.text = ""
.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.Position = 3
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
.Superscript = True
.Subscript = False
End With
With Selection.Find
.text = ""
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Clean out any existing <b>, <i>, etc.
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<b\>(*)\</b\>"
.Replacement.text = "\1"
.Replacement.Font.Bold = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<i\>(*)\</i\>"
.Replacement.text = "\1"
.Replacement.Font.Italic = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<u\>(*)\</u\>"
.Replacement.text = "\1"
.Replacement.Font.Underline = wdUnderlineSingle
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<s\>(*)\</s\>"
.Replacement.text = "\1"
.Replacement.Font.Superscript = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<sup\>(*)\</sup\>"
.Replacement.text = "\1"
.Replacement.Font.Superscript = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<sub\>(*)\</sub\>"
.Replacement.text = "\1"
.Replacement.Font.Subscript = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Clear Word attributes on all tags
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<[!>]@\>"
.Replacement.text = "^&"
.Replacement.Font.Bold = False
.Replacement.Font.Italic = False
.Replacement.Font.StrikeThrough = False
.Replacement.Font.DoubleStrikeThrough = False
.Replacement.Font.Underline = wdUnderlineNone
.Replacement.Font.Outline = False
.Replacement.Font.Shadow = False
.Replacement.Font.Hidden = False
.Replacement.Font.SmallCaps = False
.Replacement.Font.AllCaps = False
.Replacement.Font.Superscript = False
.Replacement.Font.Subscript = False
.Replacement.Font.Position = 0
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Apply tags based on attributes
' Set up general search attributes
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
' Bold
' Insert new tags
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "[!^13\<\>]"
.Replacement.text = "‹b›^&‹/b›"
.Replacement.Font.Bold = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Remove extra tags
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹/b›‹b›"
.Replacement.text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹/b›[^13^t^s^l ]@‹b›"
.Replacement.text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹b›[^13^t^s^l ]@‹/b›"
.Replacement.text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Italic
' Insert new tags
Selection.Find.ClearFormatting
Selection.Find.Font.Italic = True
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "[!^13\<\>]"
.Replacement.text = "‹i›^&‹/i›"
.Replacement.Font.Italic = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Remove extra tags
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹/i›‹i›"
.Replacement.text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹/i›[^13^t^s^l ]@‹i›"
.Replacement.text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹i›[^13^t^s^l ]@‹/i›"
.Replacement.text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Superscript
' Insert new tags
Selection.Find.ClearFormatting
Selection.Find.Font.Superscript = True
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "[!^13\<\>]"
.Replacement.text = "‹sup›^&‹/sup›"
.Replacement.Font.Superscript = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Remove extra tags
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹/sup›‹sup›"
.Replacement.text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹/sup›[^13^t^s^l ]@‹sup›"
.Replacement.text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹sup›[^13^t^s^l ]@‹/sup›"
.Replacement.text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Subscript
' Insert new tags
Selection.Find.ClearFormatting
Selection.Find.Font.Subscript = True
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "[!^13\<\>]"
.Replacement.text = "‹sub›^&‹/sub›"
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Remove extra tags
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹/sub›‹sub›"
.Replacement.text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹/sub›[^13^t^s^l ]@‹sub›"
.Replacement.text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹sub›[^13^t^s^l ]@‹/sub›"
.Replacement.text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Underscore
' Insert new tags
Selection.Find.ClearFormatting
Selection.Find.Font.Underline = wdUnderlineSingle
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "[!^13\<\>]"
.Replacement.text = "‹u›^&‹/u›"
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Remove extra tags
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹/u›‹u›"
.Replacement.text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹/u›[^13^t^s^l ]@‹u›"
.Replacement.text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹u›[^13^t^s^l ]@‹/u›"
.Replacement.text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Strikethrough
' With Selection.Find.Replacement.Font
' .StrikeThrough = True
' .DoubleStrikeThrough = False
' End With
' Insert new tags
Selection.Find.ClearFormatting
Selection.Find.Font.StrikeThrough = True
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "[!^13\<\>]"
.Replacement.text = "‹s›^&‹/s›"
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Remove extra tags
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹/s›‹s›"
.Replacement.text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹/s›[^13^t^s^l ]@‹s›"
.Replacement.text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹s›[^13^t^s^l ]@‹/s›"
.Replacement.text = " "
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Make stuff look bold, italic, etc. once again.
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "‹"
.Replacement.text = "<"
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "›"
.Replacement.text = ">"
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.MatchWildcards = True
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<b\>*\</b\>"
.Replacement.text = "^&"
.Replacement.Font.Bold = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<i\>*\</i\>"
.Replacement.text = "^&"
.Replacement.Font.Italic = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<u\>*\</u\>"
.Replacement.text = "^&"
.Replacement.Font.Underline = wdUnderlineSingle
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<s\>*\</s\>"
.Replacement.text = "^&"
.Replacement.Font.StrikeThrough = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<sup\>*\</sup\>"
.Replacement.text = "^&"
.Replacement.Font.Superscript = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<sub\>*\</sub\>"
.Replacement.text = "^&"
.Replacement.Font.Subscript = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Restore search attributes
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
End With
MsgBox "Converted character attributes to XML tags." & vbLf &
"Converted non-XML index markers to XML format.", , "Finished
TagEmphasizedText!"
dbug:
End Sub
Sub UntagEmphasisedText()
'''''''''''''''''''''
Dim theFile As String
Dim myDoc As Document
Dim theName As String
Dim theExtH As String
Dim theExtD As String
Dim thePath As String
Dim myCode As String
Dim theRandom As Long
Dim theSynchFile As String
Dim theSynch As String
''''''''''''''''''''''
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<b\>(*)\</b\>"
.Replacement.text = "\1"
.Replacement.Font.Bold = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<i\>(*)\</i\>"
.Replacement.text = "\1"
.Replacement.Font.Italic = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<u\>(*)\</u\>"
.Replacement.text = "\1"
.Replacement.Font.Underline = wdUnderlineSingle
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<s\>(*)\</s\>"
.Replacement.text = "\1"
.Replacement.Font.StrikeThrough = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<sup\>(*)\</sup\>"
.Replacement.text = "\1"
.Replacement.Font.Superscript = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "\<sub\>(*)\</sub\>"
.Replacement.text = "\1"
.Replacement.Font.Subscript = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
'On Error Resume Next
bye:
MsgBox "Reverted to local formatting.", , "Finished
UntagEmphasisedText!"
End Sub