none
How to Write create ne folder in Program file with VBA ?? RRS feed

  • Question

  •  Hell all my Friends And Happy New Year For all

    i make my access DB  and i need to Create Folder to it in that Path

    C:\Program Files

    i try that Code

    MkDir Path:="C:\Program Files\My Pro"

    to create my folder "My Pro " in that path

    i receive that error 

    and i try that Code

        Dim objShell
            Dim objFolder
            
            Set objShell = CreateObject("shell.application")
            Set objFolder = objShell.NameSpace("C:\Program Files")
    
            If (Not objFolder Is Nothing) Then
                objFolder.NewFolder ("My Pro")
            End If
    
            Set objFolder = Nothing
            Set objShell = Nothing
    

     and that code

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.CreateFolder("C:\Program Files\My Pro")
    
    and

    i receive that error

    so i ask my self how when i install any Program in My windows  how it create anew folder for it in Program File

    its magic   ???

    how my friends i do that with access  vb  PLZ ??

    Tuesday, January 3, 2017 9:58 AM

All replies

  • Hi,

    "C:Program Files" is one of "system folder"s, which requires administrator permission to make some changes(add, modify, delete). You need to get admin. right/permission before creating a folder in it.

     

    Tuesday, January 3, 2017 12:30 PM
  • Hi,

    "C:Program Files" is one of "system folder"s, which requires administrator permission to make some changes(add, modify, delete). You need to get admin. right/permission before creating a folder in it.

     

    how my friend i do that  i searched more and more about permission  but i didnt get any information about that

    Tuesday, January 3, 2017 12:43 PM
  • You need Elevated Privileges to play around in those folders.  You could try something like shelling a command such as

    runas /user:administrator cmd /C mkdir "c:\program files\YourFolderName\"

    Or find an API that permits impersonation so you can execute your code as the administrator.

    The ShellExecute looks promising: http://stackoverflow.com/questions/24741940/vba-running-elevated-command-shell-vs-shellexecute and/or http://ss64.com/vb/shellexecute.html


    Daniel Pineault, 2010-2016 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net



    Tuesday, January 3, 2017 3:08 PM
  • It might make more sense to create a folder on the C drive instead of trying to use the Program Files directory. That is what I do for each of my Access "applications". That way I have a clean folder that does not need admin rights. All dependent files like icons and images also go into that folder.

    After all, an Access file is not a program and with Windows system folders getting more and more secure it's going to be harder to write to them.


    Bill Mosca
    www.thatlldoit.com
    http://tech.groups.yahoo.com/group/MS_Access_Professionals

    Tuesday, January 3, 2017 7:35 PM
  • I must say I completely agree with Bill.  I too do the same with my installation routines.  I've also heard of developers using the %AppData% folder or Documents (make sure it is local and not a server folder if ever you go this route).

    Daniel Pineault, 2010-2016 Microsoft MVP Professional Support: http://www.cardaconsultants.com MS Access Tips and Code Samples: http://www.devhut.net

    Tuesday, January 3, 2017 7:48 PM
  • Hi,

    If you want to create a folder in System Folder such as "C:\Program Files" with using Excel(VBA), it would be the easiest way to run Excel as Administrator.  

       Right click Excel (this sample is a short cut on Desktop)
       

    I've made a sample VBA and succeeded by the above way.
       

    VBA code in the above button [Create Folder]
    Private Sub btnCreateFolder_Click()
        Dim objShell
        Dim objFolder
        Dim targetFolderPath 'As String -- you should not add "As String"
        Dim newFolderName 'As String -- you should not add "As String"
        
        ' --- input check: target folder [C2]
        targetFolderPath = Range("C2").Value
        If (targetFolderPath = "") Then
            MsgBox "Input target folder path [C2]"
            Exit Sub
        End If
        
        ' --- input check: new folder name [C4]
        newFolderName = Range("C4").Value
        If (newFolderName = "") Then
            MsgBox "Input new FolderName [C4]"
            Exit Sub
        End If
        
        ' --- directory check if it exists
        If (Dir(targetFolderPath & "\" & newFolderName, vbDirectory) <> "") Then
            MsgBox "Error:  [" & newFolderName & "]" & " has already existed"
            Exit Sub
        End If
    
        ' --- create new folder
        Set objShell = CreateObject("shell.application")
        Set objFolder = objShell.Namespace(targetFolderPath)
            
        If (Not objFolder Is Nothing) Then
            objFolder.newFolder (newFolderName)
            MsgBox "OK:  [" & newFolderName & "]" & " has been created"
        End If
        
        Set objFolder = Nothing
        Set objShell = Nothing
    End Sub

    Regards,
    Ashidacchi

    • Edited by Ashidacchi Wednesday, January 4, 2017 2:25 AM
    Wednesday, January 4, 2017 1:11 AM
  • Sayed Gamal,

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.CreateFolder("C:\Program Files\My Pro")

    So far, it works in Access 2000 with Administrator rights. The code would not work without Admin rights.

    You need to run Access with Admin rights, than open the file MDB/ACCDB. That should work. (meaning, do not open the MDB/ACCDB directly.) You have to assumed that the PC user has admin rights. If not, you have to login as the user with admin rights.

    However, I'm not entirely sure about Runtime Access. I'm unable to test new version of Access at the moment.

    Wednesday, January 4, 2017 4:21 AM
  • Sayed Gamal,

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.CreateFolder("C:\Program Files\My Pro")

    So far, it works in Access 2000 with Administrator rights. The code would not work without Admin rights.

    You need to run Access with Admin rights, than open the file MDB/ACCDB. That should work. (meaning, do not open the MDB/ACCDB directly.) You have to assumed that the PC user has admin rights. If not, you have to login as the user with admin rights.

    However, I'm not entirely sure about Runtime Access. I'm unable to test new version of Access at the moment.

    yes my friends i try open access with Admin rights and try your code and worked perfect 

    so lets ask another Question

    how  to open access with Admin rights  by vb ??

    Wednesday, January 4, 2017 5:06 PM
  • You need Elevated Privileges to play around in those folders.  You could try something like shelling a command such as

    runas /user:administrator cmd /C mkdir "c:\program files\YourFolderName\"

    Or find an API that permits impersonation so you can execute your code as the administrator.

    The ShellExecute looks promising: http://stackoverflow.com/questions/24741940/vba-running-elevated-command-shell-vs-shellexecute and/or http://ss64.com/vb/shellexecute.html


    Daniel Pineault, 2010-2016 Microsoft MVP
    Professional Support: http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net



    thanks my friends

    let me speak to you about that small story about ShellExecute  ??

    first i post here another Question about 

    Registry with VBA

    and i try  more code to add key to

    HKEY_LOCAL_MACHINE

    but i have more problem about permission  

    at least my friends here give me that code

    Dim sh: Set sh = CreateObject("Shell.Application")
    Dim path: path = "E:\AccessPro"
    
    sh.ShellExecute "reg", "add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\my_App /v Path /d """ & path & """ /t REG_SZ /f", "", "runas", 0

    and it worked good and solve problem with permission 

    so i think my problem here same that problem

    so how i can use that code with

    MkDir Path:="C:\Program Files\My Pro"

    or another Function about create folder

    how ever if we lose all hope

    lets ask our self

    how i can create folder in windows by registry ?



    Wednesday, January 4, 2017 5:17 PM
  • yes my friends i try open access with Admin rights and try your code and worked perfect 

    so lets ask another Question

    how  to open access with Admin rights  by vb ??

    You can try a creating Short-cut to MsAccess.exe. Go to Properties - Compatibility - Privilege Level - Check "Run this program as an administrator". However, gives you the annoying UAC prompt. My guess that it solve one part of your problem.

    To prevent the UAC prompt, you could try creating it under Task Scheduler and create the short-cut to run it manually. Please use Google search as the instructions are complicated with many pictorials demos.

    With the short-cut - Shortcut tab - Target field....something like

    "C:\Program Files\Microsoft Office\Office\MSACCESS.EXE" "C:\Program Files\MyDatabaseName.mdb".  If it not a ".MDB" ....change it to ".ACCDB".

    This will target your database file with admin rights, so that you don't have to open in Access menu.

    PS. I'm not sure why Daniel Pineault's code sample isn't suitable for you?

    Thursday, January 5, 2017 3:47 AM
  • i found that  code 


    '--------------
    'Start of UAC workaround code
    If WScript.Arguments.length =0 Then
    Set objShell = CreateObject("Shell.Application")
    
    objShell.ShellExecute "wscript.exe", Chr(34) & _
    WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
    Else
    
    '--------------
    'Start of code
    
    WScript.Echo "This code is ran as an elevated user, ie. 'Administrator'"
    
    '--------------
    'End of code
    
    '--------------
    'End of UAC workaround code
    
    End If

    in that link

    Run VBS As Elivated User -Win7.vbs

    but i didnt know ho i use it with  "
    MkDir Path:="C:\Program Files\My Pro"

    Sunday, January 8, 2017 11:07 AM
  • That code is meant to be run in a vbscript to elevate the script at runtime.

    Daniel Pineault, 2010-2016 Microsoft MVP Professional Support: http://www.cardaconsultants.com MS Access Tips and Code Samples: http://www.devhut.net

    Sunday, January 8, 2017 9:19 PM
  • thanks to all my friends

    i lose my hope


    Monday, January 9, 2017 8:29 AM