使用Office Automation,将TextFrame形状导出为Rtf

时间:2013-10-24 18:09:48

标签: automation export powerpoint rtf

我正在编写一个应用程序打开powerpoint演示文稿,找到形状列表,并将TextFrame的内容转换为Rtf。我很容易从TextFrame中获取文本。以下代码效果很好。

string strFilePath = @"i:\work\test1.pptx";

PowerPoint.Application PowerPoint_App = new PowerPoint.Application();
try
{
   PowerPoint_App.Visible = Office.MsoTriState.msoFalse;
}
catch { }
PowerPoint.Presentations multi_presentations = PowerPoint_App.Presentations;
PowerPoint.Presentation presentation = multi_presentations.Open(strFilePath, Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse, Office.MsoTriState.msoFalse);

for (int i = 0; i < presentation.Slides.Count; i++)
{
   foreach (PowerPoint.Shape shape in presentation.Slides[i + 1].Shapes)
   {
      switch (shape.Type)
      {
         case Office.MsoShapeType.msoTextBox:
            if (shape.TextFrame.TextRange.Text.Length > 1)
            {
               Console.WriteLine("(x=" + shape.Left.ToString() + ", y=" + shape.Top.ToString() + ", 
                     w=" + shape.Width.ToString() + ", h=" + shape.Height.ToString() + ")\n" + 
                     shape.TextFrame.TextRange.Text + "\n");
            }
            break;
      }
   }
}

PowerPoint_App.Quit();

但是我需要将TextFrame的内容作为Rtf数据输出。我尝试过以下方法:

shape.TextFrame.TextRange.Copy();
string strRtfData = Clipboard.GetText(TextDataFormat.Rtf);

在此之后,字符串为空。我想要的是在字符串中包含Rtf数据。

如果您有澄清问题,请与我们联系。

1 个答案:

答案 0 :(得分:0)

您需要在复制前选择一些内容:

编辑:我猜这是正确的链接:

http://msdn.microsoft.com/en-us/library/ff746287%28v=office.14%29.aspx