Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 for your help!

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    The Placement argument should be either wdFloatOverText or wdInline. Try this:

    Code:
    If Not IsNull(ActiveChart) Then
      ' Paste chart at bookmark
      WDApp.ActiveDocument.Bookmarks("myMark").Range.PasteSpecial Link:=False, _
    	DataType:=wdPasteMetafilePicture, Placement:=wdInline, DisplayAsIcon:=False
    End If

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    [quote name='slbibs' post='786899' date='31-Jul-2009 07:17']I am pretty sure it bombs at the Placement:=bMark spot. Any ideas as to what I am doing wrong?[/quote]
    You are right about the bombing location and Hans has suggested the fix for that part. The other problem is your line which sets the bMark variable. This doesn't change the selection object and so that line is worthless in terms of the code you have shown us.
    Try this code as a replacement
    Code:
    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
    I don't know what you know about Word but you should be aware that bookmarks can be only a point in a document or it could also contain some content (a range). Note that if your myMark bookmark is a range rather than an insertion point then the pastespecial will probably delete the existing content of the bookmark. You might want to collapse the selection before pasting if this is likely to be a problem.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #4
    New Lounger
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='Andrew Lockton' post='786920' date='30-Jul-2009 18:27']You are right about the bombing location and Hans has suggested the fix for that part. The other problem is your line which sets the bMark variable. This doesn't change the selection object and so that line is worthless in terms of the code you have shown us.
    Try this code as a replacement
    Code:
    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
    I don't know what you know about Word but you should be aware that bookmarks can be only a point in a document or it could also contain some content (a range). Note that if your myMark bookmark is a range rather than an insertion point then the pastespecial will probably delete the existing content of the bookmark. You might want to collapse the selection before pasting if this is likely to be a problem.[/quote]


    My bookmark is just a reference point. You guys are AWESOME! That worked to perfection.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •