PDA

View Full Version : Solved: Scan through document and change font



suji
11-18-2008, 09:46 PM
Greetings,

I am newbie here and dont know much about VBA. Heres what I would like you guys to help me out with.

Scan through a word 2003 document and change font of full line if the first four characters are "numerals followed by a space".

Appreciate all help.

Thanks

GTO
11-19-2008, 02:08 AM
Greetings suji,

Rather than having to recreate examples, could you post an example?

Thanks,

Mark

suji
11-19-2008, 06:23 AM
I have attached an example document. I need to change the font of all the lines not starting with the numbers ie 001, 002,003 etc.

fumei
11-19-2008, 01:26 PM
Better to use proper Styles. Demo attached. Click "Change Me" on the top toolbar. I made it toggle.

Click it, and the paragraphs starting with numeric, AND have the fourth character a space, are changed to the "Changed" style I put in the document.

Click it again, they are changed back to "Normal".

Notice the paragraph:

12345689 1234679

does NOT get changed, as it does not meet the logic.

The first four characters starting with a number AND having the last (fourth) character a space.


Here is the code. Note that there are a number of other possible routes to doing this.
Option Explicit

Sub ChangeMe()
Dim oPara As Paragraph
Dim myCheck
Dim TestString As String
Dim yadda As Boolean

For Each oPara In ActiveDocument.Paragraphs
' just to demo toggle
If oPara.Range.Style = "Changed" Then
oPara.Range.Style = "Normal"
yadda = False
Else
yadda = True
End If
' get first four characters
TestString = Left(oPara.Range.Text, 4)

If yadda = True Then ' all styles are Normal
' check if FIRST character is numeric
' and LAST character is a space
' otherwise who cares?
If myCheck = IsNumeric(Left(TestString, 1)) _
= False And _
Right(TestString, 1) = " " Then
oPara.Range.Style = "Changed"
End If
End If
Next
End Sub

suji
11-20-2008, 06:37 AM
Thanks a ton fumei. Thats exactly what I wanted.

lucas
11-20-2008, 09:48 AM
Be sure to mark your thread solved using the thread tools at the top of the page.