从 VBA Excel 文件打开一个单词模板,输入一个数字并打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19962231/
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
From a VBA Excel file open a word template, enter a number and print
提问by Alex
So far I've had great sucess with questions/solutions and obtaining lots of knowledge in excel vba but once again i'm stuck on a different kind of problem.
到目前为止,我在问题/解决方案方面取得了巨大的成功,并在 excel vba 中获得了大量知识,但我再次陷入了不同类型的问题。
Here is my scenario:
这是我的场景:
We do samples that are serialized and tracked in an excel vba spreadsheet. Multiple lines can be added and they are identified by the serial number. Each sample sent contains a information sheet that is also serialized. Right now once we enter the serial number into excel, we have to manually enter the serial number onto the information sheet, print, and repeat. This gets frusterating as sometimes we have to send upwards of 40 samples out at a time.
我们制作在 excel vba 电子表格中序列化和跟踪的样本。可以添加多行,它们由序列号标识。发送的每个样本都包含一个信息表,该信息表也是序列化的。现在一旦我们在excel中输入序列号,我们必须手动将序列号输入到信息表上,打印并重复。这令人沮丧,因为有时我们必须一次发送 40 个以上的样本。
My Ideal Solution:
我的理想解决方案:
I would like to take the userform I have in place for data entry and use a listbox to record all the samples entered to the spreadsheet. From this, if the user decides to print the sheets he can hit a command button to run a macro. This macro would take the listbox items (One at a time) and input the information to a specific part on the word template file, and then print it. Rinse and repeat.
我想使用我现有的用户表单进行数据输入,并使用列表框记录输入到电子表格中的所有样本。由此,如果用户决定打印工作表,他可以点击命令按钮来运行宏。该宏将获取列表框项(一次一个)并将信息输入到 word 模板文件的特定部分,然后打印出来。冲洗并重复。
What I am really asking: Can anyone provide a generic code for opening a word file from excel, inputting a line in a specified area, and then printing it. I know some VBA now, but this is a bit out of my league, and by a bit waaaay out of it.
我真正想问的是:任何人都可以提供一个通用代码,用于从 excel 打开 word 文件,在指定区域输入一行,然后打印它。我现在知道一些 VBA,但这有点超出我的范围,而且有点偏离。
Edit: By specified I ment that I would like to have the option to choose where the text from the list box is placed on the sheet. Ideally it would be the top right hand corner, and middle right hand side. But I would need to adjust the location make it fit perfectly! Thanks for the help, A.
编辑:我指定我希望可以选择将列表框中的文本放置在工作表上的位置。理想情况下,它应该是右上角和中间的右手边。但我需要调整位置使其完美贴合!谢谢你的帮助,A。
回答by Kazimierz Jawor
Try with following code. I added some comments inside to explain what you need to change.
尝试使用以下代码。我在里面添加了一些注释来解释您需要更改的内容。
Important! use either Option 1 or Option 2 as proposed inside the code.
重要的!使用代码中建议的选项 1 或选项 2。
Sub OpenWord_Place_Some_Text()
Dim WRD As Object
Set WRD = CreateObject("Word.Application")
'optional line to see Word applicaton
WRD.Visible = True
Dim DOC As Object
'Option 1. for new empty document...:
Set DOC = WRD.Documents.Add
'or...
'Option 2. for existing document
Set DOC = WRD.Documents.Open("C:\your folder\your document.docx")
'to place your text precisily in word document do it with TextBox
'set your LEFT, TOP, WIDTH, HEIGHT parameters based on experiments
With DOC.Shapes.AddTextbox(msoTextOrientationHorizontal, Left:=100, Top:=100, Width:=100, Height:=100)
.Line.Visible = msoFalse
.TextFrame.TextRange.Text = "YOUR TEXT HERE"
End With
'set active printer to one you use here
WRD.ActivePrinter = "PDFCreator"
'print document
DOC.PrintOut
'close document without saving
DOC.Close False
'close application
WRD.Quit
Set WRD = Nothing
End Sub