Consulting

Results 1 to 7 of 7

Thread: Word Text parsing in VBA

  1. #1
    VBAX Regular
    Joined
    Oct 2005
    Posts
    15
    Location

    Word Text parsing in VBA

    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.

    [VBA] 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
    [/VBA]
    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?

  2. #2
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi 1with49,

    Welcome to VBAX!

    Firstly, to correct your code:

    Your For Each statement is back to front, it should be[vba]For Each snt in ActiveDocument.Words[/vba]You can't just say snt.bold by itself, you must say[vba]snt.Bold = True[/vba]Finally 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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  3. #3
    VBAX Regular
    Joined
    Oct 2005
    Posts
    15
    Location

    Thanx!

    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!

    [VBA]
    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

    [/VBA]


    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
    Last edited by geekgirlau; 10-10-2005 at 02:49 AM. Reason: Put code in VBA tags

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Here is an alternative.
    [vba]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[/vba]

    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.

  5. #5
    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?

  6. #6
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •