Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
|
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 30th, 2009, 05:19 PM
Authorized User
 
Join Date: Jun 2003
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Default Write Excel VBA Code to Target Bookmark in Word Doc

I have a word doc called myGraph.doc and a bookmark within the document entitled myMark. What I would like to do is have code within an Excel Doc place a graph at the designated bookmark ("myMark").

I have code written in Excel that opens the above Word document (myGraph.doc) and places a picture of the graph in it. The problem is that it places it in the Title area of the document and not at the designated bookmark.

When I attempt to add code to place the picture chart at the designated spot, I get an 4198 run time error - Application defined or object defined error. The following is the code producing the error:

Code:
Dim bMark As Bookmark
If Not IsNull(ActiveChart) Then 
Set bMark = WDApp.ActiveDocument.Bookmarks("myMark")
 
' Paste chart at cursor position
WDApp.Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
Placement:=bMark, DisplayAsIcon:=False
End If
I am pretty sure it bombs at the Placement:=bMark spot. Any ideas as to what I am doing wrong? I am a .Net developer and am pretty green when it comes to developing in VBA.

Thanks (in advance) for your help!
__________________
SLBIBS

Last edited by slbibs; July 31st, 2009 at 09:34 AM..
 
Old July 30th, 2009, 09:56 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Can you try selecting the bookmark before pasting the chart. Something like

bMark.Select

Code:
Dim bMark As Bookmark
    If Not IsNull(ActiveChart) Then
         Set bMark = WDApp.ActiveDocument.Bookmarks("myMark")
        bMark.Select
' Paste chart at cursor position
         WDApp.Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, _
       Placement:=bMark, DisplayAsIcon:=False
    End If
Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)
 
Old July 31st, 2009, 09:37 AM
Authorized User
 
Join Date: Jun 2003
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi, thanks for your suggestion but I get the same error. I will keep hammering away at this. Any other suggestions are certainly welcome!
__________________
SLBIBS
 
Old July 31st, 2009, 09:47 AM
Authorized User
 
Join Date: Jun 2003
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Default

OK... I had this on another forum and got it resolved. This is what my code should have looked like:

Code:
Dim bMark As Bookmark
If Not IsNull(ActiveChart) Then
    'move the cursor to the correct location
    WDApp.Selection.GoTo What:=wdGoToBookmark, Name:="myMark"
    'Paste chart at cursor position
    WDApp.Selection.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, Placement:=wdInLine, DisplayAsIcon:=False
End If
__________________
SLBIBS





Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i get vb printer.print code for a word doc choudhry.usman Word VBA 1 March 6th, 2008 10:46 PM
Copy text from 1 doc to other doc thru vba in word itchock Word VBA 2 December 29th, 2006 02:47 AM
Copy contents of Word doc & Paste in Excel - How?? robear Javascript How-To 1 August 23rd, 2006 03:22 PM
How to Copy contents of Word doc & Paste in Excel robear Pro PHP 0 August 21st, 2006 12:25 PM
How to insert an equation in a Word doc. Thru VBA? PedroZorro VB How-To 1 May 31st, 2005 12:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.