【问题标题】:Word Macro renaming file from excel sheet while printing as PDF打印为PDF时从Excel工作表中重命名Word宏文件
【发布时间】:2021-05-05 10:36:25
【问题描述】:

我已经设置了一个邮件合并,以根据 Excel 表的输入草稿信件。 完成草稿后。我有 Word VBA 代码将每一页打印为 PDF。 目前它要求我手动输入保存文件夹和文件名。如何在下面的代码中合并要从 excelsheet 中选择的文件夹位置和文件名:

Sub Print_letter()
Dim x As Long, StrPrtr As String
StrPrtr = Application.ActivePrinter
Application.ActivePrinter = "microsoft print to pdf"
With ActiveDocument
  For x = 1 To .ComputeStatistics(wdStatisticPages)
    Application.PrintOut PrintToFile:=False, FileName:="", _
      Range:=wdPrintRangeOfPages, Pages:=CStr(x), Item:=wdPrintDocumentWithMarkup, _
      Background:=True, PageType:=wdPrintAllPages, Copies:=1, Collate:=False, _
      PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
  Next x
End With
Application.ActivePrinter = StrPrtr
End Sub

【问题讨论】:

  • 您对使用FileName:=""有什么期望?您是否在 Word VBA 中工作并希望从 excelsheet 中选择“文件名”?您是否希望自动化从 Excel 中获取信息片段,还是什么?
  • @FaneDuru FileName:="Workbook.Sheetname.Cells(F, 2).Value 从范围 F2 开始,应该循环直到数据结束
  • @FaneDuru FileName:="Workbook.Sheetname.Cells(F, 2).Value 。是从 Excel 范围从 F2 开始,并且应该循环直到 F 列中的数据结束
  • 你不知道在做什么?要访问现有的 Excel 会话并设置特定的工作簿,然后是工作表和范围?这是您尝试讨论的问题吗?如果是,并且您在合理的时间内回答,我可以告诉您如何做到这一点......
  • 你能告诉我你有空吗,任何时间窗口和时区

标签: vba ms-word


【解决方案1】:

你什么都不说,我必须离开我的办公室……

请尝试下一个能够定义 Excel 工作簿范围的代码:

'It needs a reference to 'Microsoft Excel ... Object library'
Function getRangefromExcelSession(strWorkbook As String, strSheet As String) As Excel.Range
  Dim Ex As Excel.Application, ws As Worksheet, wb As Workbook, rng As Range
  Dim wbFound As Workbook, Boolfound As Boolean, lastRow As Long
  
   On Error Resume Next
    Set Ex = GetObject(, "Excel.Application")
     If Err.Number = 0 Then
        Err.Clear: On Error GoTo 0
        For Each wb In Ex.Workbooks
            If wb.Name = strWorkbook Then
                Set wbFound = wb
                Boolfound = True
            End If
        Next
     Else
       On Error GoTo 0
       MsgBox "There is not any Excel session open...", vbInformation, "Ups...": Exit Function
     End If
     If Boolfound Then
        Set ws = wbFound.Worksheets(strSheet)
        lastRow = ws.Range("F" & ws.Rows.count).End(xlUp).row
        Set getRangefromExcelSession = ws.Range("F2:F" & lastRow)
     End If
End Function

如果找不到(您的)工作簿/工作表,则不提供错误处理...

这样可以得到必要的范围:

Sub testBetRangeFromExcelSess()
    Dim rng As Excel.Range, cel As Range
    Set rng = getRangefromExcelSession("Your workbook name", "Your sheet name")
    If Not rng Is Nothing Then
        Debug.Print rng.Address
        For Each cel In rng
            Debug.Print cel.Value
        Next
    End IfWindow
End Sub

已编辑:

您修改后的代码应如下所示:

Sub Print_letter()
Dim x As Long, StrPrtr As String
StrPrtr = Application.ActivePrinter
Application.ActivePrinter = "microsoft print to pdf"

Dim rng As Excel.Range
Set rng = getRangefromExcelSession("Workbook name", "sheet name")

With ActiveDocument
  For x = 1 To .BuiltInDocumentProperties("Number of Pages")
    Application.PrintOut PrintToFile:=False, OutputFileName:="C:\" & rng(x, 1).Value & ".pdf", _
      Range:=wdPrintRangeOfPages, Pages:=CStr(x), Item:=wdPrintDocumentWithMarkup, _
      background:=True, PageType:=wdPrintAllPages, Copies:=1, Collate:=False, _
      PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
  Next x
End With
Application.ActivePrinter = StrPrtr
End Sub

第二次编辑

请尝试运行下一个测试代码:

Sub testPrintToPdf_() 
 Dim StrPrtr As String, x As Long
 StrPrtr = Application.ActivePrinter
 Application.ActivePrinter = "microsoft print to pdf"
 
 With ActiveDocument
    For x = 1 To .BuiltInDocumentProperties("Number of Pages")
        Application.PrintOut PrintToFile:=False, OutputFileName:="C:\testdDoc_" & x & ".pdf", _
          Range:=wdPrintRangeOfPages, Pages:=CStr(x), Item:=wdPrintDocumentWithMarkup, _
          Background:=True, PageType:=wdPrintAllPages, Copies:=1, Collate:=False, _
          PrintZoomColumn:=0, PrintZoomRow:=0, PrintZoomPaperWidth:=0, PrintZoomPaperHeight:=0
    Next x
 End With
End Sub

它应该返回 15 个名为“testDoc_1,pdf”、“testDoc_2,pdf”等的文档...

【讨论】:

  • 在保存时出现运行时错误 5174 .says 找不到您的文件。它被删除或移动了? "C:\ABCD.pdf"
  • @user9630941:您是否在没有代码提供的文件名的情况下测试了您的代码,并从 Excel 工作表中获取了值? 它是一段工作代码吗?我只提供了你的问题的答案,因为它已经制定......
  • @user9630941:请尝试更新后的代码。恐怕你用FileName而不是OutputFileName...
  • 哦,好吧,让我试试
  • 像魅力一样工作。!!!非常感谢。这么多的手工工作都消失了。非常感谢您的时间和精力。再次感谢
猜你喜欢
  • 2019-06-16
  • 1970-01-01
  • 2016-07-20
  • 1970-01-01
  • 2013-05-17
  • 2015-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多