【问题标题】:excel vba code to check for a folder if it exists, if not create a folderexcel vba代码检查文件夹是否存在,如果不存在则创建文件夹
【发布时间】:2021-07-08 20:07:10
【问题描述】:

我是 excel vba 编码的新手,正在尝试创建 excel 工作表范围的 pdf。我的代码在 Windows 操作系统中运行良好,但不知何故它在 Mac 操作系统中不起作用。代码如下: `

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

` 我曾尝试在互联网上进行大量搜索,但没有找到任何专门教授 Mac OS 中的 vba 编码的资源。此外,我只有一个链接https://macexcel.com/examples/filesandfolders/makefolder/,但我认为它不会起作用,因为它应该只是一行命令,最大的问题是我现在没有可用的 Mac OS。那么有人可以测试我的代码更改我的命令以使其与 Mac OS 兼容

【问题讨论】:

  • 欢迎任何帮助。如果有人给我源链接,我可以在其中学习 MacOS 的 vba。我们将不胜感激。
  • 你遇到什么错误显示?
  • 它在 mkdir() 行中的错误我也在代码中注释了。
  • 我知道它在 mkdir() 行中,但是当它在该行上运行时会显示什么错误?看看 vba 在 MacOS 中是如何被沙盒化的docs.microsoft.com/en-us/office/vba/api/overview/office-mac

标签: excel vba macos directory


【解决方案1】:

我曾经使用此代码并添加以检查文件是否存在

'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

【讨论】:

    猜你喜欢
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2019-04-21
    • 1970-01-01
    • 2011-01-19
    • 2017-06-26
    相关资源
    最近更新 更多