【问题标题】:Transferring data from excel to MS word将数据从excel传输到MS word
【发布时间】:2017-07-19 21:40:57
【问题描述】:

我需要一个 VBA 代码来更新我的 word 文件。它由一些必须从 excel 文件更新的表组成。 Excel 文件由不同轴承编号的轴承数据组成。我的报告必须用轴承值更新。就像我的下一份报告一样,如果我只是输入不同的轴承文件,它必须从该文件中读取所有轴承数据。

这必须分 3 步完成。我附上了一张示例图片。首先确定始终在 A 列中的轴承名称(在这种情况下,我需要找到 (248_R), 38,7 % )。然后选择6*6的矩阵数据(假设我发现方位数据在A946,那么我需要记录B950到G955的数据)然后传输到word文件(只有表中的值)。我是 VBA 编码的新手,有人可以帮忙吗?

image of sample bearing name with matrix underneath

表格在 word 文档中的样子:

【问题讨论】:

  • “A”列中的轴承名称是否总是合并单元格?
  • 这里也没有合并。它在 A946 电池中。

标签: vba excel ms-word


【解决方案1】:

复制所需范围的第一部分相对容易。您可以使用以下代码复制所需的矩阵。我还不确定是否要粘贴到 Word 文档中,请再给我一些时间。 (现在,如果你运行这个宏,你想要的范围将被复制。然后你可以切换到你的 Word 文档并按 Ctrl+V 将其粘贴到所需的表格中。

另外,请检查是否添加了以下引用:

Option Explicit

Sub findBearingDataAndPasteToWord()
    Dim i As Integer
    Dim aCell As Range, rng As Range
    Dim SearchString As String

    Set rng = Range("A750:A1790")
    SearchString = "(248_R), 38,7 %"

    For Each aCell In rng
        If InStr(1, aCell.Value, SearchString, vbTextCompare) Then
            ActiveSheet.Range(Cells(aCell.row + 4, 1), Cells(aCell.row + 9, 6)).Copy

            Dim wrdApp As Word.Application
            Dim docWd As Word.Document

            MsgBox "Please select the word document that you want to paste the copied table data into (after pressing OK)" & _
                vbNewLine & vbNewLine & "Script written by takanuva15 with help from Stack Overflow"
            docFilename = Application.GetOpenFilename()
            If docFilename = "False" Then Exit Sub
            Set docWd = getDocument(docFilename)
            Set wrdApp = docWd.Application

            wrdApp.Selection.EndKey Unit:=wdStory
            wrdApp.Selection.TypeParagraph
            wrdApp.Selection.TypeParagraph
            wrdApp.Selection.PasteExcelTable False, True, False

            Exit Sub
        Else: End If
    Next aCell
End Sub

'Returns the document with the given filename
'If the document is already open, then it returns that document
Public Function getDocument(ByVal fullName As String) As Word.Document
    On Error Resume Next
    Set wrdApp = GetObject(, "Word.Application")
    If wrdApp Is Nothing Then Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True

    Dim fileName As String
    Dim docReturn As Word.Document

    fileName = Dir(fullName)
    Set docReturn = Word.Documents(fileName)
    If docReturn Is Nothing Then
        Set docReturn = Word.Documents.Open(fullName)
    End If
    On Error GoTo 0
    Set getDocument = docReturn
End Function

【讨论】:

  • 非常感谢您的回复。我需要使用 ifstr ,因为前一部分可能已经改变。 Socoull you extend frm here Option Explicit Sub findBearing() Dim i As Integer Dim aCell As Range, Rng As Range Dim SearchString As String Set Rng = Range("A750:A1790") SearchString = "(248_R), 38,7 % " 对于 Rng 中的每个 aCell If InStr(1, aCell.Value, SearchString, vbTextCompare) Then ActiveSheet.Range(Cells(aCell.Address, 1), Cells(aCell.Address, 6)).Copy End If Next aCell End Sub
  • 谢谢,你能把代码放在你帖子中的原始问题下吗?
  • 我已经更改了代码。现在它会将复制的矩阵粘贴到您打开的 Word 文档中。
  • 非常感谢@takanuva15,但我收到编译器错误“未定义用户定义的类型”。在 Dim appWd As Word.Application 位置。我还需要将它粘贴到 word 文件中的特定表中。你能不能朝那个方向思考?你很热心地帮助我。
  • 您应该能够通过将 Word 对象引用库添加到您的工作簿来消除该错误。按照说明here 并选中“Microsoft Word 16.0 对象库”框。我将努力将其粘贴到表格中。你能在你的帖子中放一张word文档中表格的截图吗?