VBA Copy Excel Table to Word Bookmark but keep bookmark

MarkTVC

New Member
Joined
Jun 17, 2020
Messages
5
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi all,

I am running some code which copies a table from Excel to Word inside a bookmark I wish to retain the bookmark for later use. Below is the code.

The following is the general flow:
Opens Word
Opens a Document Template
Copies a ListObject Table
Finds the bookmark in Word
Pastes the Table


Sub UpdateBookmarkContents()

'set word application dimensions
Dim wdApp As Object
Dim wdDoc As Object
Set wdApp = CreateObject("word.application")
'Open the relevent Document
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Add(Template:="\\svr\documents\Word\Template.dotx", NewTemplate:=False, DocumentType:=0)


'Clear Clipboard
Application.CutCopyMode = False
' Create reference to QuoteTableW and copy

Dim ExcLisObjq As ListObject
Set ExcLisObjq = Sheets("Quote Tables").ListObjects("QuoteTableW")
ExcLisObjq.Range.Copy

'Paste Object to Location


wdDoc.Bookmarks("RNBMQuoteTableW").Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=True

'Clear Clipboard
Application.CutCopyMode = False

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Welcome to the Forum,

Ok the code should retain your Bookmark as the content is being added at the position of the bookmark. What are you looking to do with the New Document, it isn't being saved or any other action via code. In your comments it says you are opening a document this is adding a new document from the template.

What are you trying to do, besides copying the Excel Table?
 
Upvote 0
unfortunately it does delete the bookmark. The paste replace all text and the bookmark.

To answer the question what am I trying to do..

Firstly this is a part of a much bigger set up subs which update the document and save it.

I have a large client report. there are about 20 different tables and charts to be added to specific locations. I can use paste and LinkedToExcel:=True, _ , however the number of tables wanting to refresh have an impact on the document performance.

The goal is to post the tables early in the process, allow the document to be update by user manually, then add the table again at a later date.
 
Upvote 0
I have just written this code and the Bookmark stays in place. Perhaps you need to add in the GoTo Bookmark.

VBA Code:
Sub GoToWord()
Dim wrdApp As Word.Application
Set wrdApp = CreateObject("Word.Application")
Application.Goto Reference:="QuoteTableW"
Selection.Copy
With wrdApp
.Visible = True
.Documents.Open "C:\XXXXX\Heading.docx"
'Bookmark name is tblDrop
.Selection.Goto What:=wdGoToBookmark, Name:="tblDrop"
    .Selection.PasteExcelTable False, False, True
End With
End Sub
 
Upvote 0
hmm tried this. still clears the bookmarks
bookmark.jpg
 
Upvote 0
What happens if you don't have the table within the bookmark and only have the bookmark position. Because otherwise your looking to fill a table and would need to identify which table number it is in the document.
 
Upvote 0
This has provide an answer for now.


Dim iTableNum As Integer

For J = 1 To wdDoc.Tables.Count
wdDoc.Tables(J).Select
iTableNum = J
Exit For
Next J

wdDoc.Bookmarks("RNBMQuoteTableW").Select
wdApp.Selection.Paste

wdDoc.Bookmarks.Add "RNBMISRQuoteTableW", wdDoc.Tables(iTableNum)

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,799
Members
449,337
Latest member
BBV123

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