View Full Version : Word Text parsing in VBA
1with49
10-06-2005, 05:52 AM
Does anybody know how to scroll through every word in a word document and perform an operation on that word depending on a certain condition, I think the following code is along the lines I need but I cant quite get it to work.
Dim snt As Word.Range
For Each ActiveDocument.Words In snt
If (Left(snt.Text, Len("<<BOLD>>")) = "<<BOLD>>") And _
(Right(snt.Text, Len("<</BOLD>>")) = "<</BOLD>>") Then
If InStr(snt.Text, "<<BOLD>>") And InStr(snt.Text, "<</BOLD>>") Then
snt.bold
snt = Mid(snt, Len("<<BOLD>>") + 1, Len(snt) - (Len("<<BOLD>>") _
+ Len("<</BOLD>>") + 1))
End If
Next snt
basically where ever I find <<bold>>WORD<</bold>> as a whole word I want it to go bold and remove the bold tags, the code for performing the removal works in VB 6.0 so I know the logic part is correct.
Its just the looping through the words in the document that doesnt work, help anybody?
TonyJollans
10-06-2005, 06:54 AM
Hi 1with49,
Welcome to VBAX!
Firstly, to correct your code:
Your For Each statement is back to front, it should beFor Each snt in ActiveDocument.WordsYou can't just say snt.bold by itself, you must saysnt.Bold = TrueFinally you are missing an End If.
Now, to make your life easier, you should be able to do this with a Find and Replace.
In the Find/Replace Dialog (Ctrl+h)
Check "Use Wildcards"
Find \<\<BOLD\>\>(*)\<\</BOLD\>\>
Replace with \1
Set Formatting for Replace by putting the cursor in the replace with box and clicking on Format, Selecting Font and setting it to Bold in the Dialog.
(I hope I have typed that in correctly) - the less than and greater than symbols want preceding with backslashes so that they are interpreted as the characters themselves, and the asterisk wants surrounding in parentheses so identify it as a separate term (the first or only in this case) which can be used in the replace.
1with49
10-06-2005, 07:03 AM
Thanx! I needed to do it in VBA on document open because its part a document generation application iam writing. I did solve this problem in the end with the following code, but what you suggested does look neater!
Dim sntcount As Integer
Dim sntCounter As Integer
sntCounter = 1
sntcount = ActiveDocument.Sentences.Count
Do While sntCounter <= sntcount
If InStr(ActiveDocument.Sentences(sntCounter), "<<BOLD>>") And _
InStr(ActiveDocument.Sentences(sntCounter), "<</BOLD>>") Then
ActiveDocument.Sentences(sntCounter).Bold = True
ActiveDocument.Sentences(sntCounter) = _
Mid(ActiveDocument.Sentences(sntCounter), Len("<<BOLD>>") + 1, _
Len(ActiveDocument.Sentences(sntCounter)) - (Len("<<BOLD>>") + _
Len("<</BOLD>>")) - 2)
End If
If InStr(ActiveDocument.Sentences(sntCounter), "<<ITALICS>>") And _
InStr(ActiveDocument.Sentences(sntCounter), "<</ITALICS>>") Then
ActiveDocument.Sentences(sntCounter).Italic = True
ActiveDocument.Sentences(sntCounter) = _
Mid(ActiveDocument.Sentences(sntCounter), Len("<<ITALICS>>") + 1, _
Len(ActiveDocument.Sentences(sntCounter)) - _
(Len("<<ITALICS>>") + Len("<</ITALICS>>")) - 2)
End If
If InStr(ActiveDocument.Sentences(sntCounter), "<<UL>>") And _
InStr(ActiveDocument.Sentences(sntCounter), "<</UL>>") Then
ActiveDocument.Sentences(sntCounter).Underline = True
ActiveDocument.Sentences(sntCounter) = _
Mid(ActiveDocument.Sentences(sntCounter), Len("<<UL>>") + 1, _
Len(ActiveDocument.Sentences(sntCounter)) - (Len("<<UL>>") + _
Len("<</UL>>")) - 2)
End If
sntCounter = sntCounter + 1
Loop
This is obviously quite messy so I will change it now ive had some guidance, but on another issue I also need to do right,left and center justified lines in VBA along the same principle of using tags IE <<L>> <<R>> and <<J>>.
Is it possible to justify text in VBA? thanks again for your help and time
fumei
10-06-2005, 07:06 AM
Here is an alternative.
Sub ChangeBold()
Selection.HomeKey unit:=wdStory
With Selection.Find
Do While (.Execute(findtext:="<<bold>>", Forward:=True) _
= True) = True
With Selection
.MoveEndUntil cset:="/"
.MoveEnd unit:=wdCharacter, Count:=7
.Text = Left((Right(Selection.Text, _
Len(Selection.Text) - 8)), _
Len(Selection.Text) - 17)
.Font.Bold = True
.Collapse Direction:=wdCollapseEnd
End With
Loop
End With
End Sub
This changes:
<<bold>>WORDHere<</bold>>
<<bold>>WORDthere<</bold>>
<<bold>>WORDsomethijg<</bold>>
<<bold>>WORDone<</bold>> <<bold>>WORDtwo<</bold>>
into:
WORDHere
WORDthere
WORDsomethijg
WORDone
WORDtwo
I hard coded the <<BOLD>> and <</BOLD>> lengths, on the assumption the tags are generated. Therefore they will always be the same length. It is never going to be < < BOLD > >. It will always be <<BOLD>> - that is, the same length all the time.
This could of course be changed, similar to your code.
JohnnyBravo
10-09-2005, 11:25 PM
This thread in interesting. So using the above example, how would you get VBA to examine only the first paragraph of a letter and if it contains a key word, (say "blue" for example) paste certain paragraphs at specified places throughout the body of the document? And if contains "Red", paste different lines of text throughout the letter?
TonyJollans
10-10-2005, 03:11 AM
Hi JohnnyBravo,
Welcome to VBAX!
Your question is really rather different. You can limit a Find to any Range in a document. What you request after that requires you to identify the places in the document (probably with bookmarks) and somehow identify the text to be inserted.
If you want more information, please start a new thread with more details and I, or someone else, will try to help.
fumei
10-10-2005, 08:09 PM
Good point Tony. JohnnyBravo, please DO start a thread on this if you like. It is a good subject. The answer is: absolutely yes, VBA can do that....but it belongs in its own thread.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.