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

后端 未结 2 1251
执念已碎
执念已碎 2021-01-18 14:50

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.wor

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-18 15:39

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

提交回复
热议问题