这是VBA的所有新手,我可以看到优点,希望大家能提供帮助。 我有一个具有打印范围的excel工作表,我想将它作为PDF导出到树中3个级别的新文件夹中(Windows不是mac),我设法找到了要另存为PDF的信息,并从一个单元格中选择了一个名称,但是我很难建立文件夹的路径((有3个单元格,全部来自VLOOKUP的单元格,它们没有格式或特殊字符控制,因此需要进行一些过滤)检查是否还需要考虑文件夹的存在。
所以: 单元格a1文件名
单元b1文件夹级别1(位于C \ users \ xxxx \ my documents \
下的1级)单元格c1文件夹级别2
单元d1文件夹级别3被命名为文件形式a1,另存为pdf
pdf仅需要打印1,3,5页(我仍然无法解决) 预先感谢
Sub SaveAsToPDF()
Dim Path1 As String
Dim Path2 As String
Dim wbA As Workbook
Dim wsA As Worksheet
Dim myfilename As String
Dim fpathname As String
Dim strTime As String
strTime = Format(Now(), "_ddmmyyyy\_hhmm")
userpath1 = "C:\\users\xxxx\documents\working_folders\"
GCDPath2 = Range("o5") ' Group Code Description path for the file to be saved
SUBPath3 = Range("o6") 'sub Group Code Description path for the file to be saved
myfilename = Range("o7") 'file name to be saved sometime has ,.() in text
fpathname = Path1 & "\" & myfilename & strTime & ".pdf"
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
Clean = Replace(Clean, Chr(10), " ")
Clean = Replace(Clean, Chr(13), " ")
Clean = Replace(Clean, Chr(13) & Chr(10), " ")
If Len(Dir("c:\users\xxxx\documents\working_folders\" & GCDPath2 & "\" & SUBPath3, vbDirectory)) = 0 Then
MkDir "c:\users\xxxx\documents\working_folders\test\" & GDCPath2 & "\" & SUBPath3
MsgBox "Made NEW Folder"
Else
MsgBox "Found Folder"
End If
MsgBox "You are trying to save the file to:" & vbCrLf & fpathname
If myfilename <> "False" Then
wsA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=fpathname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
From:=1, _
To:=1 _
'OpenAfterPublish:=False _ 'this appears not to work so i disabled it
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myfilename
End If
End Sub