在名为htmltab
的文件中,我有以下代码,除其他外,填写表格中定义的每个部分的表格,然后保存包含名称中的部分的pdf文件,命名为FILE
。
FILEPART.pdf
我的问题是我得到一个文件,而不是多个名称不同的文件。
一步一步地循环我看到它首先创建'parts
lastRow = Sheets("overview").Range("G1000").End(xlUp).Row
lastRowHere = Sheets("Source").Range("A13:A1000").End(xlUp).Row
'count of parts
Count = lastRowHere + 2
For m = 1 To Count
For n = 14 To lastRow
Partname = Sheets("overview").Range("C" & n) &
_" of " & Sheets("overview").Range("A" & n)
[...]
Next n
'creates the PDF file for mapping on each part
Set FSO = CreateObject("Scripting.FileSystemObject")
s(0) = ThisWorkbook.FullName
If FSO.FileExists(s(0)) Then
'//Change Excel Extension to PDF extension in FilePath
s(1) = FSO.GetExtensionName(s(0))
If s(1) <> "" Then
s(1) = "." & s(1)
sNewFilePath = Replace(s(0), s(1), Partname & ".pdf")
'//Export to PDF with new File Path
lastPart = Sheets("table").Cells(1000, m * 5 + 1).End(xlUp).Row
Sheets("table").Range(Cells(1, m * 5 + 1), Cells(lastPart, m * 5 + 5)).
_ExportAsFixedFormat Type:=xlTypePDF, FileName:=sNewFilePath,
_Quality:=xlQualityStandard, IncludeDocProperties:=True,
_IgnorePrintAreas:=False, OpenAfterPublish:=False
End If
Else
'//Error: file path not found
MsgBox "Error: this workbook may be unsaved. Please save and try again."
End If
Set FSO = Nothing
Next m
,然后将其替换为FILEPART1.pdf
,仅以FILEPART2.pdf
结尾
答案 0 :(得分:0)
此版本的代码(在SJR的评论之后)解决了问题
'parts
lastRow = Sheets("overview").Range("G1000").End(xlUp).Row
lastRowHere = Sheets("Source").Range("A13:A1000").End(xlUp).Row
'count of parts
Count = lastRowHere + 2
s(0) = ThisWorkbook.FullName
For m = 1 To Count
PartNameSource = Sheets("Source").Range("A" & m + 13)
For n = 14 To lastRow
PartnameLine = Sheets("overview").Range("C" & n) &
_" of " & Sheets("overview").Range("A" & n)
[...]
Next n
'creates the PDF file for mapping on each part
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(s(0)) Then
'//Change Excel Extension to PDF extension in FilePath
s(1) = FSO.GetExtensionName(s(0))
If s(1) <> "" Then
s(1) = "." & s(1)
sNewFilePath = Replace(s(0), s(1), PartnameSource & ".pdf")
'//Export to PDF with new File Path
lastPart = Sheets("table").Cells(1000, m * 5 + 1).End(xlUp).Row
Sheets("table").Range(Cells(1, m * 5 + 1), Cells(lastPart, m * 5 + 5)).
_ExportAsFixedFormat Type:=xlTypePDF, FileName:=sNewFilePath,
_Quality:=xlQualityStandard, IncludeDocProperties:=True,
_IgnorePrintAreas:=False, OpenAfterPublish:=False
End If
Else
'//Error: file path not found
MsgBox "Error: this workbook may be unsaved. Please save and try again."
End If
Set FSO = Nothing
Next m
<强> REMARK 强>
如果PartNameSource
包含使文件名无效的字符(例如/
),则会有run-time error 1004 Document not saved. The document may be open, or an error may have been encountered when saving