创建文件夹如果不存在,则使用VBA在Windows操作系统中工作,但在MAC操作系统上不工作

创建文件夹如果不存在,则使用VBA在Windows操作系统中工作,但在MAC操作系统上不工作,vba,macos,directory,pdf-generation,create-directory,Vba,Macos,Directory,Pdf Generation,Create Directory,我需要从以下代码中导出Excel工作表“模块”中的PDF: 链接此处: 上述代码在获得起始行和结束行的编号后将调用GeneratePDF Sub GeneratePDF() Dim wsA As Worksheet Dim wbA As Workbook Dim strTime As String Dim strName As String Dim strPath As String Dim strFile As String Dim strPathFile As String Dim myF

我需要从以下代码中导出Excel工作表“模块”中的PDF:

链接此处:

上述代码在获得起始行和结束行的编号后将调用GeneratePDF

Sub GeneratePDF()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Dim VelleName As String
Dim SelectedRange As Range
With ThisWorkbook.Worksheets("modulo")
.Activate
.Range(.Cells(1, 1), .Cells(33, 10)).Select
Selection.Name = "SelectedRange"
End With


On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = ActiveWorkbook.Worksheets("modulo")
strTime = Format(Now(), "ddmmyyyy\_hhmmss")

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & Application.PathSeparator


VelleName = ThisWorkbook.Worksheets("database").Range("E" & i) & ThisWorkbook.Worksheets("database").Range("B" & i) & "_" & ThisWorkbook.Worksheets("database").Range("C" & i)

'replace spaces and periods in sheet name
strName = Replace(VelleName, " ", "_")
strName = Replace(strName, ".", "_")
strName = Replace(strName, "-", "_")
strName = Replace(strName, "/", "_")

'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"

strPathFile = strName & "_" & strTime


'select folder for file
If Dir(strPath & Application.PathSeparator & "forme", vbDirectory) = "" Then '<== check if folder exists
   MkDir ThisWorkbook.Path & Application.PathSeparator & "forme" '<== Create Folder
End If
myFile = ThisWorkbook.Path & Application.PathSeparator & "forme" & Application.PathSeparator & strPathFile
'myFile = ThisWorkbook.Path & Application.PathSeparator & strPathFile


'export to PDF if a folder was selected
If myFile <> "False" Then
With wsA.PageSetup
    .Orientation = xlPortrait
    .PrintArea = "SelectedRange"
    .Zoom = False
    .FitToPagesTall = 1
    .FitToPagesWide = 1
End With
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
        
End If

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Impossibile creare il file pdf"
    Resume exitHandler
End Sub
Sub-GeneratePDF()
将wsA设置为工作表
将wbA设置为工作簿
作为字符串的Dim strTime
将strName设置为字符串
将strPath设置为字符串
作为字符串的Dim strFile
将strPathFile设置为字符串
Dim myFile作为变量
暗淡的丝绒名称为字符串
Dim SelectedRange作为范围
使用此工作簿。工作表(“模”)
.激活
.Range(.Cells(1,1),.Cells(33,10))。选择
Selection.Name=“SelectedRange”
以
关于错误转到错误处理程序
设置wbA=ActiveWorkbook
设置wsA=ActiveWorkbook.Worksheets(“模”)
strTime=格式(现在(),“ddmmyyyy\\u hhmmss”)
'获取活动工作簿文件夹(如果已保存)
strPath=wbA.Path
如果strPath=“”,则
strPath=Application.DefaultFilePath
如果结束
strPath=strPath&Application.PathSeparator
VelleName=ThisWorkbook.Worksheets(“数据库”).Range(“E”和i)和ThisWorkbook.Worksheets(“数据库”).Range(“B”和i)和“\UK”和ThisWorkbook.Worksheets(“数据库”).Range(“C”和i)
'替换图纸名称中的空格和句点
strName=Replace(VelleName,“,”替换)
strName=Replace(strName,“.”,“”)
strName=替换(strName,“-”,“”)
strName=Replace(strName,“/”,“”)
'为savng文件创建默认名称
strFile=strName&“&&strTime&.pdf”
strPathFile=strName&“&”strTime
'为文件选择文件夹
如果Dir(strPath&Application.PathSeparator&“forme”,vbDirectory)=“那么”
Sub GeneratePDF()
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Dim VelleName As String
Dim SelectedRange As Range
With ThisWorkbook.Worksheets("modulo")
.Activate
.Range(.Cells(1, 1), .Cells(33, 10)).Select
Selection.Name = "SelectedRange"
End With


On Error GoTo errHandler

Set wbA = ActiveWorkbook
Set wsA = ActiveWorkbook.Worksheets("modulo")
strTime = Format(Now(), "ddmmyyyy\_hhmmss")

'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
  strPath = Application.DefaultFilePath
End If
strPath = strPath & Application.PathSeparator


VelleName = ThisWorkbook.Worksheets("database").Range("E" & i) & ThisWorkbook.Worksheets("database").Range("B" & i) & "_" & ThisWorkbook.Worksheets("database").Range("C" & i)

'replace spaces and periods in sheet name
strName = Replace(VelleName, " ", "_")
strName = Replace(strName, ".", "_")
strName = Replace(strName, "-", "_")
strName = Replace(strName, "/", "_")

'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"

strPathFile = strName & "_" & strTime


'select folder for file
If Dir(strPath & Application.PathSeparator & "forme", vbDirectory) = "" Then '<== check if folder exists
   MkDir ThisWorkbook.Path & Application.PathSeparator & "forme" '<== Create Folder
End If
myFile = ThisWorkbook.Path & Application.PathSeparator & "forme" & Application.PathSeparator & strPathFile
'myFile = ThisWorkbook.Path & Application.PathSeparator & strPathFile


'export to PDF if a folder was selected
If myFile <> "False" Then
With wsA.PageSetup
    .Orientation = xlPortrait
    .PrintArea = "SelectedRange"
    .Zoom = False
    .FitToPagesTall = 1
    .FitToPagesWide = 1
End With
    wsA.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:=myFile, _
        Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, _
        IgnorePrintAreas:=False, _
        OpenAfterPublish:=False
        
End If

exitHandler:
    Exit Sub
errHandler:
    MsgBox "Impossibile creare il file pdf"
    Resume exitHandler
End Sub