I'm using Interop.Word to create a .docx file add some tables, fill it with data and then exporting it to a .pdf.
I keep getting this error:
System.ArgumentNullException: 'Value cannot be null.
Parameter name: pUnk'
All the online discussions regarding this issue are talking about TFS and VS version which I'm not really sure it is related to my problem at all.
I have several try catch in the code and I even commented the error. Yet, I can't put my hand on the reason for the exception!!
//declare word app
word.Application wordapp = new word.Application();
object missing = Missing.Value;//NA Values
word.Document myworddoc = null;
if (File.Exists((string)filename))//check if it exists
{
object readOnly = false;//word settings
object isvisible = false;
wordapp.Visible = false;
//word doc
myworddoc = wordapp.Documents.Open(ref filename, ref missing, ref readOnly, 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);
//open the word
myworddoc.Activate();
//replace key words
importdata(wordapp,myworddoc);
//export the file
try
{
myworddoc.ExportAsFixedFormat(saveas.ToString(), word.WdExportFormat.wdExportFormatPDF);
}
catch (Exception)
{
XtraMessageBox.Show("Please close the opened File!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
// close the doc
// closeWord();
try
{
myworddoc.Close();
wordapp.Quit();
}
catch (Exception)
{
closeWord();
}
XtraMessageBox.Show("Export Complete!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
XtraMessageBox.Show("Export Failed!", "Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
I get the exception immediately after the export is done successfully, no matter what the following line is.