Jon von der Heyden
MrExcel MVP, Moderator
- Joined
- Apr 6, 2004
- Messages
- 10,907
- Office Version
- 365
- Platform
- 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).
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?
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?