vba - 打印为 PDF 时从 Excel 工作表重命名 Word 宏
问题描述
我已经设置了一个邮件合并,以根据 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
解决方案
你什么都不说,我必须离开我的办公室......
请尝试下一个能够定义 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”等的文档......
推荐阅读
- javascript - 我可以在 .ajaxStart 和 .ajaxStop 函数之间写什么来显示加载栏,比如 3 秒?
- mysql - 在 SQL 嵌套选择中找不到语法错误
- r - 将“聚类函数”应用于一系列线性模型
- python - 在 Django/Python 中,我将一个对象 (A) 与另一个对象 (B) 的两个实例相关联,因此如果我调用 B.A_set.all(),它适用于 B 的任一实例
- javascript - 将 Instana 错误跟踪集成到 Angular 2 应用程序中
- android - viewModelScope.launch(Dispatchers.IO) 用途
- java - 插入@OneToMany 关系对象与子表中未插入父ID 的子表?
- android - BiometricPrompt:如何加密几个字符串?
- ruby-on-rails - Ruby on Rails + Carrierwave:为什么我的显示页面上的 image_tag 返回错误?
- java - ARROW_DOWN 键在 selenium 中不起作用( Key Down / Up 事件仅对修饰键有意义。)