Insert graphic in Word document header via Excel VBA

  • Hi guys,


    I have this problem:


    I create a Word dosument with Excel VBA. Now I want to insert a graphic into the header. Since several people have to use the Excel workbook, the graphic is not saved in a separate file. Instead i have put in in an extra sheet in my workbook.


    How can I put this graphic to the Word document header (and change size and position)?


    To avoid misunderstandings: I do not use Word VBA entirely, only Excel VBa is used.


    I have experimented with the macro recorder, but something like this does not work:



    Code
    Dim objHeader As Object   
    
    
    Worksheets("Logo").Shapes.Range(Array("Group 4")).Copy
    
    
    Set objHeader = wdApp.ActiveDocument.Sections(1).Headers(1)
    objHeader.pasteandformat


    I have psoted this already here (German)
    http://www.herber.de/forum/arc…1336/t1335594.htm#1335594


    I hope for your help


    Thanks,
    Willi

  • Re: Insert graphic in Word document header via Excel VBA


    You could try:

    Code
    Set objHeader = appwd.ActiveDocument.Sections(1).Headers(1).Range
    objHeader.Paste (wdPasteDefault)


    Add the .RANGE to the assignment to ObjHeader and just do a simple PASTE.


    Please note this is typed freehand and is totally untested...

  • Re: Insert graphic in Word document header via Excel VBA


    Thanks for your answer,


    it works :)


    CAn you also Tell me how to change size and position of the graphic then?


    Thanks

  • Re: Insert graphic in Word document header via Excel VBA


    I don't have a Windows computer at the moment (Android pad), but would have assumed that if you format the picture as required in Excel, it would keep those attributes when pasted (or as much as possible).


    Failing that, it should be a simple matter to select and format the picture in code, for example:

    Code
    Set objHeader = wdapp.ActiveDocument.Sections(1).Headers(1).Range
    objHeader.Paste
    objHeader.inlineshapes(objHeader.inlineshapes.Count).Height = 20
  • Re: Insert graphic in Word document header via Excel VBA


    Hey,
    I get
    [h=1]
    Run-time error '5941' the requested member of the collection does not exist
    [/h]
    in the last line of your code. But I think, I can modify the picture in Excel.


    Thanks! Can I somehow flag this as resolved in this forum?

  • Re: Insert graphic in Word document header via Excel VBA


    Sorry about the error, but as I mentioned that sample code was typed freehand - I have no way to check exact syntax at the moment.


    The easiest way to resolve the error would be to load Word, insert a picture then record a macro while you select that picture and change a few of the attributes. You can then edit the Word macro to get the exact (if not the most concise) code used.


    The line

    Code
    objHeader.inlineshapes(objHeader.inlineshapes.Count)...

    is correct, it's just the various properties referred to by the line that needs to be corrected.


    There's no need to mark a thread as Solved - just leave it to sink down the page.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!