-
September 27th, 2010, 08:03 AM
#1
[RESOLVED] COMException was unhandled
Hey guys, this may seem like a really basic question but I just can't get my head around why it won't work!!
I have a form where the user can select a MS Word document to convert to XHTML and save this file in a different directory. However, I keep getting 'COMException was unhandled' on this line:
Code:
aDoc.SaveAs(ref fileSave, ref missing, ref fileformat, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
These are declared before the above code:
Code:
strFileName = fDialog.FileName;
string saveFile = strFileName.Substring(0, strFileName.Length - 4); object saveFilename = saveFile + ".html"; string mPath = @"D:\Temp\";
object fileSave = mPath + saveFilename; object fileformat = Word.WdSaveFormat.wdFormatFilteredHTML;
What have I done wrong?
-
September 27th, 2010, 10:48 PM
#2
Re: COMException was unhandled
Have you wrapped that line in a try/catch block? If so, does it give you any more detailed information (or an inner exception)?
-
September 28th, 2010, 02:31 AM
#3
Re: COMException was unhandled
Yes it's within a try/catch block.
The program compiles but the error is thrown when it runs. The only error detail given is:
"COMException was unhandled. Parameter value was out of acceptable range"
-
September 29th, 2010, 10:51 AM
#4
Re: COMException was unhandled
Hi, cjja; I'm sort of guessing here, but could it be that the order of the parameters is wrong?
Originally Posted by cjja
Code:
aDoc.SaveAs(ref fileSave, ref missing, ref fileformat, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
Try changing that to:
Code:
aDoc.SaveAs(ref fileSave, ref fileformat, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
At least that's what the documentation is saying, if I got the right one: Document.SaveAs Method (2007 System).
-
October 4th, 2010, 02:28 AM
#5
Re: COMException was unhandled
The error lies with the variable 'strFileName'
I have one method where the file dialog is opened, user selects file and strFileName is declared.
In another method I want to call strFileName, but I'm unsure how to do this. If I make the variables public I also need to make the file dialog public for this to work.
Code:
public void btnOpenFile_Click(object sender, System.EventArgs e)
{
OpenFileDialog fDialog = new OpenFileDialog();
fDialog.Title = Properties.Resources.OpenWordFile;
fDialog.Filter = Properties.Resources.WordDoc;
fDialog.InitialDirectory = @"D:\Temp\1-input\";
fDialog.Multiselect = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
fDialog.AddExtension = true;
fDialog.DefaultExt = "doc";
if (fDialog.ShowDialog() == DialogResult.OK)
{
string strFileName = fDialog.FileName;
if (strFileName == String.Empty)
return;
}
}
As I want strFileName to equal fDialog.FileName when I call it in the other method, if I declare at the top of the class it will obviously generate an error
Code:
public OpenFileDialog fDialog;
public string strFileName = fDialog.FileName;
How do I get the variable to be set to fDialog.FileName for the other methods?
Last edited by cjja; October 4th, 2010 at 03:47 AM.
Reason: strFileName needs to be set to fDialog.FileName
-
October 4th, 2010, 03:38 AM
#6
Re: COMException was unhandled
try to put a break point at this location:
Code:
object fileSave = @"D:\Temp\2-xhtml\" + saveFilename;
and check what is there in "fileSave" - and is it the same what you are expecting....
-
October 4th, 2010, 04:43 AM
#7
Re: COMException was unhandled
Everything that was expected is in there. The program now runs and compiles with no errors but does not save the file anywhere!
-
October 4th, 2010, 04:55 AM
#8
Re: COMException was unhandled
cjja,
you cannot directly assign like this
Code:
public string strFileName = fDialog.FileName;
when you do this.. the fDialog.FileName does not contain the path .. it might have NULL value..
you just declare strFileName without initializing... and later in the when the user clicks on OK button then you will get the required path...
-
October 4th, 2010, 05:01 AM
#9
Re: COMException was unhandled
Try to give escape character for the input path ...
-
October 4th, 2010, 05:12 AM
#10
Re: COMException was unhandled
Yes but I need the variable to have this value when it is called in another method. If I declare it at the top as null the value called in the other method will be null.
I need it to have the filename the user selected from the file dialog.
To Clarify.
in the method where file dialog is open:
Code:
string strFileName = Path.GetFileName(fDialog.FileName);
I then have another button with a click method. When the button is clicked, I want to call:
Code:
string saveFile = strFileName.Substring(0, strFilePath.Length - 4);
-
October 4th, 2010, 05:14 AM
#11
Re: COMException was unhandled
Originally Posted by vcdebugger
Try to give escape character for the input path ...
When I do that it just gives the 'COMException was unhandled' error.
-
October 4th, 2010, 05:21 AM
#12
Re: COMException was unhandled
Originally Posted by cjja
Yes but I need the variable to have this value when it is called in another method. If I declare it at the top as null the value called in the other method will be null.
I need it to have the filename the user selected from the file dialog.
To Clarify.
in the method where file dialog is open:
Code:
string strFileName = Path.GetFileName(fDialog.FileName);
I then have another button with a click method. When the button is clicked, I want to call:
Code:
string saveFile = strFileName.Substring(0, strFilePath.Length - 4);
yup, you can do that , make sure the click method is called after the method where file dialog is open... so that the strFileName will have the file name value filled in it...
-
October 4th, 2010, 07:24 AM
#13
Re: COMException was unhandled
I'm still having the initial problem with the 'aDoc.SaveAs' as when I started this thread.
-
October 4th, 2010, 09:01 AM
#14
Re: COMException was unhandled
Originally Posted by cjja
Yes but I need the variable to have this value when it is called in another method. If I declare it at the top as null the value called in the other method will be null.
It's sounds like you don't know what to do with that variable? Just make it a private member field of the class containing the method. You know what I'm talking about?
-
October 8th, 2010, 04:19 AM
#15
Re: COMException was unhandled
Never mind, I found out my code was right, the remote procedure call wasn't working for some reason.
Thanks anyway.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|