Run VBA macro as administrator

YasserKhalil

Well-known Member
Joined
Jun 24, 2010
Messages
852
Hello everyone
In the following link, I have found a code that creates a folder and makes it shareable .. In the code there's a link to VBScript code that works fine, but not for VBA

The code creates a folder but it is not shareable and I think the cause is that I have to run the code as an administrator.
Any help in this topic, please
VBA Code:
Sub TestRajeshS()
    '* For Rajesh S
    '*
    '* How to make a folder shareable
    '* needs admin permissions!
    '* Answer to https://stackoverflow.com/questions/45525238/how-can-i-make-the-folder-sharable
    
    '* copyright
    '* based on https://blogs.msdn.microsoft.com/imayak/2008/12/05/vbscript-for-creating-and-sharing-a-folder/#
    '* Owner - Imayakumar J.   Date - December 5 2008
    '* end of copyright
    
    '----------------------------------------------------
    'Create folder
    '----------------------------------------------------
    
    Dim filesys As Object
    Set filesys = CreateObject("Scripting.FileSystemObject")
    
    Dim sFolderName As String
    sFolderName = "n:\ShareThis"
    
    If Not filesys.folderexists(sFolderName) Then
        filesys.createfolder sFolderName
    End If
    
    '---------------------------------------------------------
    ' Check if another shar with the same name exists
    '---------------------------------------------------------
    
    Dim strComputer As String
    strComputer = "."
    
    Dim objWMIService As Object
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        
    Dim colShares As Object
    Set colShares = objWMIService.ExecQuery _
        ("Select * from Win32_Share Where Name = 'MYSHARENAME'")
    
    Dim objShare As Object
    For Each objShare In colShares
        objShare.Delete
    Next
    
    '-----------------------------------------------------
    ' Share the created folder
    '-----------------------------------------------------
    
    Const FILE_SHARE = 0
    Const MAXIMUM_CONNECTIONS = 25
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        
    Dim objNewShare As Object
    Set objNewShare = objWMIService.Get("Win32_Share")
    
    Dim errReturn As Variant
    errReturn = objNewShare.Create _
        (sFolderName, "MYSHARENAME", FILE_SHARE, _
            MAXIMUM_CONNECTIONS, "Sample share created with Microsoft Scripting Runtime.")
    
    If errReturn = "0" Then
        Debug.Print "Success"
    Else
        '* did you forget to run as admin?
        Debug.Print "Task Failed - did you forget to run as admin"
    End If
    
    '---------------------------------------------
    ' Script End
    '-------------------------------———————
    

End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hi. I just tried it - as you say, it needs to be run as administrator. You need to run Excel as administrator, and then run the VBA code from that instance of Excel.
 
Upvote 0
I already tried this code but gives me Failed message
VBA Code:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Const SW_SHOWNORMAL = 1

Sub Test()
    ShellExecute 0, "runas", "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE", Command, vbNullString, SW_SHOWNORMAL
    Dim xl As Excel.Application
    Set xl = GetObject(, "Excel.Application")
    With xl
        .Visible = False
        .Workbooks.Open ThisWorkbook.FullName
        .Run "MyMacro"
    End With
End Sub
 
Upvote 0
I meant that you would actually have to quit excel and then actually load excel itself as administrator (as per the pictures below). Once Excel is loaded, you will be able to run the other script that makes the folder shareable.

1592122924144.png
1592123075939.png
 
Upvote 0
Also, I might be wrong (wouldn't be the first time), but I don't know that your code was ever going to work. Specifically, you are trying to get another instance of Excel to open ThisWorkbook, which is obviously already open because the code you're using to do all this is in the middle of being executed...
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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