Check is destination directory exist then proceed if not then create it and proceed afterwards

前端 未结 3 951
-上瘾入骨i
-上瘾入骨i 2021-01-06 08:16

I have a button on one of the worksheets that lets user to continue with his task to save his/her template as a separate workbook in the folder.

Here is my code

<
相关标签:
3条回答
  • 2021-01-06 08:29

    I created a macro that will save as pdf certain tabs of my excel in a relative (variable)folder. It will use the contract reference to create a subfolder, such subfolder label is exactly the contract reference. If subfolder already exists it just creates the files in it, else (subfolder does not exist) then it creates the folder and save the files in it.

    Sub Gera_PDF_MG_Nao_Produtor_Sem_Ajuste()
    
        Gera_PDF_MG_Nao_Produtor_Sem_Ajuste Macro
    
    
    
        Dim MyFolder As String
        Dim LaudoName As String
        Dim NF1Name As String
    
        MyFolder = ThisWorkbook.path & "\" & Sheets("Laudo").Range("C9")
        LaudoName = Sheets("Laudo").Range("K27")
        NF1Name = Sheets("MG sem crédito e sem ajuste").Range("Q3")
    
        Sheets("Laudo").Select
        Columns("D:P").Select
        Selection.EntireColumn.Hidden = True
    
    If Dir(MyFolder, vbDirectory) <> "" Then
        Sheets("Laudo").ExportAsFixedFormat Type:=xlTypePDF, filename:=MyFolder & "\" & LaudoName & ".pdf", Quality:=xlQualityMinimum, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        False
    
        Sheets("MG sem crédito e sem ajuste").ExportAsFixedFormat Type:=xlTypePDF, filename:=MyFolder & "\" & NF1Name & ".pdf", Quality:=xlQualityMinimum, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        False
    
    Else
        MkDir MyFolder
        Sheets("Laudo").ExportAsFixedFormat Type:=xlTypePDF, filename:=MyFolder & "\" & LaudoName & ".pdf", Quality:=xlQualityMinimum, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        False
    
        Sheets("MG sem crédito e sem ajuste").ExportAsFixedFormat Type:=xlTypePDF, filename:=MyFolder & "\" & NF1Name & ".pdf", Quality:=xlQualityMinimum, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        False
    
    End If
    
        Sheets("Laudo").Select
        Columns("C:Q").Select
        Selection.EntireColumn.Hidden = False
        Range("A1").Select
    
    
    
    '
    End Sub
    
    0 讨论(0)
  • 2021-01-06 08:41

    I am going to modularize your code a little bit:

    First get the directory path here

    Function getDirectoryPath()
        getDirectoryPath = ThisWorkbook.Path & Application.PathSeparator & Settings.Range("C45").Value
    End Function
    

    You can create the directory using this function

    Sub createDirectory(directoryPath)
        MkDir directoryPath
    End Sub
    

    You can check if a directory exists or not using Dir function

    Dir(directoryPath, vbDirectory) 'empty string means directoryPath doesn't exist
    

    The final function on button click:

    Private Sub ContinueButton_Click()
        Application.ScreenUpdating = 0
        Sheets(cmbSheet.Value).Visible = True
        directoryPath = getDirectoryPath
        'Creating the directory only if it doesn't exist
        If Dir(directoryPath, vbDirectory) = "" Then
             createDirectory directoryPath
        End If
        Application.Goto Sheets(cmbSheet.Value).[a22], True
        Application.ScreenUpdating = 1
        Unload Me
    End Sub
    
    0 讨论(0)
  • 2021-01-06 08:51
    If Dir(Fldrpath, vbDirectory) = "" Then
    MkDir Fldrpath
    End If
    

    Fldrpath refer to the Folderpath if Folder not found MkDir creates the folder

    0 讨论(0)
提交回复
热议问题