如何将数据从Excel加载到Word

时间:2019-03-05 16:23:05

标签: excel vba ms-word

我想在我的word文档中实现vba代码: 如何将Excel文档中的sheet1中的Cell(1,1)加载到当前Word文档中(假设IST路径为C:\ Test \ Excel.xlsx)。 Word文档中已经存在一个表(2x2)。要将其插入表的第一个单元格。 非常感谢!

1 个答案:

答案 0 :(得分:0)

  

这些解决方案使用后期绑定来传达Word-Excel


请参见此处learn more about late vs early binding


从Word到Excel进行通信并将文本重新插入Word的步骤。

请按照以下步骤操作:

在Word中:

1)在单词的表格单元格中插入一个书签,并将其命名为“ FirstCell”

enter image description here

2)添加模块

enter image description here

3)复制/粘贴此代码,并修改“ <<<自定义此>>>”部分

enter image description here

代码:

Sub InsertFromWordIntoExcel()

    Dim oExcel As Object
    Dim excelDocument As Object
    Dim bookmarkRange As Range

    Dim bookmarkName As String

    Dim excelWorkbookPath As String
    Dim exceWorkbookName As String
    Dim sheetName As String
    Dim cellContentAddress As String

    ' <<< Customize this >>>
    excelWorkbookPath = "C:\Test\" ' include backslash at the end
    exceWorkbookName = "Excel.xlsx"
    bookmarkName = "FirstCell"
    sheetName = "Sheet1"
    cellContentAddress = "A1"

    ' Check if Excel is already opened
    On Error Resume Next

    Set oExcel = GetObject(, "Excel.Application")

    If Err.Number <> 0 Then
        ' Open a new instance
        Set oExcel = CreateObject("Excel.Application")
        oExcel.Visible = True
    End If

    ' Check if document is already open
    Set excelDocument = oExcel.Workbooks(exceWorkbookName)

    If Err.Number <> 0 Then
        ' Open excel workbook
        Set excelDocument = oExcel.Workbooks.Open(excelWorkbookPath & exceWorkbookName)
    End If

    ' Reset error handling
    Err.Clear
    On Error GoTo 0

    ' Get the bookmark range
    Set bookmarkRange = ThisDocument.Bookmarks(bookmarkName).Range

    ' Insert the cells text
    bookmarkRange.Text = excelDocument.Sheets(sheetName).Range(cellContentAddress).Value

    ' Add the bookmark again
    ThisDocument.Bookmarks.Add bookmarkName, bookmarkRange

End Sub

或者,通过Excel进行通讯并将单元格的文本插入Word。

1)在Word中添加书签(如上所述)

2)在Excel中添加模块

3)复制/粘贴此代码,并修改“ <<<自定义此>>>”部分

Sub InsertFromExcelIntoWord()

    Dim oWord As Object
    Dim wordDocument As Object
    Dim bookmarkRange As Object

    Dim wordDocumentPath As String
    Dim wordDocumentName As String
    Dim bookmarkName As String
    Dim sheetName As String
    Dim cellContentAddress As String

    ' <<< Customize this >>>
    wordDocumentPath = "C:\Test\"  ' include backslash at the end
    wordDocumentName = "Word.docx"
    bookmarkName = "FirstCell"
    sheetName = "Sheet1"
    cellContentAddress = "A1"

    ' Check if Word is already opened
    On Error Resume Next

    Set oWord = GetObject(, "Word.Application")

    If Err.Number <> 0 Then
        ' Open a new instance
        Set oWord = CreateObject("Word.Application")
        oWord.Visible = True
    End If

    ' Check if document is already open
    Set wordDocument = oWord.documents(wordDocumentName)

    If Err.Number <> 0 Then
        ' Open word document
        Set wordDocument = oWord.documents.Open(wordDocumentPath & wordDocumentName)
    End If

    ' Reset error handling
    Err.Clear
    On Error GoTo 0

    ' Get the bookmark range
    Set bookmarkRange = wordDocument.Bookmarks(bookmarkName).Range

    ' Insert the cells text
    bookmarkRange.Text = ThisWorkbook.Sheets(sheetName).Range(cellContentAddress).Value

    ' Add the bookmark again
    wordDocument.Bookmarks.Add bookmarkName, bookmarkRange

End Sub