2012-12-10 84 views
0

我一直在努力與C#和PowerPoint,我早前發佈關於如何使用C#更新ppt中的鏈接,如果鏈接設置爲手動,我沒有得到任何迴應,所以我想我會嘗試通過在文件中將鏈接設置爲自動來避免這個問題,然後當C#打開它時,它會更新它們,保存文件,然後將它們分解並保存爲另一個文件名,但這並沒有證明更容易。Break Links PowerPoint 2010

所有我需要知道的是如何打破鏈接。我知道一些VBA並編寫了一段代碼來打破它們,但是當我用C#調用一個RunMacro方法的宏時,它似乎不能與我正在使用的方法一起工作(? - 我是C#的新手,所以我不會不完全理解這是爲什麼,但如果你Google「運行宏觀PowerPoint C#」,你會發現我確信我試圖去做它的方式。)請幫助,我完全失去了。

我的腳本看起來像這樣

Using PowerPoint = Microsoft.Office.Interop.PowerPoint; 

public void Main() 
    { 

     PowerPoint.Application ppt = new PowerPoint.Application(); 
     PowerPoint.Presentation PRS = ppt.Presentations.Open(@"Filename.pptm", 
     Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue); 


     PRS.UpdateLinks(); 
     PRS.Save 
     //here is where I need to break the links 
     PRS.SaveAs(@"filename with links broken.pptm", 
      Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentationMacroEnabled, MsoTriState.msoTrue); 
     PRS.Close(); 
     ppt.Quit(); 
} 

我試圖打開文件之前設置linkformat爲手動,但這並不影響已經創建的任何形狀,只有程序中創建之後新的。

回答

-1

打開文件之前更改內容對當然不打開的文件沒有影響。通過對每張幻燈片的形狀集合和每個形狀迭代:

If .Type = msoLinkedOLEObject Then 
    .LinkFormat.BreakLink 
End If 

msoLinkedOLEObject = 10

1

我已經做了在Powerpoint中一個類似的項目也涉及到的鏈接斷開。在我的程序中,我正在從Excel文件中讀取數據並將其存入Powerpoint演示文稿。在我的Powerpoint模板文件中,我已將所有鏈接到我的Excel文件,並按照我希望的格式進行格式化。該程序開始運行,並填充Excel文件。寫完Excel後,我將Powerpoint和UpdateLinks()打開到我的演示文稿中。一旦鏈接更新,我將使用Powerpoint中的Shapes中的for循環來斷開鏈接。之後,我保存並關閉文檔。下面是我用來根據每個ppt文件模板創建我的Powerpoint文件的函數(對於我的示例,我遍歷多個ppt文件,因爲每個ppt文件只包含一張幻燈片),稍後將它們合併爲一個幻燈片處理)。

public void CreatePowerpoint() 
    { 
     string[] fileArray = Directory.GetFiles(@"C:\Users\Me\Desktop\test"); 
     Microsoft.Office.Interop.PowerPoint.Application pptApp = new Microsoft.Office.Interop.PowerPoint.Application(); 
     for (int i = 0; i < fileArray.Length; i++) 
     { 
      string file = fileArray[i]; 

      Microsoft.Office.Interop.PowerPoint.Presentation powerpoint = pptApp.Presentations.Open(file); 
      powerpoint.UpdateLinks(); 

      Microsoft.Office.Interop.PowerPoint.Slides slides = powerpoint.Slides; 
      Microsoft.Office.Interop.PowerPoint.Slide slide = slides[1]; 
      Microsoft.Office.Interop.PowerPoint.Shapes pptShapes = (Microsoft.Office.Interop.PowerPoint.Shapes)slide.Shapes; 

      foreach (Microsoft.Office.Interop.PowerPoint.Shape y in pptShapes) 
      { 
       Microsoft.Office.Interop.PowerPoint.Shape j = (Microsoft.Office.Interop.PowerPoint.Shape)y; 
       try 
       { 
        //If auto link update is disabled for links 
        //j.LinkFormat.j.Update(); 
        if (j.LinkFormat != null) 
        { 
         j.LinkFormat.BreakLink(); 
        } 
       } 
       catch (Exception) 
       { 

       } 
      } 

      powerpoint.SaveAs(@"C:\Users\Me\Desktop\test" + i + ".pptx"); 
      powerpoint.Close(); 
      Thread.Sleep(3000); 
     } 
     pptApp.Quit(); 
    }