我试图将工作表保存为PDF文件到桌面,然后使用CopyFile方法将该文件复制到另一个位置。
我会犯错误
“未找到文件”
或
“路径未找到”
当使用CopyFile方法时,它会在桌面上创建一个文件,然后抛出复制该文件的错误。
在此之前,我尝试了两次创建pdf,一个接一个,它在桌面上创建,并在相关的月度文件夹中创建另一个。
对于这两个变量,我尝试了不同的语法。
Sub Save_Invoice_To_PDF()
Dim Fso As Scripting.FileSystemObject
Dim Invoice As Worksheet
Dim Fname As String
Dim Path1 As String
Dim Path2 As String
Dim PndSign As String
Dim Mth As String
Dim Mth1 As String
Set Invoice = Sheet1
PndSign = Chr(163)
Mth = Invoice.Range("A16").Value
Mth1 = MonthName(Month(Mth))
Path1 = "C:\Users\Peter\Desktop\"
Path2 = "C:\Users\Peter\Documents\Business\Sent Invoices\" & Mth1 & "\"
Fname = Range("C16").Value & " " & Range("A8").Value & " " & _
PndSign & "" & Range("E46").Value _
& " " & Format(Range("A16"), "dd-mm-yyyy") & ".pdf"
Application.ScreenUpdating = False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Path1 & " " & Fname, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Set Fso = New FileSystemObject
Fso.CopyFile Source:="C:\Users\Peter\Desktop\" & Fname, _
Destination:="C:\Users\Peter\Business\Invoices\Sent Invoices\ _
" & Mth1 & "\"
Set Fso = Nothing
Application.ScreenUpdating = True
End Sub我想保存一个pdf到桌面,并将该pdf复制到一个月文件夹中指定的Mth1变量,这是部分从文件Fname变量,即Format(Range("A16"), "dd-mm-yyyy")。
发布于 2021-10-20 10:59:36
您的代码是以这样的方式运行的,它试图在创建文件之前复制它。如果文件存在于特定目录中,则使用休眠和while循环,然后移动下一个命令或等待或休眠。
https://stackoverflow.com/questions/54326972
复制相似问题