using Word = Microsoft.Office.Interop.Word;
var app = new Word.Application();
var doc = app.Documents.Open("file.docx", PasswordDocument: "<password>");
I am trying to open word file having password length > 15.
It gives error of invalid command line.
Error Detail:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Application.exe
Additional information: Command failed
Note: I need resolution for word interop i may not allow to user third party.
Same file can open by giving manual password.
Any one have any resolution?
Related
I have some C# code that I want to apply to a single open Word document using Interop. Below's my attempt.
Microsoft.Office.Interop.Word.Application application =
(Microsoft.Office.Interop.Word.Application)
System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
if( application != null && application.Documents.Count > 0)
{
Document document = application.ActiveDocument;
}
application is non-null but application.Documents.Count is equal to zero even though I have a Word document open? If I remove the test for application.Documents.Count > 0 and then this line
Document document = application.ActiveDocument;
throws the error
System.Runtime.InteropServics.COMException: 'This command is not
available because no document is open.'
Is there something additional I need to do for application to recognize my open Word doc?
Using Winnovative's excel library I am able to parse an excel file (xls) that contains a hyperlink. It works when the program is run directly on my machine. The exact same file will fail if I try to upload it to a website and parse it. Here is an example of the logic:
// Works when bytes are parsed from a program running locally.
// Fails when bytes are parsed from a file uploaded to a website
public void TestRead(byte[] bytes)
{
try
{
// Parse excel file
var workBook = new ExcelWorkbook(new MemoryStream(bytes));
// Save file
var toSave = workBook.Save();
System.IO.File.WriteAllBytes(#"C:\Users\[User]\Downloads\Test.xls", toSave);
}
catch (Exception e)
{
}
}
This is only an issue with xls files. I don't have trouble parsing a xlsx file with a hyperlink. The uploaded file fails to parse with an exception (which also crashes the application even though it is enclosed in a try-catch block):
Could not open the workbook.Arithmetic operation resulted in an overflow.
at Winnovative.ExcelLib.ExcelWorkbook..ctor(Stream excelStream, String openPassword, ExcelWorkbookDefaultSettings defaultSettings)
at Winnovative.ExcelLib.ExcelWorkbook..ctor(Stream excelStream)
and
System.OverflowException was unhandled
Message: An unhandled exception of type 'System.OverflowException' occurred in wnvxls.dll
Additional information: Arithmetic operation resulted in an overflow.
What is the correct way to check is an excel file is password protected (I do not know the password)? When I try to open it I get a non-specific exception ("A disk error occurred during a write operation.") that could be related to any other type of invalid file. Thanks
If I try to open a password protected xlsx file, I get the exception -
{"Can not open the package. Package is an OLE compound document. If this is an encrypted package, please supply the password"}
It also suggests using the overloaded method to open it
sample:
string mySpreadsheetName = #"path/to/file/name/xlsx";
FileInfo fi = new FileInfo(mySpreadsheetName);
ExcelPackage p1 = new ExcelPackage(fi, "abcd"); // this opens correctly, here "abcd" is the password
ExcelPackage p2 = new ExcelPackage(fi); //this throws an exception
Are you getting a different exception ?
I followed this answer for my project, but failed to start because of the file not found error.
A first chance exception of type 'System.IO.FileNotFoundException' occurred in System.Drawing.dll
Additional information: File not found.
Here is my code:
PrivateFontCollection modernFont = new PrivateFontCollection();
modernFont.AddFontFile("digital.ttf");
label5.Font = new Font(modernFont.Families[0], 40);
The font file is located right in the namespace directory, is there something wrong with the the file path or else?
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
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.
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)