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