0

I am trying to open Outlook from VB.NET code through a Process object.

Dim PSI As New ProcessStartInfo

' ORG - 
'PSI.FileName =  GetOutlookPath() & My.Settings.OutlookAppExe ' Returns C:\Program Files\Microsoft Office\Office16\Outlook.exe
'PSI.Arguments = My.Settings.OutlookAppArgs
 
PSI.FileName = IO.Path.Combine(GetOutlookPath(), My.Settings.OutlookAppExe)  ' "/e ""C:\Program Files\Microsoft Office\Office16\Outlook.exe""" 

'PSI.Arguments = “/importprf \\comp.us\syscol\comp.us\Software\Outlook2010\comp.prf” ' My.Settings.OutlookAppArgs
 

Here is GetOutlookPath() method :

Private Function GetOutlookPath() As String
    Dim Val As String = ""
    If My.Settings.OutlookAppPath = "" Then
        Try
            Dim Key As RegistryKey = Registry.LocalMachine.OpenSubKey(My.Settings.OutlookRegPath)
            Val = Key.GetValue("Path")
            Val = Val.Replace(";", "")
            If Val.LastIndexOf("\") <> Val.Length - 1 Then
                Val = Val & "\"
            End If
        Catch ex As Exception
            Log.Add("Unable to get registry value for Outlook: " & ex.Message)
            Throw New Exception("Unable to get registry value for Outlook: " & ex.Message)
        End Try
    Else
        Val = My.Settings.OutlookAppPath
    End If
    Log.Add("GetOutlookPath: " & Val)
    Return Val
End Function

The above code works on Win 7, but creates problems with some Win 10 systems. I just throws dir name not found exception. I tried replacing the values of Settings variables with actual content to try, and all above ways to work with path with spaces, but nothing works. Can anyone please help me know how do I set the path values and make it work on all Win OS. The arguments passed in Process also gives a hard time.

Opening IE also gives the same error. Here's the code for IE:

Private Sub LaunchIE(ByVal UserName As String, ByVal SecurePassword As SecureString, ByVal Domain As String, Optional ByVal WebSite As String = "http://iserver1/comp")
    Try
        Log.Add("LaunchIE: UserName " & UserName & " SecurePass Length: " & SecurePassword.Length & " Domain: " & Domain & " WebSite: " & WebSite)
        Dim PSI As New ProcessStartInfo
        'PSI.FileName = GetIEPath() & My.Settings.IEAppExe
        'PSI.Arguments = WebSite
      '' Tried to open notepad - no space in path, yet it throws "The directory name is invalid" exception

        PSI.FileName = IO.Path.GetFullPath("C" & IO.Path.VolumeSeparatorChar & "\Windows\notepad.exe") ' Value = C:\Windows\notepad.exe

        PSI.UserName = UserName
        PSI.Password = SecurePassword
        PSI.Domain = Domain
        PSI.LoadUserProfile = True
        PSI.UseShellExecute = False

        IEProc.StartInfo = PSI
        IEProc.Start()
    Catch ex As Exception
        Log.Add("LaunchIE Failed: " & ex.Message)
        Throw New Exception("Unable to launch Internet Explorer: " & ex.Message)
    End Try
End Sub

Can you please help me figure out how do I handle the situation.

Thanks

Terry
  • 95
  • 1
  • 3
  • 13
  • What is the value for My.Settings.OutlookRegPath? – Jake1164 Feb 07 '19 at 18:56
  • OutlookRegPath = SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE – Terry Feb 07 '19 at 19:04
  • Is it a 32bit vs 64bit registry issue? https://www.kunal-chowdhury.com/2016/06/how-to-retrieve-office-version.html – Jake1164 Feb 07 '19 at 19:27
  • Can you provide more specific information about the "hard time" you are seeing passing the parameters into the ProcessStartInfo? IE provide the exceptions – Jake1164 Feb 07 '19 at 19:38
  • On 2 Win 10 IE also throws same error (Has Office 16). On 1 Win 10, Outlook & IE both opens (Has Office 15). I checked the "Path" value of the registry key for Wow6432 & 32 bit, both has the same value i.e. "C:\Program Files\Microsoft Office\Office16" on non-working system. – Terry Feb 07 '19 at 19:54
  • With PSI.FileName = GetOutlookPath() & My.Settings.OutlookAppExe having value of "C:\Program Files\Microsoft Office\Office16\Outlook.exe"; on Start() the exception thrown is + $exception {"Unable to launch Outlook: The directory name is invalid"} System.Exception – Terry Feb 07 '19 at 19:56
  • On command prompt runas /user:vcsois1@comp.us "C:\Program Files\Microsoft Office\Office16\Outlook.exe /importprf path:\\...... executes perfectly fine. – Terry Feb 07 '19 at 19:59

1 Answers1

1

Set the 'WorkingDirectory' property of the ProcessStartInfo or you will get the "The directory name is invalid".

As per: https://stackoverflow.com/a/25072978/373334

Jake1164
  • 12,291
  • 6
  • 47
  • 64
  • Thanks Jake1164, this did resolve my problem totally. I add my working dir as PSI.WorkingDirectory = IO.Path.GetPathRoot(GetOutlookPath()) ' This gives the root drive of the path. With directoryName, again space in dir's was causing problem. So set root drive as the working dir and it does Work. Thank You once again. – Terry Feb 07 '19 at 21:08