本文介绍了如何实现下旬在C#中的Microsoft.Office.Interop.Word约束力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现下面的代码线,使用早期绑定后期绑定。我如何在C#中实现呢?



当我删除的Microsoft.Office.Interop.Word参考,我得到了一些错误,这是在评论在下面的代码中提到。
Folloowing是我使用转换Word文档为PDF的代码。

 键入wordType = Type.GetTypeFromProgID( Word.Application); 

动感字= Activator.CreateInstance(wordType);


对象oMissing = System.Reflection.Missing.Value;


DirectoryInfo的dirInfo =新的DirectoryInfo(@\\server\folder);
FileInfo的wordFile =新的FileInfo(文件名);

Word.Visible = FALSE;
Word.ScreenUpdating = FALSE;

目标文件名=(对象)wordFile.FullName;


VAR DOC = Word.Documents.Open(REF文件名);
doc.Activate();

对象outputFileName = wordFile.FullName.Replace(DOC,.PDF);

/ *获得一个错误WdSaveFor​​mat * /
对象FILEFORMAT = WdSaveFor​​mat.wdFormatPDF;



doc.SaveAs(REF outputFileName,裁判FILEFORMAT);



/ *获得一个错误WdSaveOptions * /
的SaveChanges对象= WdSaveOptions.wdDoNotSaveChanges;

/ *获得一个错误_document * /
((_document)DOC).Close(REF的SaveChanges);
DOC = NULL;


Word.Quit();


MessageBox.Show(转换成功);


解决方案

做了以下变化时,其现在工作的罚款。

 键入wordType = Type.GetTypeFromProgID(Word.Application); 

动感字= Activator.CreateInstance(wordType);


对象oMissing = System.Reflection.Missing.Value;


DirectoryInfo的dirInfo =新的DirectoryInfo(@\\server\folder);
FileInfo的wordFile =新的FileInfo(文件名);

Word.Visible = FALSE;
Word.ScreenUpdating = FALSE;

目标文件名=(对象)wordFile.FullName;

VAR DOC = Word.Documents.Open(REF文件名);
doc.Activate();

对象outputFileName = wordFile.FullName.Replace(DOC,.PDF);

/ *在WdSaveFor​​mat枚举,17是pdf格式的值* /
对象FILEFORMAT = 17;


doc.SaveAs(REF outputFileName,裁判FILEFORMAT);

/在WdSaveOptions枚举,0是不保存未决更改* /
的SaveChanges对象= 0。

doc.Close(REF的SaveChanges);
DOC = NULL;

Word.Quit();

MessageBox.Show(转换成功);


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");
解决方案

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

这篇关于如何实现下旬在C#中的Microsoft.Office.Interop.Word约束力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 08:28