Results 1 to 5 of 5
  1. #1
    Lounger
    Join Date
    Apr 2005
    Location
    Silver Spring, Maryland, USA
    Posts
    36
    Thanks
    0
    Thanked 1 Time in 1 Post

    Extending selection in Word (VBA, Word 2003)

    I'm working on a macro to convert text chunks into tables. The text chunks look something like this:

    Subject: EHS_Remote

    Description: The remote should indicate to the user if the batteries are running low.


    Build Number: Beta 2


    Steps:


    I need to convert everything from Subject to the end of the Build Number line into a table and then do some fancy formatting. There may be several lines of description and a few blank paragraphs, so I can’t just count lines and move down.

    Which means I need to find, then select, everything from Subject: to the end of the line that starts with Build Number. I need to leave the Steps: line alone.

    Finding “subject:” is no problem, of course, and I figured out how to check whether it is already in a table.

    But selecting the rest of the relevant stuff is a bear. I've kludged together something using Selection.MoveEnd and Selection.MoveUntil. Can anyone point me to a better way?

    Many thanks,

    Jessica Weissman

    P.S. Here is the kludge, which I execute once "Select:" is selected. There has to be a better way.

    If Selection.Information(wdWithInTable) = False Then
    Selection.MoveEndUntil cset:=":", Count:=wdForward
    Selection.MoveEnd unit:=wdLine, Count:=1
    Selection.MoveEndUntil cset:=":", Count:=wdForward
    Selection.MoveEnd unit:=wdLine, Count:=1
    End If

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Extending selection in Word (VBA, Word 2003)

    I'd use the Find object: first search for "Subject:", store the start position in a variable, then search for "Build Number:", extend to the end of the line or paragraph, and store the end position in another variable.
    You can now work with the range from the start position to the end position.

  3. #3
    Lounger
    Join Date
    Apr 2005
    Location
    Silver Spring, Maryland, USA
    Posts
    36
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Extending selection in Word (VBA, Word 2003)

    Thanks - of course this makes sense, but I am not sure what format the start and end positions get stored in. The Word object model is so convoluted.

    Can you point me to an example of how the end position could be stored? It's probably obvious to everybody but me.

    - Jessica, still a novice.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Extending selection in Word (VBA, Word 2003)

    The Start and End properties of a range are numbers (long integers). They range from 0 to the total number of characters (including spaces, tabs, paragraph breaks etc.)
    Here is an example (to be modified, of course):
    <code>
    Dim lngStart As Long
    Dim lngEnd As Long
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    Do
    .Text = "Subject:"
    If .Execute Then
    lngStart = Selection.Start
    Selection.Collapse Direction:=wdCollapseEnd
    .Text = "Build Number:"
    If .Execute Then
    Selection.EndKey Unit:=wdLine
    lngEnd = Selection.End
    Selection.Collapse Direction:=wdCollapseEnd
    ActiveDocument.Range(lngStart, lngEnd).ConvertToTable
    Else
    Exit Do
    End If
    Else
    Exit Do
    End If
    Loop
    End With</code>

  5. #5
    Lounger
    Join Date
    Apr 2005
    Location
    Silver Spring, Maryland, USA
    Posts
    36
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Extending selection in Word (VBA, Word 2003)

    Got it - it never occurred to me that Word would relate the selection to the position within the long string of characters that makes up each story. Obvious in retrospect of course.

    Thanks yet again.

    And my author, the test team, thank you too although they don't know it.

    - Jessica

Posting Permissions

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