0
votes

I am creating a small application to "RunAs different user". Files (Full path) which should be opened are stored in a Listview.

If call the procedure with Do_RunAs("C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE", "D:\Run As Test\Naming - Test.xlsx") ... it is working

But If call the procedure with Call Do_RunAs("C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE", item.SubItems(1).Text) I get this message in EXCEL (see linked picture) ...file is locked for editing by another user

Here is the code for Do_RunAs

Public Sub Do_RunAs(strApplication As String, strFilename As String)
    Dim procStartInfo As New ProcessStartInfo
    Dim procExecuting As New Process

    With procStartInfo
        .UseShellExecute = False

        .Domain = strDomain
        .UserName = strUserName
        .Password = ConvertToSecureString(strPassword)
        .Verb = "runas"
        .LoadUserProfile = True

        .FileName = GetShortPathName(strApplication)
        .Arguments = GetShortPathName(strFilename).ToString
        .WindowStyle = ProcessWindowStyle.Normal
    End With

    procExecuting = Process.Start(procStartInfo)
    procExecuting.Close()
End Sub

As Arguments I already tried

  • .Arguments = strFilename
  • .Arguments = Chr(34) + strFilename + Chr(34)
  • .Arguments = GetShortPathName(strFilename)
  • .Arguments = GetShortPathName(strFilename).ToString

Nothing works, always the same error.

Would be great if someone could help me - Thanks a lot in advance!

1

1 Answers

0
votes

I found the answer to that problem ... maybe someone needs it This Sub was called within a For-Next loop. I simply added a WAIT function before calling this Sub the next time

For i = 1 To 10
    Call Do_RunAs("Excel.exe", "xy.xls")
    Wait(3) 'wait 3 seconds
Next

This solved the problem