简体   繁体   English

excel vba 代码检查文件夹是否存在,如果不存在则创建文件夹

[英]excel vba code to check for a folder if it exists, if not create a folder

I am a newbie in excel vba coding and trying to create pdf of a excel sheet range.我是 excel vba 编码的新手,并尝试创建 ZBF57C906FA7D125566D0Z38D1Z 工作表范围的 pdf。 My code works well in windows OS but somehow it doesn't work in Mac OS.我的代码在 windows OS 中运行良好,但不知何故它在 Mac OS 中不起作用。 The Code is as below: `代码如下:`

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\_hhmm")

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

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

'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 but its not detecting even though i had created a folder there.
    MkDir (ThisWorkbook.Path & Application.PathSeparator & "forme") '<== Create Folder and its not working for Mac OS.
End If
myFile = ThisWorkbook.Path & Application.PathSeparator & "forme" & 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
        
    
    MsgBox "Il file pdf è stato creato: " _
      & vbCrLf _
      & myFile
End If

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

` I have tried searching a lot on internet but havent found any source which specifically teaches vba coding in Mac OS. ` 我已经尝试在互联网上进行大量搜索,但没有找到任何专门教授 Mac OS 中 vba 编码的资源。 Moreover, i got only one link https://macexcel.com/examples/filesandfolders/makefolder/ but i dont think it would work as it should be only one line of command and the biggest issue i dont have Mac OS available now.此外,我只有一个链接https://macexcel.com/examples/filesandfolders/makefolder/但我不认为它会起作用,因为它应该只是一行命令,最大的问题是我现在没有可用的 Mac OS。 So can somebody test my code change my command to make it compatible it with Mac OS那么有人可以测试我的代码更改我的命令以使其与 Mac OS 兼容吗

I used to use this code and also add to check file exist我曾经使用此代码并添加以检查文件是否存在

'Only Change code Here
Sub Verify()
    Dim myPath As String
    myPath = "C:\abc" '<--------This line
    If Not PathExist(myPath) Then MkDir (myPath)
End Sub

Private Function PathExist(path_ As String) As Boolean
    On Error GoTo ErrNotExist
    Call ChDir(path_)
   PathExist = True
   Exit Function
ErrNotExist:
    PathExist = False
End Function

Private Function FileExist(filePath_ As String) As Boolean
    FileExist = Len(Dir(filePath_)) <> 0
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM