【发布时间】:2023-04-09 13:44:02
【问题描述】:
我通过 vba 代码设置邮件合并,该代码使用 Excel 将数据输入到 MS Word 文件中。我想将打印选项从 excel 添加到附加的打印机。假设有 5 个字母需要打印我希望当打印一个字母时,将在 5 秒后给出第二个字母命令。
想直接从 excel(而不是 word)和特定打印机(如 HP1100 等)打印。
有什么建议吗?
这里是代码
Sub CreateWordDocuments()
Dim CustRow, CustCol, LastRow, TemplRow, FrDays, ToDays As Long
Dim DocLoc, TagName, TagValue, TemplName, FileName, PrintStatus As String
Dim CurDt, LastAppDt As Date
Dim WordDoc, WordApp, OutApp, OutMail As Object
Dim WordContent As Word.Range
With Sheet1
If .Range("B3").Value = Empty Then
MsgBox "Please select a correct template from the drop down list"
.Range("g3").Select
Exit Sub
End If
TemplRow = .Range("B3").Value 'Set Template Row
TemplName = .Range("G3").Value 'Set Template Name
'FrDays = .Range("L3").Value 'Set From Days
'ToDays = .Range("N3").Value 'Set To Days
DocLoc = Sheet2.Range("F" & TemplRow).Value 'Word Document Filename
'Open Word Template
On Error Resume Next 'If Word is already running
Set WordApp = GetObject("Word.Application")
If Err.Number <> 0 Then
'Launch a new instance of Word
Err.Clear
'On Error GoTo Error_Handler
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True 'Make the application visible to the user
End If
LastRow = .Range("c400").End(xlUp).Row 'Determine Last Row in Table
For CustRow = 8 To LastRow
PrintStatus = .Range("d" & CustRow).Value
If PrintStatus = "Ready" Then
Set WordDoc = WordApp.Documents.Open(FileName:=DocLoc, ReadOnly:=False) 'Open Template
For CustCol = 4 To 19 'Move Through 9 Columns
TagName = .Cells(7, CustCol).Value 'Tag Name
TagValue = .Cells(CustRow, CustCol).Value 'Tag Value
With WordDoc.Content.Find
.Text = TagName
.Replacement.Text = TagValue
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll 'Find & Replace all instances
End With
Next CustCol
.Range("d" & CustRow).Value = "Done"
FileName = ThisWorkbook.Path & "\" & .Range("c" & CustRow).Value & "_" & .Range("i" & CustRow).Value & ".docx"
WordDoc.SaveAs FileName
.Range("d" & CustRow).Value = "Done"
' WordDoc.Close
End If
Kill (FileName) 'Deletes the PDF or Word that was just created
Next CustRow
WordApp.Quit
End With
End Sub
【问题讨论】:
-
所以这里有很多,从头开始编写所有这些需要相当多的编码。请先发布您拥有的代码并说明您遇到的具体问题。
-
只想知道以下几点: 1. vba 代码发送打印命令到特定打印机 2. 打印机完成打印时的消息框。请问这是否有意义或仍然需要查看代码。
-
下面我的回答应该可以帮助您入门。
标签: excel printing mailmerge vba