简体   繁体   English

如何在C#中为Microsoft.office.interop.word实现后期绑定?

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

I want to achieve late binding for the following lines of code which are using early binding. 我想为使用早期绑定的以下代码行实现后期绑定。 How can I achieve it in C#? 如何在C#中实现?

When I removed Microsoft.office.interop.word reference, I am getting some errors, which are mentioned in the comments in following code. 当我删除Microsoft.office.interop.word参考时,出现了一些错误,这些错误在以下代码的注释中提到。 Folloowing is the code I am using to convert a word document to pdf. 以下是我用来将Word文档转换为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");

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: 随着C#4中动力学的引入,后期绑定仅是切换到动态绑定器的问题:

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. 就这么简单,您甚至不必将这些可选参数传递给Quit方法。 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 http://msdn.microsoft.com/zh-CN/magazine/ff714583.aspx

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Microsoft.Office.Interop.Word - C# Microsoft.Office.Interop.Word C#Microsoft Word自动化Microsoft.Office.Interop.Word - C# Microsoft Word automation Microsoft.Office.Interop.Word SynonymInfo []适用于C#中的Microsoft.Office.Interop.Word - SynonymInfo[] For Microsoft.Office.Interop.Word in C# 在C#中对Microsoft.Office.Interop.Word进行排序 - Sort Microsoft.Office.Interop.Word in C# C#Microsoft.Office.Interop.Word保护段落 - C# Microsoft.Office.Interop.Word protect the paragraph 使用参考C#添加Microsoft.Office.InterOp.Word - Add Microsoft.Office.InterOp.Word using reference C# 如何使用Microsoft.Office.Interop.Word在C#中从Word文件中查找突出显示的文本? - How to find highlighted text from Word file in C# using Microsoft.Office.Interop.Word? 如何在C#中使用Microsoft.Office.Interop.Word从Word文件(.Docx)中按页获取文本 - How to get text by page from word file (.Docx) using Microsoft.Office.Interop.Word in C# 如何使用microsoft.office.interop.word c#从word文件中获取段落和形状 - How to get paragraphs and shapes from word file using microsoft.office.interop.word c# 如何使用Microsoft.Office.Interop.Word C#在Word文档中插入/获取封面 - How to insert/fetch a cover page in word document using Microsoft.Office.Interop.Word C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM