从 Excel 打开 Word,传递变量和文本(VBA 宏),远程控制 Word
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21271680/
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 Word from Excel, pass variables and text (VBA macro), remote-control Word
提问by user1579166
I'm completely new to VBA, especially across different programs, but I have created myself a little real-world task that quickly got complicated, someone could give me some pointers or code snippets to try out to achieve the following:
我对 VBA 完全陌生,尤其是在不同的程序中,但我为自己创建了一个很快变得复杂的小现实任务,有人可以给我一些指示或代码片段来尝试实现以下目标:
I have an Excel file that's filled with names and numbers (see below) and I would like to transfer them individually to a Word document.
我有一个包含姓名和数字的 Excel 文件(见下文),我想将它们单独传输到 Word 文档。
If I have highlighted cell A2 and click on [BUTTON], I want Word to open automagically and type out something like
如果我突出显示了单元格 A2 并单击 [BUTTON],我希望 Word 自动打开并输入类似的内容
--"Hi Mike, your current amount is $12.37and you live in 23 One Street. Thanks."--
--“嗨,迈克,你目前的金额是12.37 美元,而且你住在 23 One Street。谢谢。”--
The amount should be printed in bold, and after that Word should save the file and close itself without further input needed.
金额应以粗体打印,然后 Word 应保存文件并自行关闭,无需进一步输入。
Similarly, when I have selected A3, it should open another document, write the same text but with Julia's variables filled in, save it to a specified location and close.
同样,当我选择 A3 时,它应该打开另一个文档,编写相同的文本,但填充了 Julia 的变量,将其保存到指定位置并关闭。
A B C
1 Name Address Amount
2 Mike 23 One Way .37
3 Julia 3949 Street .23
[BUTTON]
So essentially, I guess, I'm trying to "remote-control" Word from within Excel and feed some variables from Excel into Word. I am at a complete loss how to do that, to be honest.
所以基本上,我想,我正在尝试从 Excel 中“远程控制” Word,并将 Excel 中的一些变量提供给 Word。老实说,我完全不知道该怎么做。
What I have found so far is this:
到目前为止我发现的是:
Dim wdApp As Word.Application, wdDoc As Word.Document
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then 'Word isn't already running
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
Set wdDoc = wdApp.Documents.Open("C:\temp\[NAME AFTER SELECTED FIELD A2].docx")
wdApp.Visible = True
...
Now I don't know what to do! How to pass the standard text with the variables from the row of the selected field over to Word? How to format them (bold, Arial, red, etc.)? How to save under the specified filename?
现在我不知道该怎么办!如何将带有变量的标准文本从所选字段的行传递给 Word?如何格式化它们(粗体、Arial、红色等)?如何保存在指定的文件名下?
Is this even possible to do? I know VBA is very powerful, so I hope you can help me out! I'm using Office 2013, so any caveats related to macro programming or VBA language should take that into account.
这甚至可以做到吗?我知道 VBA 非常强大,所以我希望你能帮助我!我使用的是 Office 2013,因此任何与宏编程或 VBA 语言相关的警告都应考虑在内。
Thank you so much!
非常感谢!
回答by gNerb
Don't use get/create object. My macros are all coded as follows:
不要使用获取/创建对象。我的宏全部编码如下:
dim wdApp as New Word.Application 'Always use a new instance
Set wdDoc = wdApp.Documents.Open("C:\temp\[NAME AFTER SELECTED FIELD A2].docx")
wdApp.visible = true
From there you simply work as you would in word. Passing variables is pretty simple as the datatypes exist on both sides (double, integer, string etc are all included in the standard VBA libraries that are included by default so no need to add references). So if I want to tell word to add a paragraph:
从那里你只需像在文字中一样工作。传递变量非常简单,因为数据类型存在于两侧(双精度、整数、字符串等都包含在默认包含的标准 VBA 库中,因此无需添加引用)。所以如果我想告诉 word 添加一个段落:
wdDoc.paragraphs.add
wdDoc.paragraphs(wdDoc.paragraphs.count).range.text = "Hello World!"
Want to add text from a specific range?
想要添加特定范围内的文本?
dim xlRange as Excel.Range
dim wdApp as New Word.Application 'Always use a new instance
Set wdDoc = wdApp.Documents.Open("C:\temp\[NAME AFTER SELECTED FIELD A2].docx")
wdApp.visible = true
set xlRange = activesheet.range("A1") 'Just as an example
wdDoc.paragraphs.add
wdDoc.paragraphs(wdDoc.paragraphs.count).range.text = xlRange.value
Finally, say you want to use a variable name form excel to open a specified word document:
最后,假设你想用一个变量名表单excel打开一个指定的word文档:
dim name as string
name = selection.cells(1).value 'Assuming they selected the cell
dim wdApp as New Word.Application 'Always use a new instance
Set wdDoc = wdApp.Documents.Open("C:\temp\" & name & ".docx")
wdApp.visible = true
I know this is a year late, but hey, hope it helps.
我知道这晚了一年,但是嘿,希望它有所帮助。
As a final note: Use exit sub just prior to end sub in the word macro. Exit sub returns to caller where as end sub does not.
最后一点:在单词宏中的 end sub 之前使用 exit sub。Exit sub 返回给调用者,而 end sub 没有。
sub test
'do the word stuff
exit sub 'Stop it short of end sub
end sub
回答by simon at rcl
You could do this but it would be quite difficult.
你可以这样做,但这会非常困难。
Much easier is to use your spreadsheet from Work.
使用 Work 中的电子表格要容易得多。
In Word (2013) go to the Design ribbon, and use the Start Mail Merge wizard. The source of addresses will be your spreadsheet. I think this will be much easier than what you are planning.
在 Word (2013) 中,转到“设计”功能区,然后使用“开始邮件合并”向导。地址的来源将是您的电子表格。我认为这比您计划的要容易得多。
Cheers -
干杯——