VBA code needed to creata folder on desktop

darksidedavey

New Member
Joined
Oct 13, 2003
Messages
1
Hi all. Is there specific code needed to have an Excel macro create a folder on your desktop? I can't seem to figure it out.

Thanks, Dave
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
First you would have to activate the Microsoft Scripting Runtime library (it's under Tools/ References)

Then add these lines to your macro. Be sure to change the path, though.

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateFolder("c:\Test Folder")
 
Upvote 0
This should work ok, without hardcoding the desktop path:

<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Const</SPAN> CSIDL_DESKTOP = &H0
<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Type</SPAN> ****EMID
    cb <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    abID <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Type</SPAN>
<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Type</SPAN> ITEMIDLIST
    mkid <SPAN style="color:#00007F">As</SPAN> ****EMID
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Type</SPAN>
<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> SHGetSpecialFolderLocation <SPAN style="color:#00007F">Lib</SPAN> "shell32.dll" _
    (<SPAN style="color:#00007F">ByVal</SPAN> hwndOwner <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> nFolder <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, pidl <SPAN style="color:#00007F">As</SPAN> ITEMIDLIST) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Declare</SPAN> <SPAN style="color:#00007F">Function</SPAN> SHGetPathFromIDList <SPAN style="color:#00007F">Lib</SPAN> "shell32.dll" Alias _
    "SHGetPathFromIDListA" (<SPAN style="color:#00007F">ByVal</SPAN> pidl <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> pszPath <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Function</SPAN> GetSpecialfolder(CSIDL <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> r <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, Path$
    <SPAN style="color:#00007F">Dim</SPAN> IDL <SPAN style="color:#00007F">As</SPAN> ITEMIDLIST
    <SPAN style="color:#007F00">'Get the special folder</SPAN>
    r = SHGetSpecialFolderLocation(100, CSIDL, IDL)
    <SPAN style="color:#00007F">If</SPAN> r = 0 <SPAN style="color:#00007F">Then</SPAN>
        <SPAN style="color:#007F00">'Create a buffer</SPAN>
        Path$ = Space$(512)
        <SPAN style="color:#007F00">'Get the path from the IDList</SPAN>
        r = SHGetPathFromIDList(<SPAN style="color:#00007F">ByVal</SPAN> IDL.mkid.cb, <SPAN style="color:#00007F">ByVal</SPAN> Path$)
        <SPAN style="color:#007F00">'Remove the unnecessary chr$(0)'s</SPAN>
        GetSpecialfolder = Left$(Path, InStr(Path, Chr$(0)) - 1)
        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Function</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    GetSpecialfolder = ""
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> Test()
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>    <SPAN style="color:#007F00">'In case it already exists</SPAN>
    MkDir GetSpecialfolder(CSIDL_DESKTOP) & "\My Folder"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
JPG, thanks, I knew either you or Ivan would produce the special folder code. I don't have it.


north19701 said:
First you would have to activate the Microsoft Scripting Runtime library (it's under Tools/ References)

Then add these lines to your macro. Be sure to change the path, though.

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateFolder("c:\Test Folder")

Not to sound like a *****, but the purpose of the late binding here means that you don't have to set a reference to the Microsoft Scripting Runtime library.
 
Upvote 0
...and here's a little latebinding, earlybinding example I cooked up:

Code:
Public Sub latebinding()
  Dim fs, a
  
  Set fs = CreateObject("Scripting.FileSystemObject")
  Set a = fs.CreateFolder("c:\Test Late Binding")

End Sub

Public Sub earlybinding()
  Dim fs As FileSystemObject 'this object variable becomes available after reference
  Dim a As Folder            'to MS Scripting Library is made
  
  Set fs = New FileSystemObject
  Set a = fs.CreateFolder("c:\Test Early Binding")
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,579
Messages
6,131,531
Members
449,654
Latest member
andz

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