How do I set a range for Word in vba for a specific set of characters?

RaifMT

I am finding a starting and ending character within a Word paragraph, but for the life of me, cannot determine how to set a range that includes those characters. Here is an excerpt of what I have:

Set rngTemp = w.Paragraphs(i).Range
For iCounter = 1 To rngTemp.Characters.Count
    If rngTemp.Characters(iCounter).HighlightColorIndex = wdYellow Then
        lStartPos = iCounter
        While rngTemp.Characters(iCounter).HighlightColorIndex = wdYellow
            iCounter = iCounter + 1
        Wend
        lEndPos = iCounter

Using the values for the starting and ending characters, how do I set a range that contains that string? I have made a number of attempts, but all unsuccessful.

The instruction on Microsoft's "Working with Ranges" page have the following code:

Set rngDoc = ActiveDocument.Range(Start:=0, End:=10)

I receive no errors in the first line of my sample ("Set rngTemp ..."), which seems to be very comparable to Microsoft's sample. But when I try:

Set r = w.Paragraphs(i).Range(Start:=lStartPos, End:=lEndPos)

I get the "Compile Error: Wrong number of arguments or invalid property assignment"

RaifMT

I thought I had found the answer with this code:

 Set r = w.Range(w.Paragraphs(i).Range.Characters(lStartPos).Start, _
          w.Paragraphs(i).Range.Characters(lEndPos).End)

but it still produced an error. In the immediate window, I pasted the code from each line and the numbers showed up for each. Then I changed the set statement to use the actual numbers, and that worked. For some reason, VBA does not like to allow the numbers from the code immediately above to work in setting the range variable. I changed the original code (from the top) to this:

Set rngTemp = w.Paragraphs(i).Range

        For iCounter = 1 To rngTemp.Characters.Count
            If rngTemp.Characters(iCounter).HighlightColorIndex = wdYellow Then

                'lStartPos = iCounter
                lStartPos = w.Paragraphs(i).Range.Characters(iCounter).Start

                While rngTemp.Characters(iCounter).HighlightColorIndex = wdYellow
                    iCounter = iCounter + 1
                Wend

                'lEndPos = iCounter
                lEndPos = w.Paragraphs(i).Range.Characters(iCounter).End

                Set r = w.Range(lStartPos, lEndPos)

and it worked.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I create a pattern to only match a specific set of characters?

Set specific font size for Hebrew characters in Word in VBA

How do I retrieve a set number of characters that precede and follow a key word?

How do I set font for text but keep font in equations in Word with VBA?

How do I make my colour bar for Cartopy have a specific range set by me?

How do I set the range selector to go back to a specific time in plotly?

How do I check whether the first character of each string in a list starts with a specific set of characters or not in python?

How do I resolve specific invalid characters for Khmer character set in Strings.xml?

How do I change or set a specific author name for my comments on Microsoft Word 2016

How do I get a range of a specific word for ITextDocument

How do I change this statically set range to a dynamic range?

How do I set timezone on a specific date?

How to set a specific range for a variable

How do I set Word 2010 as the default program for word documents?

How can I set a variable range to a table in excel VBA 2007?

Excel vba is changing the Range I set

How do I set a limit to the range of my bullet?

How do I set a range for a combo box showing a span of years

How do I set range in forloop in Django template tags?

How do I set the IP range for Scapy Sniff filter?

How do I set a limit on the buttons for Plus / Minus / Range value?

How do I set limits for my range function in python?

How do I set the range to change automatically in Google Sheets API?

How do I count the number of occurrences of a set of characters?

How do I remove as set of characters from a string in TypeScript

How do I set a bullet style as default in Microsoft Word 2010?

How to set a specific range for different characters inside in a bracket that inside in a capture and group regex

Set selected text as string and search for string in a range, VBA Word

How to set and use empty range in VBA?