1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: VBA-Word 2003:Select picks up extra para markers

Discussion in 'Business Applications' started by MysFiddler, Dec 16, 2011.

Thread Status:
Not open for further replies.
  1. MysFiddler

    MysFiddler Thread Starter

    Joined:
    Dec 16, 2011
    Messages:
    2
    I'm not totally new to VBA (in fact I'm so old I'm out-of-date on many things), but I am new to using it in Word and the Word object model. I'm embarrassed to say that I've spent days on this problem and have not yet wrapped my wits around how to work with the Range and selection objects.

    I'm looping to each paragraph and testing to see if the word "chapter" is contained in the first line. If it is, I intend to do the following:
    1) select the entire line (containing a chapter title if present)
    1a) test to ensure the word "chapter" is not in a table nor in a string in the first sentence
    2) reset the style if valid and
    3) if there is no chapter title, replace the selection with a number using Word builtin styles that I have modified. (this is not yet coded entirely)

    However, after getting the new para, when I tell the range to select the first sentence, it also picks up any extra paragraph markers that might be below the sentence. Then, when I set my format, it also formats these markers and/or adds numbers. I have not been able to figure out how to make word only pick up the first sentence and not ensuing blank paragraph markers as well. When I do anything to remove the markers from the selection, it literally removes the markers from my document. It seems like this should be simple and I'm probably just missing a builtin function or something. I also don't know a clean way to strip extra ASCII characters that show up as little boxes from my string.

    Here's my test code:

    Code:
            For Each oPar In ActiveDocument.Paragraphs
           i = oPar.Range.Words.Count
                oPar.Range.Sentences(1).Expand (wdSentence)
                oPar.Range.Sentences(1).Select
                Set newR = oPar.Range.Sentences(1)
                'above it selects two or more graf markers if exist
    
                curSent = Selection.Text
    'this is messy  -- trying to get extra chars out of code          
                curSent = Trim(newR.Text)
                curSent = Application.CleanString(curSent)
                curSent = Replace(curSent, Chr(13), "", 1, Len(curSent), vbTextCompare)
                curSent = Trim(Replace(curSent, Chr(10), "", 1, Len(curSent), vbTextCompare))
                curSent = CStr(Trim(curSent))
                
              
                'need to test for if "chapter" is first word (rather than a common word contained in the first sentence of the graf)
                If InStr(1, curSent, "chapter", vbTextCompare) = 1 _
                    And Not IsNull(curSent) And Not curSent = "" _
                    And Not IsEmpty(newR) And Len(curSent) >= 7 Then
                
                        If Not newR.Information(wdWithInTable) Then
                        'eliminate end paragraphs
                        
                                With newR.Find
                                .ClearFormatting
                                
                                .Text = curSent
                                '.Replacement.Text = "chapter 1"  -- testing, but replacement text doesn't work either
                                .Execute
                                .Style = ActiveDocument.Styles(curStyle)
                                
                                End With
                                
                        'set style
                        newR.Sentences.Item(1).Style = ActiveDocument.Styles(curStyle)
                        End If
     
                 End If
                
                    Next
    
    
    
    
    Thank you for any help you can give me.
     
  2. MysFiddler

    MysFiddler Thread Starter

    Joined:
    Dec 16, 2011
    Messages:
    2
    I finally worked it out with a workaround. Probably would have saved time to study more.

    I created a function to determine how many extra paragraph markers Word was picking up during the "select" process, then added utilized some Word devices.
    Code:
    Public Function fCountParaMarks(sSelText) As Integer
    
    'use with fSetChapterHeads
    Dim i As Integer
    fCountParaMarks = 0
    'For Each str In sSelText 'Selection.Text
        Do Until Right(sSelText, 1) <> Chr(13)
        If Right(sSelText, 1) = Chr(13) Then
            'it's a marker, so count it
            i = i - 1
        Else
        'exit
        End If
        sSelText = Left(sSelText, Len(sSelText) - 1)
        
    Loop
    i = i + 1
    fCountParaMarks = i
    End Function
    
    I added these things to my code:
    Code:
     For Each oPar In ActiveDocument.Paragraphs
           i = oPar.Range.Words.Count
                oPar.Range.Sentences(1).Expand (wdSentence)
    'select the sentence which captures extra empty paragraph markers
                oPar.Range.Sentences(1).Select
    'set the variable sentence
                curSent = Selection.Text
    'run the function to find where the end of the sentence should end
                oPar.Range.MoveEnd Unit:=wdCharacter, Count:=fCountParaMarks(curSent)
    'reselect the new range
                oPar.Range.Select
    'see the new sentence without the added paragraph markers
                curSent = Selection.Text
    
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1031519

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice