Hi,
I have a macro in Visual Studio to launch the current project with the proper command line. This command lines holds a file name that I would like the user to select with an open file dialog box but OpenFileDialog fails to work with the following error:
"Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process."
Googling this error made me add this line to my sub:
But now I get a "Failed to set the specified COM apartment state." error. What should I do? All I want is a dialog to select a file within a macro.
Here is the code:
Thanks.
I have a macro in Visual Studio to launch the current project with the proper command line. This command lines holds a file name that I would like the user to select with an open file dialog box but OpenFileDialog fails to work with the following error:
"Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process."
Googling this error made me add this line to my sub:
Code:
System.Threading.Thread.CurrentThread.SetApartmentState(System.Threading.ApartmentState.STA)
Here is the code:
Code:
Public Sub SelectArgs()
Dim openfile As New System.Windows.Forms.OpenFileDialog
openfile.InitialDirectory = "c:\"
openfile.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openfile.FilterIndex = 2
openfile.RestoreDirectory = True
If openfile.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
[...]
End Sub