excel - 使用 Word VBA 将特定的 Excel 单元格值拉入 Word 文档

标签 excel vba ms-word

我是 VBA 和宏的新手。

我得到了从 Excel 中复制数据并将其粘贴到 word 文档中特定位置的重复任务。

比如我的excel表中有这样的数据:

<表类="s-表"> <头> 第 1 列 第二列 <正文> ID_1 我是_One ID_2 我是_Two ID_3 我三岁

现在我正在寻找 Word 宏

  1. 获取单元格位置为 3 的 Word 表格中的文本
  2. 在 Excel Col1 中查找相同的文本
  3. 从Excel中获取Col2的值
  4. 将 Col2 的值粘贴到单元格位置 10 的单词表中
  5. 对 Word 文档中的另一个表格重复相同的过程

[更新] 我尝试通过谷歌搜索使用多个代码片段,但无法构建工作宏。

Sub pull_from_Excel2()
    'ref: https://www.macworld.com/article/211753/excelwordvisualbasic.html
    
    Dim Month As String
    
    ID_Range = "A2:A6"     'Select this as range like "A2:A16"
    Offset_to_fetch = 1         'Select this to fetch comments etc. value starts with
    
    Set xlSheet = GetObject("D:\Excel.xlsx")
    
     
    'Snippets:
    'Debug.Print VarType(xlSheet.Worksheets("Sheet1").Range("A3:A5").Value)
    '8204
    
    Dim Cell As Range, rng As Range
    
    Debug.Print VarType(xlSheet.Worksheets("Sheet1").Range(ID_Range).Value2)
   
    Set rng = xlSheet.Worksheets(1).Range(ID_Range)
    
    For Each Cell In rng
        Debug.Print Cell.Text
    Next Cell
  
End Sub

我使用这个 url 来构建我的骨架代码:https://www.macworld.com/article/211753/excelwordvisualbasic.html

当我尝试从 excel 中的单元格区域中获取值时,出现以下代码错误。

Set rng = xlSheet.Worksheets(1).Range(ID_Range).Value2

上述行在运行时出现“Object required”错误。

Set rng = xlSheet.Worksheets(1).Range(ID_Range)

上述行在运行时出现“Type Mismatch”错误。 注意:对于此错误,我尝试使用 for each 循环,因为这是数组,但在执行 for 循环之前显示错误。

请提供帮助。

最佳答案

我建议使用 Option Explicit 并正确声明所有变量。这样您就不太可能遇到看不见的错误。

To activate it for all new codes that you add in the future, you can activate it directly in Excel and Word. This is a good practice and will protect you from doing it wrong by notifying you of not declared variables:

In the VBA editor go to ToolsOptionsRequire Variable Declaration.

This will add Option Explicit to new modules only. In existing modules Option Explicit needs to be added manually as first line.

此外,我强烈建议根据变量包含的内容来命名变量,否则它会变得非常困惑。您将变量命名为 xlSheet,但将工作簿而不是工作表加载到其中。

下一个问题是您的代码在 Word 中,如果您声明 rng As Range 那么这是 Word.Range 类型而不是 Excel.Range 这些是不同的类型,所以这就是为什么您会收到 “类型不匹配” 错误。 要解决此问题,您可以在 Word VBA 中转到 ExtrasRefereces … 并设置对 Excel 库的引用,以便您可以将变量声明为 Dim xlRng As Excel。 Range 或者如果您没有设置引用,则将其声明为 ObjectVariant,如下例所示:

' This code is in Word!

Option Explicit

Public Sub pull_from_Excel2()
    'declare constants
    Const ID_Range As Sting = "A2:A6"     'Select this as range like "A2:A16"
    Const Offset_to_fetch As Long = 1         'Select this to fetch comments etc. value starts with
    
    Dim xlWorkbook As Object
    Set xlWorkbook = GetObject("D:\Excel.xlsx") 'This expects the Excel to be already open! If not open you need to use CreateObject("Excel.Application")
    
    Dim xlRng As Object
    Set xlRng = xlWorkbook.Worksheets(1).Range(ID_Range)
    
    Dim xlCell As Object
    For Each xlCell In xlRng 
        Debug.Print xlCell.Text
    Next xlCell 
End Sub

请注意,如果您的工作簿 Set xlWorkbook = GetObject("D:\Excel.xlsx") 未在 Excel 中打开,您需要使用 CreateObject("Excel.Application") 并打开它。

Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")

Dim xlWorkbook As Object
Set xlWorkbook = xlApp.Workbooks.Open(FileName:="D:\Excel.xlsx") 'will open the workbook
xlApp.Visible = True 'make it false to open Excel invisible in the background

'your code here …

'in the end close workbook and Excel (espaciall if you had it invisible!)
xlWorkbook.Close SaveChanges:=False  
xlApp.Quit 'close Excel

关于excel - 使用 Word VBA 将特定的 Excel 单元格值拉入 Word 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67489997/

相关文章:

c# - 密码保护的 OpenXml Word 文档重新保存为密码保护的二进制 Word

c# - 如何通过OleDB打开存储在byte[]中的Excel文件?

javascript - 将带有换行符的值导出到 Excel 中的单个单元格中。 jQuery 数据表

vba - VBA 中的 SUMIF 函数无法正常工作

vba - 操作 11 列的列表框

powershell - 以编程方式访问 Word 2007 文档的文档属性

c# - 在 Windows 窗体或 WPF 应用程序中使用集成服务项目?

excel - 从 Windows Scripting Host 操作 Excel 文件

excel - 在Excel中执行嵌入的.exe对象

ms-word - 如何将参数传递给 Microsoft Word 宏?