Macro Syntax

  • Thread starter Thread starter GregR
  • Start date Start date
G

GregR

I am looking for the macro syntax to do the following: I am pasting a
recipe from the web to Word. After pasting I want to select the whole
document and format it to my style of "Recipe Format". Then select any
line that has a colon at the end and format this line to my style
"Recipe SubHead", and the remove the colon. Then move to the beginning
of the document and format this line to my style "Recipe Header".
Finally replace any fractions with the superscript/subscript format. TIA
 
Greg,

You aren't asking for much are you :-)

First, text pasted from the web can be full of nasty formatting that
should first be cleaned up. See:

http://gregmaxey.mvps.org/Clean_Up_Text.htm

Second, the superscript fractions that Word automatically generates for
1/3, 1/2, 3/4 etc. are a special and limited set. However you can get
a close approximation with a macro provided by fellow MVP Graham Mayor.

After you get your text cleaned up, give this a try. Change the style
names to yours.

Sub Test()
Dim myRange As Range
Dim oPara As Paragraph


Set myRange = ActiveDocument.Range
myRange.Style = "Heading 1"
For Each oPara In ActiveDocument.Paragraphs
If oPara.Range.Characters.Last.Previous = ":" Then
oPara.Style = "Heading 2"
oPara.Range.Characters.Last.Previous.Delete
End If
Next
myRange.Paragraphs(1).Style = "Title"
With myRange.Find
.Text = "[0-9]{1,}/[0-9]{1,}"
.MatchWildcards = True
While .Execute
FmtFraction myRange
myRange.Collapse Direction:=wdCollapseEnd
Wend
End With
End Sub
Sub FmtFraction(myRange As Range)
Dim OrigFrac As String
Dim Numerator As String, Denominator As String
Dim NewSlashChar As String
Dim SlashPos As Integer

NewSlashChar = ChrW(&H2044)
OrigFrac = myRange
SlashPos = InStr(OrigFrac, "/")
Numerator = Left(OrigFrac, SlashPos - 1)
Denominator = Right(OrigFrac, Len(OrigFrac) - SlashPos)
myRange.Select
Selection.Font.Superscript = True
Selection.TypeText Text:=Numerator
Selection.Font.Superscript = False
Selection.TypeText Text:=NewSlashChar
Selection.Font.Subscript = True
Selection.TypeText Text:=Denominator
Selection.Font.Subscript = False
End Sub
 
Back
Top