Microsoft.Office.Core 引用以及 Microsoft.Office.Core.MsoTriState 的問題

weixin_33941350發表於2017-12-26

轉自原文 xiaoanianMicrosoft.Office.Core 引用以及 Microsoft.Office.Core.MsoTriState 的問題

 

因為要做一個提取ppt文字的工程,第一次接觸Office開發.

以下是原始碼:

    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Text;  
    using PowerPoint = Microsoft.Office.Interop.PowerPoint;  
    using Microsoft.Office.Core;  
    using System.Runtime.InteropServices;  
    namespace ConsoleApplication1  
    {  
        public class Program  
        {  
            public static void Main(string[] args)  
            {  
                String fileName = "F://test.ppt";  
                Program.Parse(fileName);  
            }  
            public static void Parse(String fileName)  
            {  
                try  
                {  
                    PowerPoint.Application pa = new PowerPoint.ApplicationClass();  
                    PowerPoint.Presentation pp = pa.Presentations.Open(fileName,  
                                                                                                          Microsoft.Office.Core.MsoTriState.msoTrue,  
                                                                                                          Microsoft.Office.Core.MsoTriState.msoFalse,  
                                                                                                          Microsoft.Office.Core.MsoTriState.msoFalse);  
                    Console.WriteLine("Open Success");  
                    PowerPoint.TextFrame frame;  
                    String text;  
                    foreach (PowerPoint.Slide slide in pp.Slides)  
                    {                      
                        foreach (PowerPoint.Shape shape in slide.Shapes)  
                        {  
                              
                            if (shape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue)  
                            {  
                                frame = shape.TextFrame;  
                                if (frame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue)  
                                {  
                                      
                                    text = frame.TextRange.Text;  
                                    Console.WriteLine(text);  
                                }  
                            }  
                        }  
                    }  
                }  
                catch (Exception e)  
                {                  
                    Console.WriteLine(e.Message);  
                }  
            }  
        }  
    }  

程式碼倒挺簡單,但是新增引用廢了我半天的力氣。其中,引用中出現以下內容:

(1).net中有Microsoft.Office.Interop.PowerPoint, Office

(2) com中有Microsoft.Office 1x.0 Object library

Microsoft.Office.Interop.PowerPoint肯定是要新增的.

但Office和Microsoft.Office 1x.0 Object library新增誰?

只新增Office足以(如上圖中圈出的那個元件)!

 

實踐證明,

(1)如果只新增Microsoft.Office 1x.0 Object library,會出現下面的錯誤:

錯誤 1 型別“Microsoft.Office.Core.MsoTriState”在未被引用的程式集中定義。必須新增對程式集“office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”的引用

(2)如果Office和Microsoft.Office 1x.0 Object library都新增,就會出現下面的錯誤:

錯誤 1 型別“Microsoft.Office.Core.MsoTriState”同時存在於“E:/Program Files/Microsoft Visual Studio 9.0/Visual Studio Tools for Office/PIA/Office12/Office.dll”和“C:/Documents and Settings/Administrator/我的文件/Visual Studio 2008/Projects/PPTPaser/ConsoleApplication1/obj/Debug/Interop.Microsoft.Office.Core.dll”中

原因是MsoTriState在兩個dll中都出現了。正確做法:只新增Office引用即可

 

相關文章