Results 1 to 12 of 12
  1. #1
    New Lounger
    Join Date
    Aug 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Copy Tables with Bookmarks (word 2000 )

    I have a .doc file that has tables 2 tables with bookmarks in it. AT runtime depending on the number of rows in a local table, i want to be able to copy the table along w/ the bookmarks. Please let me know how this can be done...

    Thanks,

  2. #2
    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: Copy Tables with Bookmarks (word 2000 )

    Where do you want to copy to? Within a single file, you cannot duplicate bookmark names, so your copy will be missing the bookmarks. Your macro code could create new ones if there is an easy way to figure out where they go...

  3. #3
    New Lounger
    Join Date
    Aug 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy Tables with Bookmarks (word 2000 )

    I want to copy the table(S) to the same doc file.Ok, i understand abt the bookmarks disappearing because of the copy.

    But can i copy the bookmarks with similar names with an identifier at the end . For ex: if the bookmark in the original table was comp_name, in table(1) i can name it as comp_name1 or sumthing like that. If so how can this be done?

    Thanks,

  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: Copy Tables with Bookmarks (word 2000 )

    Right now, my mind is flooded with very complicated ideas I've never tried. Hopefully someone will have a quicker and easier solution soon. It might help to post a sample document containing the part you want to copy along with any relevant context.

  5. #5
    New Lounger
    Join Date
    Aug 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy Tables with Bookmarks (word 2000 )

    ok here i am attaching the .doc file w/ the tables and bookmarks...

    Thanks for all your help:-)
    Attached Files Attached Files

  6. #6
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy Tables with Bookmarks (word 2000 )

    It ain't pretty, but it works (at least on the document you posted), and should get you moving in the right direction.

    It works on the currently selected table, and will duplicate it w/bookmarks (bookmark names get a "1" tacked on the end) and paste the new table at the end of the document. If you planned to copy a table multiple times, you'd want to adjust the code to increment that accordingly (consider a static variable or a document variable).

    HTH!

    <pre>Sub CopyTblWithBookmarsToEndOfDoc()
    Dim tblOrig As Table
    Dim tblNew As Table
    Dim rng As Range
    Dim str As String
    Dim rngPastePoint As Range
    Dim bk As Bookmark
    Dim rngToSearch As Range
    Dim rngResult As Range
    Dim doc As Document

    Set tblOrig = Selection.Tables(1)
    Set doc = Selection.Document

    Dim k As Integer
    For k = tblOrig.Range.Bookmarks.Count To 1 Step -1
    Set bk = tblOrig.Range.Bookmarks(k)
    Set rng = bk.Range
    rng.Text = "====" & bk.Name & "===="
    Next k

    tblOrig.Range.Copy

    ' Add a blank paragraph at end
    ' and paste in table
    doc.Range.InsertAfter vbCrLf
    Set rngPastePoint = doc.Range

    rngPastePoint.Collapse wdCollapseEnd
    rngPastePoint.Paste

    Set tblNew = rngPastePoint.Tables(1)
    Set rngToSearch = doc.Range(tblNew.Range.start, doc.Range.End)
    Set rngResult = rngToSearch.Duplicate

    Do
    With rngResult.Find
    .ClearFormatting
    .Text = "====*===="
    .Forward = True
    .MatchWildcards = True
    .Wrap = wdFindStop
    .Execute
    End With

    If rngResult.Find.Found = False Then
    Exit Do
    End If
    With rngResult
    .Select
    str = rngResult.Text
    .Delete
    With doc.Content.Find
    .Text = str
    .Replacement.Text = ""
    .MatchWildcards = False
    .Format = False
    .Execute Replace:=wdReplaceAll
    End With
    str = Replace(str, "====", "")
    .Bookmarks.Add Name:=str & "1", Range:=rngResult
    .MoveStart wdCharacter
    .End = rngToSearch.End
    End With
    Loop Until rngResult.Find.Found = False
    End Sub
    </pre>


  7. #7
    New Lounger
    Join Date
    Aug 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy Tables with Bookmarks (word 2000 )

    Thank you soo very much!!!! This code helped me a LOT:-) But, i have a question, when new tables are added to a .doc file do I need to re-set or set some Word properties to recognize the new tables? I ask this question because, i modified the above code to run 3 times for the first table and 2 times for the second table. And in the end I want to delete the Original 2 tables so.. i use the following code to delete the tables :
    For i = 1 To 2
    docNew.Tables(i).Delete
    Next
    For some weird reason it delete the 1st table and the 3rd table in the document. Can sumone tell me why this is hapenning?

    Thanks,

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Copy Tables with Bookmarks (word 2000 )

    This is a very common mistake in VBA. when you deleted the first table, the one that was second now became docNew.Tables(1), and the third one became docNew.Tables(2).

    The solution is to delete them in reverse order

    For i = 2 to 1 step -1
    docNew.Tables(i).Delete
    Next

    StuartR

  9. #9
    New Lounger
    Join Date
    Aug 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy Tables with Bookmarks (word 2000 )

    Thanks Again you guys r just great!!! Ok, here is another problem that i have now... i am calling the same function (CopyTblWithBookmarsToEndOfDoc()) twice in the main function, but the second time when i call it, it gives me an error" requested object does not exist in collection" at the statement Set tblOrig = Selection.Tables(i).

    Here is how i am calling it ...
    If ll_food_rows > 0 Then
    Call CopyTblWithBookmarsToEndOfDoc(ll_food_rows, 1)
    End If
    If ll_bev_rows > 0 Then
    Call CopyTblWithBookmarsToEndOfDoc(ll_bev_rows, 2)
    End If

    And here is the modified function ::
    Sub CopyTblWithBookmarsToEndOfDoc(ByVal num_tbls As Integer, ByVal tbl_num As Integer)
    Dim tblOrig As Table
    Dim tblNew As Table
    Dim rng As Range
    Dim str As String
    Dim rngPastePoint As Range
    Dim bk As Bookmark
    Dim rngToSearch As Range
    Dim rngResult As Range
    Dim doc As Document
    Dim i As Integer

    Set tblOrig = Selection.Tables(tbl_num)
    Set doc = Selection.Document

    Dim k As Integer
    For k = tblOrig.Range.Bookmarks.Count To 1 Step -1
    Set bk = tblOrig.Range.Bookmarks(k)
    Set rng = bk.Range
    rng.Text = "====" & bk.Name & "===="
    Next k

    tblOrig.Range.Copy

    ' Add a blank paragraph at end
    ' and paste in table
    doc.Range.InsertAfter vbCrLf
    Set rngPastePoint = doc.Range

    rngPastePoint.Collapse wdCollapseEnd
    rngPastePoint.Paste

    Set tblNew = rngPastePoint.Tables(tbl_num)
    Set rngToSearch = doc.Range(tblNew.Range.Start, doc.Range.End)
    Set rngResult = rngToSearch.Duplicate

    Do
    With rngResult.Find
    .ClearFormatting
    .Text = "====*===="
    .Forward = True
    .MatchWildcards = True
    .Wrap = wdFindStop
    .Execute
    End With

    If rngResult.Find.Found = False Then
    Exit Do
    End If
    For i = 1 To num_tbls
    With rngResult
    .Select
    str = rngResult.Text
    .Delete
    With doc.Content.Find
    .Text = str
    .Replacement.Text = ""
    .MatchWildcards = False
    .Format = False
    .Execute Replace:=wdReplaceAll
    End With
    str = Replace(str, "====", "")
    .Bookmarks.Add Name:=str & i, Range:=rngResult
    .MoveStart wdCharacter
    .End = rngToSearch.End
    End With
    Next
    Loop Until rngResult.Find.Found = False
    End Sub


    Thanks Again

  10. #10
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy Tables with Bookmarks (word 2000 )

    Add this variable to the function:
    <pre>Dim rngOriginal as Range
    </pre>

    Then just above the line:
    <pre>Set tblOrig = Selection.Tables(tbl_num)
    </pre>

    add this:
    <pre>Set rngOriginal = Selection.Range.Duplicate
    </pre>

    And then, at the end, just before the End Sub line, add this:
    <pre>rngOriginal.Select
    </pre>


  11. #11
    New Lounger
    Join Date
    Aug 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copy Tables with Bookmarks (word 2000 )

    tried that, but it still gives me the error "The requested member of the collection does not exist". However, when i go look at the .doc file, the correct table is selected and exists...

  12. #12
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Copy Tables with Bookmarks (word 2000 )

    Just before the line that gives the error can you add

    MsgBox selection.tables.count

    To check that there really are two tables in the selection.

    StuartR

Posting Permissions

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