简体   繁体   中英

How can I transfer an image from a cell in Excel to the Word header (left) VBA

how to insert a picture which is in an Excel Celle (C1) by VBA in a new created Word document in the header without formatting(no cell color)?

logo.copy

Set objHeader = myDoc.Sections(1).Headers(1).Range
objHeader.Paste

thank you!

Please, try the next way:

Sub InsertHeaderPict()
  'copy picture from Excel (open session, active sheet):
  Dim appExcel As Excel.Application, ws As Excel.Worksheet
  Set appExcel = GetObject(, "Excel.Application")
   Set ws = appExcel.ActiveWorkbook.ActiveSheet
   ws.Shapes("Picture 1").CopyPicture xlScreen, xlBitmap 'use here your real picture name

    'create a table of a row, 3 columns and paste the copied picture in its first cell:
    Dim oSec As Word.Section, rng As Range
    'ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
     Set oSec = ActiveDocument.Sections(1)
     Set rng = oSec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
     With rng
          .Tables.Add Range:=rng, NumRows:=1, NumColumns:=3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow
          With .Tables(1)
              .Borders.InsideLineStyle = wdLineStyleNone
              .Borders.OutsideLineStyle = wdLineStyleNone
              .Rows.SetLeftIndent LeftIndent:=-37, RulerStyle:=wdAdjustNone
              .Cell(1, 1).Range.PasteSpecial
          End With
    End With
End Sub

There must be a picture in the active sheet of the Excel open session. Use this real picture name instead of "Picture 1" and run the code.

I believe you have the right idea, just have to use copypicture and pastespecial

This is a snippet of my code that does basically the same thing I'm just using the shape object instead of a range.

Set reportHeader = masterReport.Sections.Item(1).Headers(wdHeaderFooterPrimary).Range
masterWorkbook.Worksheets("Template").Shapes("LogoSmall").CopyPicture
reportHeader.PasteSpecial

Thanks again to this post where I originally found this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM