Results 1 to 7 of 7
  1. #1
    New Lounger
    Join Date
    Mar 2008
    Location
    Frederick, Maryland, USA
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    HELP - Problem with Simple Word Task (Ms Word 2003)

    I have spent an good deal of time trying to get the following code to select each item in a word table cell, but no luck yet. Would someone please take a look at this and hopefully provide insight? I have attached a word document that explains the detail of what I am trying to do.

    Public Sub EstablishVol1Bookmarks(strDocName)
    Dim objApp As Word.Application, wdoc As Word.Document, rng As Range, mySentenceCount As Integer, i As Integer, _
    myString1 As Variant, myString2 As Variant, myLastString As String
    Set objApp = CreateObject("Word.Application")
    Set wdoc = objApp.Documents.Open(CurrentProject.Path & "" & strDocName) 'Forms!frmMain!cboChapter
    wdoc.Activate
    objApp.Visible = True
    Set rng = ActiveDocument.Tables(3).Rows(9).Cells(8).Range
    mySentenceCount = rng.Sentences.Count
    For i = 1 To mySentenceCount

    rng.Sentences(i).Select
    Debug.Print Selection.Text
    Next i
    wdoc.Close
    Set wdoc = Nothing
    objApp.Quit
    Set objApp = Nothing

    End Sub

    thanks, please see attachment
    Attached Files Attached Files

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

    Re: HELP - Problem with Simple Word Task (Ms Word 2003)

    Welcome to Woody's Lounge!

    It looks like your cell contains a few empty paragraph marks at the end. Moreover, a cell in a Word table always contains two fixed characters at the end: an invisible paragraph mark Chr(13) and an end-of-cell marker Chr(7). Try something like this:

    Dim rng As Range
    Dim par As Paragraph
    Dim strText As String
    ...
    Set rng = ActiveDocument.Tables(3).Rows(9).Cells(8).Range
    rng.MoveEnd Unit:=wdCharacter, Count:=-2
    For Each par In rng.Paragraphs
    strText = par.Range.Text
    Debug.Print Left(strText, Len(strText) - 1)
    Next par

    Note that I don't select each paragraph in turn - that isn't necessary to obtain its text.

  3. #3
    New Lounger
    Join Date
    Mar 2008
    Location
    Frederick, Maryland, USA
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: HELP - Problem with Simple Word Task (Ms Word 2003)

    Hans are anyone who can help:

    The code that you recommend using worked, however when I add a line of to add a bookmark, the "Add bookmark method fails". I cursor over both strText and par.Range and it appears that they both have either empty paragraph marks (little boxes) in the text and range.

    Again, any help would be appreciated.

    Thanks

    Set rng = ActiveDocument.Tables(3).Rows(9).Cells(8).Range
    rng.MoveEnd Unit:=wdCharacter, Count:=-2
    For Each par In rng.Paragraphs
    strText = par.Range.Text
    Debug.Print Left(strText, Len(strText) - 1)
    wdoc.Bookmarks.Add "strText", par.Range
    Next par

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: HELP - Problem with Simple Word Task (Ms Word

    Please ignore my previous post for a moment.
    <hr><code>wdoc.Bookmarks.Add "strText", par.Range</code><hr>
    You are duplicating the name of the first bookmark by naming all of them strText (the exact string strText, not the content of strText). So that's probably what is causing the error.

    Now, if you change that...

    Little boxes could be line breaks or non-ASCII characters. In your example, there are punctuation characters such as period and hyphens. None of these things are permitted in bookmark names.

    Do you have an alternative naming scheme you would want to use?

  5. #5
    New Lounger
    Join Date
    Mar 2008
    Location
    Frederick, Maryland, USA
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: HELP - Problem with Simple Word Task (Ms Word

    Yes, I added some code to change the period (.) and dashes (-) to underscores. This worked for each of the cell items. But for the last cell item when I go to the bookmark, it highlights the entire cell instead of only the cell item. (This only happens for the last cell item)

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

    Re: HELP - Problem with Simple Word Task (Ms Word 2003)

    Try this:
    <code>
    Dim rng As Range
    Dim i As Integer
    Dim n As Integer
    Dim par As Paragraph
    Dim rngPar As Range
    Dim strText As String

    Set rng = ActiveDocument.Tables(3).Rows(9).Cells(8).Range
    rng.MoveEnd Unit:=wdCharacter, Count:=-2
    n = rng.Paragraphs.Count
    For i = 1 To n
    Set rngPar = rng.Paragraphs(i).Range
    rngPar.MoveEnd Unit:=wdCharacter, Count:=-1
    strText = rngPar.Text
    If Len(strText) > 0 Then
    ActiveDocument.Bookmarks.Add Name:="Bookmark" & i, Range:=rngPar
    End If
    Next i
    </code>
    Note that I didn't use strText as name of the bookmark - that is dangerous, because the name of a bookmark cannot contain spaces and cannot be numeric.

  7. #7
    New Lounger
    Join Date
    Mar 2008
    Location
    Frederick, Maryland, USA
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: HELP - Problem with Simple Word Task (Ms Word 2003)

    Hans, this works as expected.

    An immense thanks to you and all others

    Regards

Posting Permissions

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