Results 1 to 7 of 7
  1. #1
    2 Star Lounger
    Join Date
    Jan 2009
    Location
    Rye, Victoria, Australia
    Posts
    152
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Here I am again, Colleagues All, sitting at the feet of the masters with what is probably a trivial question about bookmarks in MS Word. I have looked around MSDN and other MS sites, but the information (while useful) is so scattered, it is sometimes hard to create a consistent story about it all.
    Here is my problem. In a previous post, I inquired about putting a table into a word document as part of a mailmerge operation. Now, I have gone one step further and wish to insert the table (as previously described), preceded by a paragraph of text incorporating data from the Access database which drives this operation. The intention is to create templates which the client can use for a variety of reports and letters, so I have to allow for the possibility of either scenario (paragraph or table) or both. Now, I can place a bookmark for the text paragraph and a bookmark for the table. The problem is that both the paragraph and the table are of variable length. Assuming that the bookmarks are placed on adjacent lines, one above the other, I will need to achieve one of two things. When I populate the first bookmark, I would like the second to be pushed down the document so as to have the same location relative to the end of the text (or table) that it had to the first bookmark. Alternatively, if the text or table attached to the first bookmark overflows the location of the second bookmark, will it be destroyed by being overwritten or can I simply relocate it with a bit of code ?
    I have had quite conflicting advice on this point and I hope that some of you clever guys out there can resolve it for me.

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    Haven't read the first post so hope the following is relevant:

    As long as you insert the content in a way that it is wrapped by the placeholder bookmark, then you should not get a problem with content from the first bookmark overflowing the second bookmark - everything should just push down automatically.
    Code to insert text wrapped into a bookmark has been posted many times before; here's some I've used:
    Code:
    Public Sub InsertBmkTextWrapped(BmkName As String, BmkText As String)
       Dim Bmks As Bookmarks
       Dim BmkRange As Range
      
       Set Bmks = ActiveDocument.Bookmarks
      
       If Bmks.Exists(BmkName) Then
    	  Set BmkRange = Bmks(BmkName).Range
    	  BmkRange.Text = BmkText
    	  Bmks.Add BmkName, BmkRange
    	  Set BmkRange = Nothing
       End If
      
       Set Bmks = Nothing  
    End Sub
    - so that should work for inserting the paragraph text; however the above method only works for inserting text - I don't think it would work for inserting a table per se, as it is currently written.
    Probably it could be modified to deal with inserting a table as well; the exact method might depend on how your table is being originated - what methods, if any, are you already using in code to get the table and insert it into the document? Are you using copy/paste?

    Gary

  3. #3
    2 Star Lounger
    Join Date
    Jan 2009
    Location
    Rye, Victoria, Australia
    Posts
    152
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks for that, Gary.
    I originally created the table in Excel and then embedded the Excel table into the Word document. This worked in principle, but some features, such as the heavy top borders for subtotal cells did not translate properly. The way I now propose to do it is to have a function which performs the following actions:

    1. Create the document by copying it from a template.
    2. Call another function which creates the paragraph using data from the database and returns it.
    3. Insert the paragraph at the appropriate bookmark in the document.
    4. Close the document.
    5. Call another function which inserts the table.
    6. Open the document again and proceed with the mail merge.

    The document which creates the table opens the document, generates the table one row at a time (again using data from the database), writes that row into the document (starting at the bookmark) and closes the document after inserting the last row.

    The template itself may be a copy of another template (blank except for mail-merge fields and bookmarks) into which the user has entered their own (non-database) text. This text, too, needs to be secure from beng overwritten.

    Jim.

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    [quote name='jim_from_oz' post='793319' date='14-Sep-2009 17:53']Thanks for that, Gary.
    I originally created the table in Excel and then embedded the Excel table into the Word document. This worked in principle, but some features, such as the heavy top borders for subtotal cells did not translate properly. The way I now propose to do it is to have a function which performs the following actions:

    1. Create the document by copying it from a template.
    2. Call another function which creates the paragraph using data from the database and returns it.
    3. Insert the paragraph at the appropriate bookmark in the document.
    4. Close the document.
    5. Call another function which inserts the table.
    6. Open the document again and proceed with the mail merge.

    The document which creates the table opens the document, generates the table one row at a time (again using data from the database), writes that row into the document (starting at the bookmark) and closes the document after inserting the last row.

    The template itself may be a copy of another template (blank except for mail-merge fields and bookmarks) into which the user has entered their own (non-database) text. This text, too, needs to be secure from beng overwritten.

    Jim.[/quote]
    Jim,
    Assuming you have a document with a bookmark called "bmkInsertTable" sittiing by itself in one paragraph, and a bookmark called "bmkInsertText" sitting by itself in the next paragraph immediately below, the following code will insert a table and some text, to the two bookmarks, while preserving the paragraph (line) space between the two bookmarks:
    Code:
    Sub InsertTableAndTextToBmks()
       Dim rngInsertTable As Range
       Set rngInsertTable = ActiveDocument.Bookmarks("bmkInsertTable").Range
       rngInsertTable.Tables.Add rngInsertTable, 6, 4
       InsertBmkTextWrapped "bmkInsertText", "This is the text we inserted."
       'Remove artifact paragraph that gets inserted after table - quick and dirty via Selection:
       ActiveDocument.Bookmarks("bmkInsertText").Range.Select
       Selection.Collapse wdCollapseStart
       Selection.TypeBackspace
       Set rngInsertTable = Nothing
    End Sub
    (the code calls the 'InsertBmkTextWrapped' sub posted earlier in this thread).
    The 6 and 4 indicate the number of rows and columns to add.

    Gary

  5. #5
    2 Star Lounger
    Join Date
    Jan 2009
    Location
    Rye, Victoria, Australia
    Posts
    152
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Hi, Gary,

    The only problem I have with your idea, Gary, is that your code requires the number of rows to be known before writing the table to the document. The creation of the table (which is a set of itemised costs for catering a social function) is done one line at a time, with periodic subtotals as one works down the list. However, the simplicity of your approach has suggested another solution. I could build the table in another (temporary) document and then simply do a transfer into the main one. This is similar to what I did with Excel except, that, as I said earlier, some of the properties did not translate to my satisfaction. If this is a Word table, one would imagine that the transfer of cells would include all of the cell properties.
    Anyway, I will give this a go and report back in due course.
    Thanks again.

    Jim.

  6. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi Jim,

    From your problem descriptions in both this thread and your other one, it seems you might be able to use a Catalog or Directory mailmerge. For more details, check out my my Word 97-2007 Catalogue/Directory Mailmerge Tutorial, in post 731107.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. #7
    2 Star Lounger
    Join Date
    Jan 2009
    Location
    Rye, Victoria, Australia
    Posts
    152
    Thanks
    12
    Thanked 0 Times in 0 Posts
    Thanks for that macropod (surely not Bigfoot ???)

    I have downloaded your stuff and will give it a whirl. My intention is to allow the users as much latitude as possible in creating and running mail-merges from within Access, with the programmer only having to intervene once to create the generator for the material attached to a particular bookmark. Perhaps your model will do this.

    Jim

Posting Permissions

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