I am converting a doc file to a pdf file but with my current code it seems that the file is open even after the conversion. I see the pdf file in the output folder but if i try to reupload it tells me that it is opened in another program(which i don’t see anywhere).
Code section error:
if (getExt == ".doc")
{
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
wordDocument = appWord.Documents.Open(DocumentUNCPath.Text);
wordDocument.ExportAsFixedFormat(@"c:\temp\DocTo.pdf", WdExportFormat.wdExportFormatPDF);
}
The full method:
private void btnSubmitStep2_Click(object sender, EventArgs e)
{
string getExt = Path.GetExtension(DocumentUNCPath.Text);
if (getExt == ".doc")
{
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
wordDocument = appWord.Documents.Open(DocumentUNCPath.Text);
wordDocument.ExportAsFixedFormat(@"c:\temp\DocTo.pdf", WdExportFormat.wdExportFormatPDF);
}
// Frame up the record of submission
using (var dc = new DocMgmtDataContext())
{
DocumentLibrary.Document doc = new DocumentLibrary.Document()
{
LibraryID = (AssignmentListStep2.SelectedItem as Library).ID,
OwnedByUserID = (StudentListStep2.SelectedItem as User).ID,
UploadedByUserID = (StudentListStep2.SelectedItem as User).ID,
UploadDT = DateTime.UtcNow,
ID = Guid.NewGuid()
};
dc.Documents.InsertOnSubmit(doc);
dc.SubmitChanges();
// Copy file into managed storage
doc.StoragePath = FILESTORELOCATION + doc.ID + ".pdf";
File.Copy(DocumentUNCPath.Text, doc.StoragePath);
doc.Pages = CompatiblePdfReader.VerifyAndFixPdfDocument(doc.StoragePath);
dc.SubmitChanges();
}
// Refresh the list of student submissions
UpdateStudentSubmissionGrid();
}
Try closing the document at the end of your sub with
wordDocument.Close SaveChanges:=wdDoNotSaveChanges, asExportAsFixedFormatdoesn’t close the file for you.