Aniket Bhansali Aniket Bhansali - 23 days ago 15
C# Question

This command is not available because no document is open while getting Active document

try
{
Microsoft.Office.Interop.Word.Application WordObj = System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application") as Microsoft.Office.Interop.Word.Application;
Office.CustomXMLParts currClassification = WordObj.ActiveDocument.CustomXMLParts;
}
catch(Exception ex)
{
//I am getting, This command is not available because no document is open. this error here.
}


When I am using above code, I am getting this error:


This command is not available because no document is open.


Regards

Answer

Actually you are trying to access active document when there is no document open in word application so you are getting an error. Your word application is open but no document is opened in it i.e. you are at home screen of word application as shown in image.

enter image description here

Try to use following code to check whether there are any open documents in your application and then access ActiveDocument

if(WordObj.Documents.Count >= 1)
Comments