保存为pdf目标问题

时间:2016-07-01 16:37:07

标签: excel vba pdf save-as

我正在从循环函数中保存到pdf导出。我已将file_name设置为:

Sub AutoFill_export2pdf()
'

Dim rowCount As Integer
Dim CurBU As String
Dim CurOPRID As String
Dim CurName As String
Dim CurJournalID As String
Dim CurJournalDate As String
Dim FILE_NAME As String

Sheets("List").Select

rowCount = ActiveSheet.UsedRange.Rows.count

Set Destsh = ActiveWorkbook.Sheets("Sheet")

For sourceRow = 2 To rowCount

CurOPRID = Range("A" & CStr(sourceRow)) 'OPRID
CurName = Range("B" & CStr(sourceRow)) 'Name
CurBU = Range("C" & CStr(sourceRow)) 'BU
CurJournalID = Range("D" & CStr(sourceRow)) 'Journal ID
CurJournalDate = Range("E" & CStr(sourceRow)) 'Journal Date

FILE_NAME = ActiveWorkbook.Path & "\" & "OTGL_" & "JRNL_" & CurBU & "_" &     CurJournalID & "_" & Format(CurJournalDate, "mm-dd-yyyy") & "_" & ".PDF"
CurName = "*" & CurName & "*"
CurBU = "*" & CurBU & "*"
CurJournalID = "*" & CurJournalID & "*"
CurJournalDate = "*" & CurJournalDate & "*"

Destsh.Range("K27") = CurName
Destsh.Range("D7") = CurBU
Destsh.Range("G7") = CurJournalID
Destsh.Range("I7") = CurJournalDate

On Error GoTo 0

Call SaveAsPDF(Destsh, FILE_NAME)

Sheets("List").Select

Next

End Sub


Public Sub SaveAsPDF(ByVal destSheet As Worksheet, ByVal PDFName As String)


On Error Resume Next
Kill PDFName

destSheet.Activate

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=FILE_NAME,   Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False


End Sub


Sub Autofill()
'

Dim rowCount As Integer
Dim CurBU As String
Dim CurName As String
Dim CurOPRID As String
Dim CurJournalID As String
Dim CurJournalDate As String
Dim FILE_NAME As String



CurName = "*" & CurName & "*"
CurBU = "*" & CurBU & "*"
CurJournalID = "*" & CurJournalID & "*"
CurJournalDate = "*" & CurJournalDate & "*"

Sheets("List").Select

rowCount = ActiveSheet.UsedRange.Rows.count

Set Destsh = ActiveWorkbook.Sheets("Sheet")

For sourceRow = 2 To rowCount

CurOPRID = Range("A" & CStr(sourceRow)) 'OPRID
CurName = Range("B" & CStr(sourceRow)) 'Name
CurBU = Range("C" & CStr(sourceRow)) 'BU
CurJournalID = Range("D" & CStr(sourceRow)) 'Journal ID
CurJournalDate = Range("E" & CStr(sourceRow)) 'Journal Date

FILE_NAME = ActiveWorkbook.Path & "\" & "OTGL_" & "JRNL_" & CurBU & "_" & CurJournalID & "_" & Format(CurJournalDate, "mm-dd-yyyy") & "_" & ".PDF"

Destsh.Range("K27") = CurName
Destsh.Range("D7") = CurBU
Destsh.Range("G7") = CurJournalID
Destsh.Range("I7") = CurJournalDate

On Error GoTo 0

Call SaveAsPDF(Destsh, FILE_NAME)


Sheets("List").Select



Next


End Sub

目标是使用我建立的命名约定导出到特定文件夹。如果你需要整个宏,请告诉我。

2 个答案:

答案 0 :(得分:0)

为了教鱼和分发鱼......

  1. 将选项显式放在最顶层(甚至在第一个子设置之前)。
  2. 打开本地窗口(在VBE中查看)。使用F8逐步执行代码,在本地窗口中查看变量,以确保它们符合您希望它们在脚本中的那一步。
  3. 选项显式将识别您没有的数字变量 声明。 (“未定义变量”)
  4. 修复那些以及出现的任何其他问题。 (当它转到其他潜艇时,它也会发现变量的ID问题。)
  5. 您的问题可能会消失,只需修复明确标识的选项和/或每次点击F8时可能会逐步查看代码并查看本地窗口。
  6. 如果您仍然卡住,请使用更新的代码编辑您的帖子,并解释所发生的任何错误消息和行。如果没有错误解释预期和实际行为。

答案 1 :(得分:0)

上面的代码工作正常,我正在处理的文件是一个保存在临时文件夹中的相同名称的副本,因此所有文件都保存到活动的工作簿路径中......我在监视的文件夹中没有。我现在有大约30000个pdf,从运行宏这么多次,哈哈!感谢所有试图提供帮助的人。