• Write Excel VBA Code to Target Bookmark in Word Doc

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Write Excel VBA Code to Target Bookmark in Word Doc

    Author
    Topic
    #461571

    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!

    Viewing 1 reply thread
    Author
    Replies
    • #1171638

      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
    • #1171651

      I am pretty sure it bombs at the Placement:=bMark spot. Any ideas as to what I am doing wrong?

      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.

      • #1171698

        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.

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

    Viewing 1 reply thread
    Reply To: Write Excel VBA Code to Target Bookmark in Word Doc

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: