从 Word 调用 Excel VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8087617/
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
Call Excel VBA from Word
提问by jneasy
We have a standard form in MS Word to manually fill in details and launch a macro. This macro will open a MS Excel register to auto populate it from the fields in the Word document whilst doing other things.
我们在 MS Word 中有一个标准表单,可以手动填写详细信息并启动宏。这个宏将打开一个 MS Excel 寄存器,在做其他事情的同时从 Word 文档中的字段自动填充它。
The current solution has a keyboard shortcut for the macro in the Excel spreadsheet and the Word document macro sends the keys strokes to the opened Excel spreadsheet to launch the macro. This only works for a few people in the office.
当前的解决方案为 Excel 电子表格中的宏提供了一个键盘快捷键,Word 文档宏将击键发送到打开的 Excel 电子表格以启动宏。这仅适用于办公室中的少数人。
Can I call my Excel macro directly from the Word macro while my Excel spreadsheet is in focus or is there a Windows setting that is blocking my send keys to launch the macro?
当我的 Excel 电子表格处于焦点时,我是否可以直接从 Word 宏调用我的 Excel 宏,或者是否有 Windows 设置阻止了我的发送键启动宏?
回答by devuxer
This is just a hunch, but would it be possible to migrate the Excel macro from the Excel spreadsheet to the Word document? Then you could add a bit of code to launch the correct Excel spreadsheet and perform the necessary field manipulations. So, basically, let your Word document do all the work on the Excel spreadsheet.
这只是一种预感,但是否可以将 Excel 宏从 Excel 电子表格迁移到 Word 文档?然后您可以添加一些代码来启动正确的 Excel 电子表格并执行必要的字段操作。因此,基本上,让您的 Word 文档在 Excel 电子表格上完成所有工作。
Alternately, you could follow the procedure discussed here:
或者,您可以按照此处讨论的程序进行操作:
http://support.microsoft.com/kb/177760
http://support.microsoft.com/kb/177760
Here is the relevant code listing:
这是相关的代码清单:
Sub XLTest()
Dim XL as Object
Set XL = CreateObject("Excel.Application")
XL.Workbooks.Open "C:\My Documents\ExcelFile.xls"
' If there is more than one macro called TestMacro,
' the module name would be required as in
'
' XL.Run "Module1.TestMacro"
'
' to differentiate which routine is being called.
'
XL.Run "TestMacro"
End Sub