使用 VBA 从 MS Word 打开 Excel 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7995963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open an Excel document from MS Word Using VBA
提问by JavaBits
I want to open a Excel sheet through Word VBA, paste information from the Word document row wise and save it. I have code for picking up specific information from the Word document but do not know how to open Excel and save it.
我想通过 Word VBA 打开 Excel 工作表,将 Word 文档中的信息逐行粘贴并保存。我有从 Word 文档中获取特定信息的代码,但不知道如何打开 Excel 并保存它。
I have found a solution in the following site: http://www.exceltip.com/st/Control_Excel_from_Word_using_VBA_in_Microsoft_Excel/463.html
我在以下站点找到了解决方案:http: //www.exceltip.com/st/Control_Excel_from_Word_using_VBA_in_Microsoft_Excel/463.html
It is giving an error and also I do not know how to add a reference to the Excel library in Word.
它给出了一个错误,而且我不知道如何在 Word 中添加对 Excel 库的引用。
Error: Dim xlApp As Excel.Application - Compile Error "User defined type not defined"
错误:Dim xlApp As Excel.Application - 编译错误“未定义用户定义的类型”
回答by JimmyPena
You could do one of two things here --
你可以在这里做两件事之一——
1) Change "Excel.Application" and "Excel.Workbook" to "Object" (late bound)
1)将“Excel.Application”和“Excel.Workbook”改为“Object”(后期绑定)
or
或者
2) In the VBA Editor, go to Tools > References and look for "Microsoft Excel x.0 Object Library" and check the checkbox. (early bound)
2) 在 VBA 编辑器中,转到工具 > 引用并查找“Microsoft Excel x.0 对象库”并选中复选框。(早期绑定)
The 'x' depends on your Excel version. 11.0 is Office 2003, 12.0 is Office 2007, and so on.
“x”取决于您的 Excel 版本。11.0 是 Office 2003,12.0 是 Office 2007,依此类推。
回答by macropod
Your exceltip link seems to be bad, so it's impossible to know what other changes might be needed if you move to late binding; setting a VBA reference to the Excel Object library would be the simplest solution. For an implementation on StackOverflow that you might adapt, see: VBA code that copies tables from multiple Word files to separate worksheets in excel Excel, naming the worksheet the name of the Word doc?
您的 exceltip 链接似乎很糟糕,因此如果您转向后期绑定,则不可能知道可能需要进行哪些其他更改;设置对 Excel 对象库的 VBA 引用将是最简单的解决方案。有关您可能采用的 StackOverflow 上的实现,请参阅:将表格从多个 Word 文件复制到 Excel Excel 中的单独工作表的 VBA 代码,将工作表命名为 Word 文档的名称?