Question

A few months ago while finishing up school I created a basic application that coverts word and excel documents to PDF. The client I developed the app for needs it to be able to use any version of word so thats when I discovered I would need to use late binding. Now the app works perfectly using office interop but I am having trouble with converting everything over to use late binding instead.

Ive got a word document to open but I run into a problem with the following.

Type wordType = Type.GetTypeFromProgID("Word.Application");
if(wordType == null)
    throw new Exception(message);

dynamic wordApplication = null;
wordApplication = Activator.CreateInstance(wordType);
if(wordApplication == null)
    throw new Exception(message);

dynamic wordDocument = null;
object paramSourceDocPath = sourceDocPath;
object paramMissing = Type.Missing;

WdExportFormat targetFormat = WdExportFormat.wdExportFormatPDF;
WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForOnScreen;
WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;

try
{
    // Open the source document.
    wordDocument = wordApplication.Documents.Open(ref paramSourceDocPath, ref paramMissing, ref paramMissing, ref paramMissing,
        ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing,
        ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing);

    // Export it in the specified format.
    if (wordDocument != null)
        wordDocument.ExportAsFixedFormat(targetFilePath, targetFormat, openAfter, paramExportOptimizeFor,
            paramExportRange, paramStartPage, paramEndPage, paramExportItem, true, true, paramCreateBookmarks, true,
            true, false, ref paramMissing);
}

The WdExportXXXXXX items cannot be resolved because I removed the reference to the office interop assembly. I have never really used late binding and I have no idea how to resolve these types. Im hoping once I get that that resolved the ExportAsFixedFormat function call will work.

Was it helpful?

Solution

I found NetOffice to work perfectly for the task at hand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top