[VBA] Update code to create folder and save docs within

sprigelz

Board Regular
Joined
Jan 7, 2016
Messages
98
Office Version
  1. 365
Platform
  1. Windows
Below is some of the code from a VBA macro. Currently when this runs it saves all newly created docs directly onto the desktop. Instead I would like for this code to create a named folder onto the desktop and save the files within.

VBA Code:
DesktopFolder = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\"

Could you help please.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Change value of sfs. Last 2 lines in sub are for testing. Change those to suit.
VBA Code:
Sub SubFoldersInDesktop()
  Dim DesktopFolder As String, sfs As String
  
  sfs = "Named Folder\Sub1\Sub2"
  With CreateObject("WScript.Shell")
    DesktopFolder = .SpecialFolders("Desktop") & "\"
    sfs = DesktopFolder & sfs
    .Run "cmd /c md " & """" & sfs & """", 0, True
  End With
  
  FileCopy "d:\t\test.txt", sfs & "\test.txt"
  Shell "explorer " & """" & sfs & """", vbNormalFocus
End Sub
 
Upvote 0
Kenneth, thanks for the code. It created the folder, but did not save the files within. Below is the full code. Is there something else I need to change?

VBA Code:
Public Sub Create_PDFs()

    Dim DesktopFolder As String, sfs As String
    Dim r As Range
   
    sfs = "Named Folder"
    With CreateObject("WScript.Shell")
    DesktopFolder = .SpecialFolders("Desktop") & "\"
    sfs = DesktopFolder & sfs
    .Run "cmd /c md " & """" & sfs & """", 0, True
    End With
   
    For Each r In Range(Range("K3").Validation.Formula1)
        Range("K3").Value = r.Value
        Sheets("Letter").ExportAsFixedFormat _
            Type:=xlTypePDF, Filename:=DesktopFolder & Sheets("Letter").Range("K3").Value & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    Next

End Sub
 
Upvote 0
You saved the pdfs to the desktopfolder and not sfs folder. r could be blank or be data with illegal filename characters.

Maybe something like:
VBA Code:
'Sheets("Letter").ExportAsFixedFormat _
            Type:=xlTypePDF, Filename:=DesktopFolder & Sheets("Letter").Range("K3").Value & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Sheets("Letter").ExportAsFixedFormat _
            Type:=xlTypePDF, Filename:=sfs & "\" & r & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
 
Upvote 0
Solution
You saved the pdfs to the desktopfolder and not sfs folder. r could be blank or be data with illegal filename characters.

Maybe something like:
VBA Code:
'Sheets("Letter").ExportAsFixedFormat _
            Type:=xlTypePDF, Filename:=DesktopFolder & Sheets("Letter").Range("K3").Value & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Sheets("Letter").ExportAsFixedFormat _
            Type:=xlTypePDF, Filename:=sfs & "\" & r & ".pdf", _
            Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Absolutely perfect. Thank you!
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,821
Members
449,049
Latest member
cybersurfer5000

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top