none
This command is not available because no document is open

    Question

  • Hello

    when i am saving word file as a Save as format using c# i got error below i written my code.

              objWord.Documents.Open(ref FileName, ref readOnly, ref missing, ref missing, ref missing, ref missing,
              ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing,
              ref missing, ref missing);
    
            
    
    
              Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
    
              
              
                //For doc
    
              oDoc.ExportAsFixedFormat(filesavexls, WdExportFormat.wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint,
                WdExportRange.wdExportAllDocument,1,1,
                WdExportItem.wdExportDocumentWithMarkup, 
                true, true, WdExportCreateBookmarks.wdExportCreateHeadingBookmarks, true, true, true, ref missing);
    

     


    Amitsp
    Tuesday, August 31, 2010 6:32 AM

Answers

  • Hello Amit,

    Thanks for your thread!

    The best place for VSTO issues is here:
    http://social.msdn.microsoft.com/Forums/en-US/vsto/

    However, you could try the following code snippet to open your word document if you use Visual Studio 2010 and .NET Framework 4.0:

    Document doc = app.Documents.Open(@"<path>\test.docx", ReadOnly: false, Visible: false);
    doc.Activate();
    // other work...
    

    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Roahn Luo Monday, September 06, 2010 10:02 AM
    Wednesday, September 01, 2010 1:51 AM
  • Yes Santhosh

    M

     public static void Convertword(string SourceFile, string DestinatioFile)
        {
          Microsoft.Office.Interop.Word.ApplicationClass objWord = null;
    
          object fltDocFormat = 10; //For filtered HTML Output
          object missing = System.Reflection.Missing.Value;
          object readOnly = false; //Open file in readOnly mode
          object isVisible = false;//The process has to be in invisible mode
          object Unknown = Type.Missing;
    
          object FileName = SourceFile;
          object FileToSave = DestinatioFile;
    
    
    
          try
          {
    
            objWord = new Microsoft.Office.Interop.Word.ApplicationClass();
            objWord.Application.Visible = false;
    
            objWord.Documents.Open(ref FileName, ref readOnly, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing,
            ref missing, ref missing);
    
            Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
    
            object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
    
            //For doc
            //Response.Write(objWord.Documents.Count.ToString());
            objWord.ActiveDocument.SaveAs(ref FileToSave, ref format,
              ref Unknown, ref Unknown, ref Unknown,
              ref Unknown, ref Unknown, ref Unknown,
              ref Unknown, ref Unknown, ref Unknown,
              ref Unknown, ref Unknown, ref Unknown,
             ref Unknown, ref Unknown);
    
            objWord.Quit(ref missing, ref missing, ref missing);
            // ((Microsoft.Office.Interop.Word._Application)objWord).Quit(ref missing, ref missing, ref missing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(objWord);
          }
          catch (Exception ex)
          {
          }
          finally
          {
            objWord.Quit(ref missing, ref missing, ref missing);
            // ((Microsoft.Office.Interop.Word._Application)objWord).Quit(ref missing, ref missing, ref missing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(objWord);
          }
        }
    

     


    Amitsp
    • Marked as answer by Amit Prajapati Wednesday, April 25, 2012 5:10 AM
    Friday, October 29, 2010 6:30 AM

All replies

  • Hello Amit,

    Thanks for your thread!

    The best place for VSTO issues is here:
    http://social.msdn.microsoft.com/Forums/en-US/vsto/

    However, you could try the following code snippet to open your word document if you use Visual Studio 2010 and .NET Framework 4.0:

    Document doc = app.Documents.Open(@"<path>\test.docx", ReadOnly: false, Visible: false);
    doc.Activate();
    // other work...
    

    Best regards


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Roahn Luo Monday, September 06, 2010 10:02 AM
    Wednesday, September 01, 2010 1:51 AM
  • Hello

    this code perfectly work when i run from vs2008. issue is when i publish on my server
    Amitsp
    Wednesday, September 01, 2010 4:46 AM
  • Thanks for your feedback!

    Have you debugged your published application? How did you install your application? VSTO installer? or clickonce? or other? Can you please provide us more information about the issue?

    Thanks a lot!


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Wednesday, September 01, 2010 5:55 AM
  • Hello I publish my application on IIS7.0, there is already install OFFICE2007.
    Amitsp
    Wednesday, September 01, 2010 6:52 AM
  • Hello,

    Based on the error message, your document is not opened for the command. Would you please check the path to your document? Is it avaiable after publishing your project to IIS?

    Since you are debugging in production environment, from my perspective, to find the root cause, you need to get a call stack information to narrow down the issue. I recommend you two tools when debugging in production environment : WinDbg and Process Explorer. Please try to follow the example here for a tutorial on how to use them to find the root cause. You could also post the call stack information here.

    Best regards 


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    Thursday, September 02, 2010 6:50 AM
  • Hi Amit,

    Did you get any solution . I am facing the same problem

    Regards,

    Santhosh

    Wednesday, October 06, 2010 1:42 PM
  • Yes Santhosh

    M

     public static void Convertword(string SourceFile, string DestinatioFile)
        {
          Microsoft.Office.Interop.Word.ApplicationClass objWord = null;
    
          object fltDocFormat = 10; //For filtered HTML Output
          object missing = System.Reflection.Missing.Value;
          object readOnly = false; //Open file in readOnly mode
          object isVisible = false;//The process has to be in invisible mode
          object Unknown = Type.Missing;
    
          object FileName = SourceFile;
          object FileToSave = DestinatioFile;
    
    
    
          try
          {
    
            objWord = new Microsoft.Office.Interop.Word.ApplicationClass();
            objWord.Application.Visible = false;
    
            objWord.Documents.Open(ref FileName, ref readOnly, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing,
            ref missing, ref missing);
    
            Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
    
            object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
    
            //For doc
            //Response.Write(objWord.Documents.Count.ToString());
            objWord.ActiveDocument.SaveAs(ref FileToSave, ref format,
              ref Unknown, ref Unknown, ref Unknown,
              ref Unknown, ref Unknown, ref Unknown,
              ref Unknown, ref Unknown, ref Unknown,
              ref Unknown, ref Unknown, ref Unknown,
             ref Unknown, ref Unknown);
    
            objWord.Quit(ref missing, ref missing, ref missing);
            // ((Microsoft.Office.Interop.Word._Application)objWord).Quit(ref missing, ref missing, ref missing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(objWord);
          }
          catch (Exception ex)
          {
          }
          finally
          {
            objWord.Quit(ref missing, ref missing, ref missing);
            // ((Microsoft.Office.Interop.Word._Application)objWord).Quit(ref missing, ref missing, ref missing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(objWord);
          }
        }
    

     


    Amitsp
    • Marked as answer by Amit Prajapati Wednesday, April 25, 2012 5:10 AM
    Friday, October 29, 2010 6:30 AM
  • Thanks Amit
    • Proposed as answer by santhosh04 Wednesday, November 17, 2010 10:51 AM
    Wednesday, November 17, 2010 10:51 AM
  • refer to my answer posted today on http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/f878ac45-fe93-40f3-9e27-83c9d363808c/
    Wednesday, February 15, 2012 12:41 AM
  • Hi Amit,

    I am using you code to convert html to word document its work find in development after publish the site it wont work i am getting the error "This command is not available because no document is open." Please help me to solve this issue.

    Tuesday, February 18, 2014 10:10 AM