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

前端 未结 3 978
-上瘾入骨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
    

提交回复
热议问题