Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve late binding in c# for Microsoft.office.interop.word?

Tags:

c#

I want to achieve late binding for the following lines of code which are using early binding. How can I achieve it in C#?

When I removed Microsoft.office.interop.word reference, I am getting some errors, which are mentioned in the comments in following code. Folloowing is the code I am using to convert a word document to pdf.

    Type wordType = Type.GetTypeFromProgID("Word.Application");

        dynamic Word = Activator.CreateInstance(wordType);


        object oMissing = System.Reflection.Missing.Value;


        DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder");
        FileInfo wordFile = new FileInfo(fileName);

        Word.Visible = false;
        Word.ScreenUpdating = false;

        Object filename = (Object)wordFile.FullName;


        var doc = Word.Documents.Open(ref filename);
        doc.Activate();

        object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");

    /*Getting an error in WdSaveFormat */ 
        object fileFormat = WdSaveFormat.wdFormatPDF;



        doc.SaveAs(ref outputFileName, ref fileFormat);



        /*Getting an error in WdSaveOptions */       
        object saveChanges = WdSaveOptions.wdDoNotSaveChanges;

    /*Getting an error in _Document*/ 
        ((_Document)doc).Close(ref saveChanges);
        doc = null;


        Word.Quit();


        MessageBox.Show("Successfully converted");
like image 295
turbo88 Avatar asked Oct 01 '22 17:10

turbo88


2 Answers

Historically, late binding was possible (and still is!) with reflection:

object word = ...;

object[] args = new object [] { oMissing, oMissing, oMissing };
word.GetType().InvokeMember( "Quit", 
    BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
    null, word, args );

With the introduction of dynamics in C#4, late binding is just a matter of switching to the dynamic binder:

dynamic word = ...;

word.Quit();

It is really as simple as that, you don't even have to pass these optional parameters to the Quit method. The only drawback is that there is no intellisense on the dynamically typed code.

More on that here in the Dino's article:

http://msdn.microsoft.com/en-us/magazine/ff714583.aspx

like image 50
Wiktor Zychla Avatar answered Oct 04 '22 18:10

Wiktor Zychla


Made the following changes, Its working fine now.

    Type wordType = Type.GetTypeFromProgID("Word.Application");

    dynamic Word = Activator.CreateInstance(wordType);


    object oMissing = System.Reflection.Missing.Value;


    DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder");
    FileInfo wordFile = new FileInfo(fileName);

    Word.Visible = false;
    Word.ScreenUpdating = false;

    Object filename = (Object)wordFile.FullName;

    var doc = Word.Documents.Open(ref filename);
    doc.Activate();

    object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");

/*in the WdSaveFormat enum, 17 is the value for pdf format*/ 
    object fileFormat = 17;


    doc.SaveAs(ref outputFileName, ref fileFormat);

 /in the   WdSaveOptions enum, 0 is for Do not save pending changes.*/
    object saveChanges = 0;

    doc.Close(ref saveChanges);
    doc = null;

    Word.Quit();

    MessageBox.Show("Successfully converted");
like image 41
turbo88 Avatar answered Oct 04 '22 18:10

turbo88