简体   繁体   English

从VBA Excel文件中打开一个单词模板,输入一个数字并打印

[英]From a VBA Excel file open a word template, enter a number and print

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. 我们在excel vba电子表格中进行序列化和跟踪的样本。 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. 现在,一旦我们将序列号输入excel,我们必须在信息表上手动输入序列号,打印并重复。 This gets frusterating as sometimes we have to send upwards of 40 samples out at a time. 这有点令人费解,因为有时候我们必须一次发送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. 冲洗并重复。

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. 我真正要问的是:任何人都可以提供从excel打开word文件,在指定区域输入一行,然后打印它的通用代码。 I know some VBA now, but this is a bit out of my league, and by a bit waaaay out of it. 我现在知道一些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。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM