将图像从Excel复制到Word

时间:2018-02-26 13:12:39

标签: excel vba excel-vba ms-word word-vba

我的代码可以从Word复制信息并将其粘贴到Excel中。代码是:

Sub ExceltoLabel_ActiveX()
    Dim objExcel As New Excel.Application
    Dim exWb As Excel.Workbook

    'only allow the user to select one file
    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
    'make the file dialog visible to the user
    intChoice = Application.FileDialog(msoFileDialogOpen).Show
    'determine what choice the user made

    If intChoice <> 0 Then
        'get the file path selected by the user
        strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
    End If

    Set exWb = objExcel.Workbooks.Open(strPath)

    ThisDocument.Label1.Caption = exWb.Sheets("Sheet1").Range("A1")
    ThisDocument.Label3.Caption = exWb.Sheets("Sheet1").Range("A2")
    ThisDocument.Image1.Picture = LoadPicture(exWb.Sheets("Sheet1").Range("A3"))
    ThisDocument.Image2.Picture = LoadPicture(exWb.Sheets("Sheet1").Range("A4"))
    ThisDocument.Image21.Picture = LoadPicture(exWb.Sheets("Sheet1").Range("A6"))

    exWb.Close

    Set exWb = Nothing

End Sub

但我需要反过来这样做:从Excel复制并将其粘贴到Word中,我有以下代码,目前复制文本,但它不能处理图像:

Sub ExceltoLabel_ActiveX()
    Dim objWord As New Word.Application
    Dim doc As Word.Document

    'only allow the user to select one file
    Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
    'make the file dialog visible to the user
    intChoice = Application.FileDialog(msoFileDialogOpen).Show

    'determine what choice the user made
    If intChoice <> 0 Then
        'get the file path selected by the user
        strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
    End If

    Set doc = objWord.Documents.Open(strPath)

    doc.Label1.Caption = ActiveSheet.Range("A2")
    'LoadPicture(ActiveSheet.Range("A3")) = doc.Image1.Picture

    doc.Close

End Sub

我需要从Excel复制图像并将其粘贴到单词中,但此刻,我不能......

1 个答案:

答案 0 :(得分:0)

这个:

\r?

不起作用,因为ActiveSheet.Range(“A2”)引用A2中的值,而不是形状。您可以在循环中选择形状并将它们粘贴到Word文档中,例如:

doc.Label1.Caption = ActiveSheet.Range("A2")

但这可能会不稳定,具体取决于您如何将图片插入Excel中。无论如何,我认为您可以修改我的代码以满足您的需求。