我试图在excel-VBA中创建目录,然后将xlsm和pdf文件复制到此目录中。
我可以创建目录,但我似乎无法将文件保存到此目录中?
以下代码。任何帮助深表感谢。这真让我抓狂。我正在做的就是连接文件名并将其保存到c:\ temp文件夹,但是我希望它将它保存到c:\ temp
中的子文件夹中Sub Macro2()
'
' Macro2 Macro
'
Dim FileName As String
Dim FileName2 As String
'FileName3 As String
FileName = Sheet1.TextBox1.Text
FileName2 = ("C:\TEMP\" & FileName)
'CheckDir As String
MsgBox (FileName2)
MkDir (FileName2)
ChDir (FileName2)
ActiveWorkbook.SaveAs FileName:=FileName2 & FileName & "2xlsm.xlsm" _
, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
FileName2 & "FileName" & "_2xlsm.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End Sub
答案 0 :(得分:0)
& "\" &
FileName2 & "\" & FileName, _
(反斜杠)
请参阅完整代码
Option Explicit
Sub Macro2()
'
' Macro2 Macro
'
Dim FileName As String
Dim FileName2 As String
FileName = Sheet1.TextBox1.Text
FileName2 = ("C:\TEMP\" & FileName)
MsgBox (FileName2)
MkDir (FileName2)
ActiveWorkbook.SaveAs FileName:= _
FileName2 & "\" & FileName, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, _
CreateBackup:=False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
FileName2 & "\" & "FileName", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
或者只是这样做
FileName2 = ("C:\TEMP\" & FileName & "\")
答案 1 :(得分:0)
应修复FileName2
的格式,以便在文件创建中正确连接FileName
。此外,假设您希望将XLSM和PDF文件保存在一个目录中,FileName
和SaveAs
的{{1}}参数应该相同。
请参阅以下修改后的代码:
ExportAsFixedFormat