CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Sep 2010
    Location
    .NET4.0/VS 2010
    Posts
    9

    [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?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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)?

  3. #3
    Join Date
    Sep 2010
    Location
    .NET4.0/VS 2010
    Posts
    9

    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"

  4. #4
    Join Date
    Jan 2010
    Posts
    1,133

    Re: COMException was unhandled

    Hi, cjja; I'm sort of guessing here, but could it be that the order of the parameters is wrong?
    Quote Originally Posted by cjja View Post
    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).

  5. #5
    Join Date
    Sep 2010
    Location
    .NET4.0/VS 2010
    Posts
    9

    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

  6. #6
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    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....

  7. #7
    Join Date
    Sep 2010
    Location
    .NET4.0/VS 2010
    Posts
    9

    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!

  8. #8
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    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...

  9. #9
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: COMException was unhandled

    Try to give escape character for the input path ...

    Code:
     eg; ("C:\'Temp");

  10. #10
    Join Date
    Sep 2010
    Location
    .NET4.0/VS 2010
    Posts
    9

    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);

  11. #11
    Join Date
    Sep 2010
    Location
    .NET4.0/VS 2010
    Posts
    9

    Re: COMException was unhandled

    Quote Originally Posted by vcdebugger View Post
    Try to give escape character for the input path ...

    Code:
     eg; ("C:\'Temp");

    When I do that it just gives the 'COMException was unhandled' error.

  12. #12
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: COMException was unhandled

    Quote Originally Posted by cjja View Post
    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...

  13. #13
    Join Date
    Sep 2010
    Location
    .NET4.0/VS 2010
    Posts
    9

    Re: COMException was unhandled

    I'm still having the initial problem with the 'aDoc.SaveAs' as when I started this thread.

  14. #14
    Join Date
    Jan 2010
    Posts
    1,133

    Re: COMException was unhandled

    Quote Originally Posted by cjja View Post
    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?

  15. #15
    Join Date
    Sep 2010
    Location
    .NET4.0/VS 2010
    Posts
    9

    Resolved 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
  •  





Click Here to Expand Forum to Full Width

Featured