theman4392000

New Member
Joined
Apr 14, 2011
Messages
2
I'm having a problem creating an excel vba code that creates a folder on the desktop and saves it under a name that I have on the excel sheet.

I have the code configured to work for my computer, but it won't work on others because the code reads:

Sheets("Folder Creator").Select

fnamepdf = Range("B8").Value

MkDir "C:\Users\Smith\Desktop\Roof Projects\" & fnamepdf

fnamepdf2 = "C:\Users\Smith\Desktop\Roof Projects\" & fnamepdf & "\" & fnamepdf & " Job Folder.pdf"

Not everyone is a Smith User. How can I change that so anyone can use the code and have it create a folder on their desktop, on their computer?

I have windows 7, but others may be using windows vista
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this function to get the user's Desktop folder:
Code:
Private Function Get_SpecialFolderPath(strSpecialFolder) As String

    Dim WSshell As Object
     
    Set WSshell = CreateObject("WScript.Shell")
    Get_SpecialFolderPath = WSshell.SpecialFolders(strSpecialFolder)
    Set WSshell = Nothing
    
End Function
Call it like this:
Code:
Sub test()
    Dim desktopFolder As String
    desktopFolder = Get_SpecialFolderPath("Desktop")
End Sub
 
Upvote 0
I'm fairly new to VBA. Is there any way you can explain what that does and how to input it into the code? Would I put the private sub into the ThisWorkbook file? How could that connect with what I already have?

Thanks so much for your quick response.
 
Upvote 0
WSshell.SpecialFolders returns the folder path of the specified 'special' Windows folder - in this case it is called with the argument "Desktop", so it returns the folder path of the user's Desktop folder. See http://msdn.microsoft.com/en-us/library/0ea7b5xe(VS.85).aspx.

Incorporate it into your code like this (all code goes in the ThisWorkbook module if you want it executed when the workbook is opened):
Code:
Option Explicit

Private Sub Workbook_Open()

    Dim fnamepdf As String, fnamepdf2 As String
    Dim desktopFolder As String
    
    Sheets("Sheet1").Select
    fnamepdf = Range("B8").Value
    
    desktopFolder = Get_SpecialFolderPath("Desktop")
    
    MkDir desktopFolder & "\Roof Projects\" & fnamepdf
    
    fnamepdf2 = desktopFolder & "\Roof Projects\" & fnamepdf & "\" & fnamepdf & " Job Folder.pdf"
    
    MsgBox fnamepdf2

End Sub


Private Function Get_SpecialFolderPath(strSpecialFolder) As String

    Dim WSshell As Object
     
    Set WSshell = CreateObject("WScript.Shell")
    Get_SpecialFolderPath = WSshell.SpecialFolders(strSpecialFolder)
    Set WSshell = Nothing
    
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,774
Messages
6,132,651
Members
449,740
Latest member
Stevejhonsy

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