从VBA Excel文件打开一个单词模板,输入一个数字并打印 [英] From a VBA Excel file open a word template, enter a number and print

查看:271
本文介绍了从VBA Excel文件打开一个单词模板,输入一个数字并打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我已经在问题/解决方案中获得了很大的成功,并在excel vba中获得了很多知识,但是我再次遇到了一个不同的问题。



这是我的场景:



我们在excel vba电子表格中进行序列化和跟踪的示例。可以添加多行,并通过序列号标识。发送的每个样本都包含序列化的信息表。现在一旦我们将序列号输入到excel中,我们必须手动输入序列号到信息表,打印和重复。因为有时候我们必须一次发送40个样本,这样就会变得截然不同。



我的理想解决方案:



我想采取用户形式我已经到位数据输入和使用一个列表框,用于记录输入到电子表格的所有样本。从此,如果用户决定打印纸张,他可以按命令按钮运行宏。此宏将采用列表框项目(一次一个),并将信息输入到单词模板文件中的特定部分,然后打印。冲洗并重复。



我真正问的是:
任何人都可以提供通用代码,用于打开excel中的单词文件,在指定区域中输入一行,然后打印它。我现在知道一些VBA,但这是有点超出了我的联赛,有一点哇就出来了。



编辑:通过指定,我想选择选择列表框中的文本放在工作表上的位置。理想情况下,右上角是右上角,右边是右上角。但是我需要调整位置使其完美!
感谢您的帮助,
A。

解决方案

请尝试使用以下代码。我在里面添加了一些意见来解释你需要改变什么。



重要!使用代码中提出的选项1或选项2.

  Sub OpenWord_Place_Some_Text()

Dim WRD As Object
设置WRD = CreateObject(Word.Application)

'可选行查看Word应用程序
WRD.Visible = True

Dim DOC As Object
'选项1.对于新的空文档...:
设置DOC = WRD.Documents.Add

'或...
'选项2.对于现有文档
设置DOC = WRD.Documents.Open(C:\你的文件夹\你的文档.docx)

'文档使用TextBox
设置基于实验的LEFT,TOP,WIDTH,HEIGHT参数
使用DOC.Shapes.AddTextbox(msoTextOrientationHorizo​​ntal,Left:= 100,Top:= 100,Width:= 100 ,高度:= 100)
.Line.Visible = msoFalse
.TextFrame.TextRange.Text =您的文本
结束

'设置活动打印机一个你使用她e
WRD.ActivePrinter =PDFCreator
'打印文档
DOC.PrintOut
'关闭文档不保存
DOC.Close False
'关闭应用程序
WRD.Quit
设置WRD =没有

结束子


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.

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.

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. I know some VBA now, but this is a bit out of my league, and by a bit waaaay out of it.

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.

解决方案

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.

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

这篇关于从VBA Excel文件打开一个单词模板,输入一个数字并打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆