【问题标题】:printing of word document through excel通过excel打印word文档
【发布时间】: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


【解决方案1】:

选择打印机

Application.ActivePrinter = Printers(2) ' set index to correct printer in the printers collection

打印命令语句

ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
    IgnorePrintAreas:=False

留言

MsgBox "The sheet was printed."

【讨论】:

  • 感谢这些命令。我愿意按其名称或当前从 excel 打开的 word 文档打印。所以你提到的命令有效?
  • @rehanemis 同样,如果您需要更多帮助而不仅仅是指针,请发布您的代码并在这样做之前阅读this。
  • @rehanemis 此代码无法编译,即使我在工具/参考下添加了Microsoft Word Object Library,所以我假设您在发布之前进行了一些编辑?
  • 还有其他模块代码和表格参考,其实不是小代码行。无论如何,我会在添加/修改代码后回复您。明天分享一下情况。 .
  • 嗨米奇,我们的打印机不接受通过word文档输入,所以我打算将邮件与PDF合并。我正在努力,90% 都在努力。只是更多地测试它。希望它能正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 2014-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多