Issue with Bookmark

chandrashekar

Well-known Member
Joined
Jul 15, 2005
Messages
529
Office Version
  1. 365
Platform
  1. Windows
Hello,

am trying to paste chart & table to word document using excel vba code. I have created few bookmarks in word. but when I run code am getting multiple chart & graphs.

May I know what is the issue?

Public Sub copypaste_TemplateTable()

Dim pvt_tbl As PivotTable
Dim appWrd As Object
Dim objDoc As Object
Dim FilePath As String
Dim FileName As String
Dim wb As Workbook
Dim wbfile As String


FilePath = "D:\"
FileName = "AOne.docx"


Set appWrd = CreateObject("Word.Application")
Set objDoc = appWrd.Documents.Open(FilePath & "\" & FileName)

appWrd.Visible = True

On Error Resume Next

appWrd.Selection.Goto What:=wdGoToBookmark, Name:="Chart_1_1"
ThisWorkbook.Worksheets(1).Select
ThisWorkbook.Worksheets(1).ChartObjects(1).Copy
appWrd.Selection.Paste xlPasteAll
appWrd.Bookmarks(1).Add "Chart_1_1", objrange

end sub

Regards,

Chandru
 
Hello All,

Thanks its working fine now.:-) Is there any way other than bookmark to paste data in word(Learning purpose)?

Could you let me know is there any materials is there to learn VBA code for word and PowerPoint.

Thanks in advance.


Regards,

Chandru
 
Upvote 0

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.
Thanks for posting your outcome. There is mailmerge, U can move to any selection point in the document and do whatever, U can also paste to formfields or tables and probably do some stuff that I haven't mentioned. Google is a really good at helping U find any resource materials that U need. Have a nice day. Dave
 
Upvote 0
Dave/chandrashekar,

Sorry I have not be back earlier and I'm glad I could help. Dave, thanks for the warm welcome.
 
Upvote 0
Is there any way other than bookmark to paste data in word(Learning purpose)?
You could insert the output into a content control, table, a defined Section or range within the document; it's really up to you to decide what best suits your needs.
 
Upvote 0
I'm not happy with the code I posted... it was missing the "Set objDoc = Nothing" and that lack of clarity re. file path was probably making it useless for less advanced users. Also, I'm guessing repetitive use of this routine will eventually cause the clipboard to overfill and crash. API's should probably be used to open, empty and then close the clipboard after each use. So...
module code...
Code:
'Open the clipboard to read
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
'Empty the clipboard 
Public Declare Function EmptyClipboard Lib "user32" () As Long
'Close the clipboard
Public Declare Function CloseClipboard Lib "user32" () As Long

To operate adjust the code for file path to suit, the chart name to suit, the range to suit and both bookmark names to suit. Then to operate...
Code:
Call XLTable_Rng_Chart_toWordBookmark
OpenClipboard
EmptyClipboard
CloseClipboard

Here's the updated code. Dave
Code:
Public Sub XLTable_Rng_Chart_toWordBookmark()
Dim appWrd As Object, objDoc As Object
Dim FilePath As String, FileName As String
Dim TblRng As Range, ORng As Object, LngIndex As Long
'Transfers chart(1) of sheet(1) to existing Word bookmark
'Transfers table/range to existing Word bookmark

FilePath = "D:\" '***change to suit
FileName = "AOne.docx" '***change to suit
On Error GoTo ErFix
Set appWrd = CreateObject("Word.Application")
Set objDoc = appWrd.Documents.Open(FilePath & FileName)

'put chart(1) from sheet(1) in bookmark
ThisWorkbook.Worksheets(1).ChartObjects(1).CopyPicture _
           Appearance:=xlScreen, Format:=xlPicture
With objDoc
'*** "Chart_1_1" bookmark must exist!!
Set ORng = objDoc.Bookmarks("Chart_1_1").Range
ORng.Delete
ORng.Paste
.Bookmarks.Add "Chart_1_1", ORng
End With
Application.CutCopyMode = False

'put table/range in bookmark
'**set TblRng to ws range containing table
With ThisWorkbook.Worksheets(1)
'***change TblRng to suit
Set TblRng = .Range(.Cells(1, "A"), .Cells(6, "G"))
End With
TblRng.Copy
With objDoc
'*** "TblRngBookmark" must exist!!
Set ORng = objDoc.Bookmarks("TblRngBookmark").Range
ORng.Delete
For LngIndex = ORng.Tables.Count To 1 Step -1
ORng.Tables(LngIndex).Delete
Next
ORng.Paste
.Bookmarks.Add "TblRngBookmark", ORng
End With
Application.CutCopyMode = False

Set ORng = Nothing
'save & quit Word
appWrd.objDoc.Close savechanges:=True
Set objDoc = Nothing
appWrd.Quit
Set appWrd = Nothing
Exit Sub

ErFix:
On Error GoTo 0
MsgBox "Error"
Set ORng = Nothing
Set objDoc = Nothing
appWrd.Quit
Set appWrd = Nothing
End Sub
 
Upvote 0
I can't see the point of all the clipboard manipulations and you should test for the presence of the Word bookmark, instead of assuming it exists:
Code:
With objDoc
  'Confirm the "Chart_1_1" bookmark exists!!
  If .Bookmarks("Chart_1_1").Exists Then
    Set ORng = .Bookmarks("Chart_1_1").Range
    ORng.Delete
    ORng.Paste
    .Bookmarks.Add "Chart_1_1", ORng
  End If
End With
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,271
Members
452,628
Latest member
dd2

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