none
Indexes in Microsoft Office Word vba RRS feed

  • Question

  •  

    I am working a on a macro, and I need to convert an index for a word range to an index of a character range. Is there an easy way of getting the number of characters that a word is from the beginning of the document from the number of words that a word has in front of it?

    Thanks.

    Friday, July 19, 2013 12:14 PM

All replies

  • I'm not sure I fully understand your question but I will tell you that the value returned by the "Selection.Start" VBA command is the character position of the selection point based on a starting address of 0, which is the first character position in the story level string.

    I used the term "story level" and what that refers to is the various layers of a Word document of which there are about a dozen if I recall correctly. The main body layer is usually what most people think about.

    Thus if the first character in your document is the letter A, then the starting address of the letter is 0 and the ending address is 1. If a blank space follows the letter A, then its starting address is 1 and its ending address is 2.

    Hope this helps


    Kind Regards, Rich ... http://greatcirclelearning.com

    Friday, July 19, 2013 1:16 PM
  • I'm not really sure what it is you need, but if you want the number of characters that precede a particular character in the document, I'd do something like:

    Dim nrChars as Integer
    Dim rngChar as Word.Range
    Set rngChar = Selection.Characters(1)
    rngChar.MoveStart wdStory, -1
    nrChars = rngChar.Characters.Count - 1


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 19, 2013 1:19 PM
    Moderator
  • I understand how indexes work. What I want to know is can I translate the index of a word to the index of the first character of the word.

    Example:

    The cat jumped over the purple dog.

    in the Word Range "jumped" is 3 i we don't count spaces.

    Is there a simple programmatic way of translating 3 to 9. 9 being the Character Range index of the first letter of "jumped".

    Thanks.

    Friday, July 19, 2013 7:34 PM
  • See if the following gives you what you are looking for:

    Sub WordRange()
        Dim doc As Word.Document
        Dim rng As Word.Range
        
        Set doc = ActiveDocument
        Set rng = doc.Content
        rng.SetRange 0, Selection.Start
        Debug.Print rng.ComputeStatistics(wdStatisticWords)
        Selection.StartOf unit:=Word.WdUnits.wdWord
        Debug.Print Selection.Start
    End Sub
    
    Word statistics defines the range of a word to include the following space or punctuation mark. Thus depending on where you place the insertion point you may have to adjust how many words there are in the document up to the current insertion point. Play with it bit and you'll see what I mean.


    Kind Regards, Rich ... http://greatcirclelearning.com

    Friday, July 19, 2013 8:45 PM
  • Hi SonOfSharePoint

    I assume you weren't answer me in your response "I understand how indexes work. " If you try the code I posted I believe you'll find it does what you request...


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, July 20, 2013 10:32 AM
    Moderator
  • This is close to the answer. Thank you.

    However, my macro goes through the document automatically. I need a way to set rngChar to the current Word in the loop for that goes through each word in the document.

    Monday, July 29, 2013 4:24 PM
  • I'm trying to do this, but to no avail. Wd is the word for each iteration of the for loop

      Set rngChar = wd.Characters
                        rngChar.MoveStart wdStory, -1
                        nrChars = rngChar.Characters.Count - 1

    Monday, July 29, 2013 5:34 PM
  • I also tried to use Information to calculate the number of character til the word. Didn't work. I would get -1 after the first word was changed and onto the second in the document.
    Monday, July 29, 2013 5:35 PM
  • Hi SonOfSharepoint

    I'm not sure I'm understanding exactly what you need, but...

    the Word object is, itself, a Range object. So if you have something like

    For each wd in Doc.Content.Words
      'Do something
      rngChar = wd.Duplicate
    Next

    Does that help?


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, August 1, 2013 4:40 PM
    Moderator
  • My answer was to make a selection out of the word I was counting towards. Then I started a count from the beginning of the document and counted the characters to the selection.
    Tuesday, August 6, 2013 5:01 PM