Reformatting ascii text files....

  • Thread starter Thread starter cgg
  • Start date Start date
C

cgg

I have a plain ascii text file that indicates type face for example as
follows...

words enclosed in +pluses+ represent boldface
words enclosed in /slashes/ represent underlined words
words enclosed in ~tildes~ represent a wavy underline

How do I search and replace words with the appropriate type face throughout the
document?

Colin
 
Hi, Colin,

Use the Replace dialog. Click the More button and check the box for
"Use wildcards". In the Find What box, type an expression like

+(*)+

In the Replace With box, type the expression

\1

then click the Format button, select Font, select Bold and click OK in
the dialog, and then click the Replace All button. Repeat with the
other symbols and their formatting.

See http://word.mvps.org/FAQs/General/UsingWildcards.htm for an
explanation of the expressions above.
 
I would suggest that you write a VBA (Visual Basic for
Applications) macro to do this. If you don't have any
VBA experience, try this:

Make a backup copy of your Normal.dot file.

Select "Tools / Macro / Visual Basic Editor" (or the
equivalent in your version of Word).

Select the "Normal" or "Normal.dot" project in the
Project pane on the left.

Select the command "Insert / New Module"

Paste the macro below into the new module (that is, all
of the text afer "**** START OF MACRO ****")

Now go back to Word, and open the document that you want
to reformat. Select "Tools / Macro / Macros". Select
the "FormatText" macro, and push the Run button.

- David M. Gauntt

**** START OF MACRO ****
Option Explicit

Public Sub FormatText()
Dim POS As Long
Dim myRange As Range

Set myRange = ActiveDocument.Range(0, 0)

POS = 0
While FindText(POS, myRange, "+")
myRange.Bold = True
Wend

POS = 0
While FindText(POS, myRange, "/")
myRange.Underline = True
Wend

POS = 0
While FindText(POS, myRange, "~")
myRange.Italic = True
Wend

End Sub

Private Function FindText(POS As Long, myRange As Range,
delimiter As String) As Boolean
Dim startBold, startUnderline, startItalic As Long
Dim startPos As Long, endPos As Long

FindText = False

startPos = FindString(POS, delimiter)
If startPos < 0 Then
Exit Function
End If

endPos = FindString(startPos + 1, delimiter)
If endPos < 0 Then
Exit Function
End If

' Delete the end delimiter
myRange.start = endPos
myRange.End = endPos + 1
myRange.Delete

' Delete the start delimiter
myRange.start = startPos
myRange.End = startPos + 1
myRange.Delete
endPos = endPos - 1

myRange.start = startPos
myRange.End = endPos
myRange.Select

FindText = True

End Function

Private Function FindString(ByRef startPos As Long,
theString As String) As Long
Dim myRange As Range

Set myRange = ActiveDocument.Range(startPos, startPos)

With myRange.Find
.Forward = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = False
.Wrap = wdFindContinue
If Not .Execute(FindText:=theString) Then
FindString = -1
Else
FindString = myRange.start
myRange.Select
End If
End With
End Function
 
Back
Top