2

My app is supposed to take user inputs and archive them in Excel files.

All of that would work if I didn't need to access said Excel files as a special user due to the company's safety restrictions.

I have working code that opens said restricted files through creating a new process (it is pretty much the same as the one here).

I use the code as such:

Sub RunAsUser_Main()
    Dim ExeCommand As String
    ExeCommand = "C:\Program Files\Microsoft Office\Office16\EXCEL.EXE \\192.168.88.3\share\public\Workbook.xlsm"
    RunAsUser "username", "password", "domain", ExeCommand, "C:\Windows"
    
'-------------------- OPEN WORKBOOK --------------------
    Dim ret As Integer
    Dim ExcelApp As Object
    Dim WorkbookPath As String
    Dim MyWorkbook As Object
    
    On Error Resume Next
    Set ExcelApp = GetObject("Excel.Application").Application
    If ExcelApp Is Nothing Then
        ret = MsgBox("error!", vbCritical + vbOKOnly, title)
        Exit Sub
    End If
...

All goes well until the GetObject statement. The new process starts, Excel opens the workbook as it should and I have verified that it is run by the special user.
After this, I am unable to reference the running ExcelApp object. It returns nothing.

I've also used the code from this thread that is supposed to list all current instances of Excel. It does list the ones running under my current user, but doesn't register the one under my special user.

Community
  • 1
  • 1
O_W
  • 37
  • 4
  • 1
    Try changing your GetObject statement to `GetObject(, "Excel.Application")` – Brian M Stafford Sep 22 '22 at 12:15
  • @BrianMStafford I believe I've tried this one before, without success. Will give it a go once again when I get to the company network. – O_W Sep 25 '22 at 11:23
  • @BrianMStafford Finally made it back and as expected, the **GetObject(, "Excel.Application")** method still returns nothing. My guess is that the app running under my default user can't see the processes ran by the special one, without some special code that is. I'm sure this must be doable somehow, since i can edit the wokbook manually, it's just completely eyond me where I could find the docummentation necessary for this problem. – O_W Sep 29 '22 at 06:52
  • It may not have an impact but to not distract future possible responders fix your `GetObject` line and as well update this question post. Notice how `On Error Resume Next` is immediately dismissed with `On Error GoTo 0`. https://stackoverflow.com/a/73127493/1571407 – niton Mar 31 '23 at 21:32

0 Answers0