Copy Excel table to Word Bookmark - issue with intersecting bookmarks

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,907
Office Version
  1. 365
Platform
  1. Windows
Hi All

Using Excel, but think I need help more related to Word.

I am using a class in Excel to manage report creation in a Word document (template - for want of a better word).
Code:
Public Function RangeToBookmark(ByVal rngTarget As Excel.Range, ByVal strBookmark As String) As Boolean    Dim blnContinue     As Boolean
    Dim wrdDocument     As Object
    Dim wrdRange        As Object
    Dim lngRow          As Long
    Dim lngColumn       As Long
    
    'Exit if there is no document to edit (result = False)
    blnContinue = Not Me.WordDocument Is Nothing
    If Not blnContinue Then Exit Function


    Set wrdDocument = Me.WordDocument
    
    'Exit if the bookmark cannot be found (result = False)
    On Error Resume Next
        Set wrdRange = wrdDocument.Bookmarks(strBookmark).Range
    On Error GoTo 0
    blnContinue = Not wrdRange Is Nothing
    If Not blnContinue Then Exit Function
    
    'Attempt to paste range to the bookmark
    On Error Resume Next
        Call wrdRange.Tables(1).Delete
        Call rngTarget.Copy
        Call wrdRange.PasteExcelTable(LinkedToExcel:=False, WordFormatting:=False, RTF:=False)
        With wrdRange.Tables(1).Rows
            .HeightRule = 1
            .Height = 1.25
        End With
        Call wrdDocument.Bookmarks.Add(Name:=strBookmark, Range:=wrdRange)
        Application.CutCopyMode = False
    On Error GoTo 0
    
    'Result = True if all steps successful
    RangeToBookmark = (Err.Number = 0)
    
    Set wrdRange = Nothing
    Set wrdDocument = Nothing
End Function

This one function words great in most circumstances. However:

Say I have two bookmarks called:
TBL001
and
TBL002

with one beneath the other (i.e. a single carriage return).

It appears that the Bookmark Ranges are deemed intersecting. What I mean is that when I paste the first Excel table to TBL001 it is fine. However when we paste the second table to TBL002 then because the Bookmark Ranges intersect the method Call wrdRange.Tables(1).Delete will delete the first pasted table before pasting the second table. This means I have a single table output, not two.

Hope this makes sense. The immediate work around is to have two carriage returns between the bookmarks. But the effect of this is that the report doesn't look as good.

Any suggestions on how I can handle tables in two Bookmark Ranges where the Ranges sit one beneath the other?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
The simple fix might be to have an empty paragraph between the two bookmarks. Note that Word tables must be separated by at least an empty paragraph; otherwise you'll end up with a single table even if you do manage to get the output from both.
 
Upvote 0
Hi Paul

Thank you for responding.

I think my assumption of the ranges being contiguous is not correct.

So for example, if the code element is Word content:
Code:
Bookmark: TBL001
{paragraph}
Bookmark: TBL002

There is a break between the two bookmarks (I can see the paragraph icon).

I notice that the first pasted table is overwritten with the second table. Both bookmarks are present, but they occupy the exact same range.

In my code, the main elements are:

Get the range from the bookmark
Code:
Set wrdRange = wrdDocument.Bookmarks(strBookmark).Range

Delete the table that is currently positioned in the bookmark range
Code:
Call wrdRange.Tables(1).Delete

Paste the table from Excel to the bookmark range
Code:
Call rngTarget.Copy
Call wrdRange.PasteExcelTable(LinkedToExcel:=False, WordFormatting:=False, RTF:=False)

Reapply the bookmark
Code:
Call wrdDocument.Bookmarks.Add(Name:=strBookmark, Range:=wrdRange)

Is there any obvious reasons why the bookmarks will overlay each other as a result of this routine?
 
Upvote 0
Why are you deleting the existing table in the bookmark range?

Have you checked where wrdRng points to after you do that?
 
Upvote 0
Hi Norie

It's driven by Excel and the tables are elements of financial statements. Both structure and contents are likely to change.

Can you elaborate on 'points'? Can you recommend how I check the range points (could spare me having to look it up). :)
 
Upvote 0
All I mean is where/what in the document wrdRange 'points' to.
 
Upvote 0
'Points' as in where it is pointing to. In this case, it's determined by whatever is being passed to the Function as the strBookmark argument. You don't just need a paragraph break between the two bookmarks - you need at least an empty paragraph between them. Try:
Code:
Public Function RangeToBookmark(ByVal rngTarget As Excel.Range, ByVal strBookmark As String) As Boolean
    Dim wrdRange        As Object
    RangeToBookmark = False
    If Me.WordDocument Is Nothing Then Exit Function
    With Me.WordDocument
        If .Bookmarks(strBookmark).Exists Then
            Set wrdRange = .Bookmarks(strBookmark).Range
            With wrdRange
                .Text = vbCr
                rngTarget.Copy
                .PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False
                Application.CutCopyMode = False
                With .Tables(1).Rows
                    .HeightRule = 1
                    .Height = 1.25
                End With
            End With
            .Bookmarks.Add Name:=strBookmark, Range:=wrdRange
            RangeToBookmark = True: Set wrdRange = Nothing
        End If
    End With
End Function
 
Last edited:
Upvote 0
Thank you Paul - I'm going to give that a go.

Could you clarify a few assumptions I am making?

1. You are not deleting the table. Is that not needed now because you let the Text property to vbCr?

2. vbCr (carriage return) is what is actually determining that there is a paragraph between tables?

Thanks for your help.
 
Upvote 0
Could you clarify a few assumptions I am making?

1. You are not deleting the table. Is that not needed now because you let the Text property to vbCr?

2. vbCr (carriage return) is what is actually determining that there is a paragraph between tables?
Replacing the bookmark range's content with just a paragraph break, which is what .Text = vbCr does, both deletes the table and inserts and empty paragraph for the new table to be pasted over. Without that empty paragraph, the range collapses to whatever was following it, in this case the empty paragraph between the two tables it seems your destination document usually already has - then that gets pasted over and the two tables merge. Calling the function a second time would then replace the merged table - which I suspect is what you were seeing. Inserting the paragraph break is also less error-prone than trying to explicitly delete a table that might not be there.
 
Upvote 0

Forum statistics

Threads
1,214,972
Messages
6,122,530
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top