I had the same problem, to get around it you have to thread the OpenFileDialog:
Here is the thread instance
-----------
OpenFileDialog_Class oOpenDialog = new OpenFileDialog_Class();
Thread tFileNameThread = new Thread(new ThreadStart(oOpenDialog.StartThread));
tFileNameThread.SetApartmentState(ApartmentState.S TA); //This must be set
tFileNameThread.Start(); //Start the thread
tFileNameThread.Join();
sTemplate = oInvoice.GetTemplateName(); //Get the path/name of the file to use
---------------
Below is the class for the thread above
using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;
/// <summary>
/// Summary description for OpenFileDialog_Class
/// </summary>
public class OpenFileDialog_Class
{
public string sTemplate = null;
public OpenFileDialog_Class()
{
//
// TODO: Add constructor logic here
//
//GetTemplate();
}
public void StartThread()
{
sTemplate = GetTemplate();
}
[STAThread] //Must have this declared, or your thread crashes
public string GetTemplate()
{
string sTemplate = null;
try
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
openFileDialog.Filter = "Word Template (*.dot)|*.dot|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
int iStartFile = openFileDialog.FileName.LastIndexOf("\\");
try
{
sTemplate = openFileDialog.FileName;
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}
}
catch (Exception ex)
{
//LogWriter_Class sw = new LogWriter_Class();
//sw.logError(ex, " error, check the log file.", " Error");
}
return sTemplate;
}
[STAThread]
public string GetTemplateName()
{
return sTemplate;
}
}
This works ok, only problem is the OpenFileDialog is not in Focus, your IE is, but you can Alt+Tab it easy enough.
My use is to select a Word Template document to create an Invoice for my sales.
Hope this helps.
Scott Thornton
www.thornton-imports.com