轉(zhuǎn)自原文 xiaoanian, Microsoft.Office.Core 引用以及 Microsoft.Office.Core.MsoTriState 的問題 因?yàn)橐鲆粋€提取ppt文字的工程,第一次接觸Office開發(fā). 以下是源碼: 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); } } } } 代碼倒挺簡單,但是添加引用廢了我半天的力氣。其中,引用中出現(xiàn)以下內(nèi)容: (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足以(如上圖中圈出的那個組件)! 實(shí)踐證明, (1)如果只添加Microsoft.Office 1x.0 Object library,會出現(xiàn)下面的錯誤: 錯誤 1 類型“Microsoft.Office.Core.MsoTriState”在未被引用的程序集中定義。必須添加對程序集“office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”的引用 (2)如果Office和Microsoft.Office 1x.0 Object library都添加,就會出現(xiàn)下面的錯誤: 錯誤 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中都出現(xiàn)了。正確做法:只添加Office引用即可 |
|