2015-09-17 51 views
0

我有一個要求以編程方式(在C#中)打開或關閉特定段落的縮進縮進。c#Word Interop - 設置段落縮進

我已經創建了一個加載項,並且有一個按鈕,當點擊它時,我會執行代碼(嘗試)執行此操作。它是一個切換,所以第一次點擊添加了懸掛縮進,第二次點擊應該刪除它。

在字,其在段落>縮進設定,接着由上述設定 「特別」 等於

enter image description here

我最擅長此嘗試用下面的代碼:

foreach (Footnote rngWord in Globals.OSAXWord.Application.ActiveDocument.Content.Footnotes) 
    rngWord.Range.ParagraphFormat.TabHangingIndent(
     rngWord.Range.ParagraphFormat.FirstLineIndent == 0 ? 1 : -1); 

它僅修正段落出於某種原因的最後一行。我需要它是除了第一個之外的所有行。我究竟做錯了什麼?

修改:

enter image description here

注 - 實際上,我在我的文檔腳註上執行此。

+0

莫非樣的人誰投下來,我的問題是慷慨地給予一些實際的反饋而不是僅僅是至關重要的! –

回答

2

對於任何人誰可能碰巧遇到這一點 - 這裏是我如何解決:

try 
{ 
    // Loop through each footnote in the word doc. 
    foreach (Footnote rngWord in Microsoft.Office.Interop.Word.Application.ActiveDocument.Content.Footnotes) 
    { 
     // For each paragraph in the footnote - set the indentation. 
     foreach (Paragraph parag in rngWord.Range.Paragraphs) 
     { 
      // If this was not previously indented (i.e. FirstLineIndent is zero), then indent. 
      if (parag.Range.ParagraphFormat.FirstLineIndent == 0) 
      { 
       // Set hanging indent. 
       rngWord.Range.ParagraphFormat.TabHangingIndent(1); 
      } 
      else 
      { 
       // Remove hanging indent. 
       rngWord.Range.ParagraphFormat.TabHangingIndent(-1); 
      } 
     } 
    } 
    // Force the screen to refresh so we see the changes. 
    Microsoft.Office.Interop.Word.Application.ScreenRefresh(); 

} 
catch (Exception ex) 
{ 
    // Catch any exception and swollow it (i.e. do nothing with it). 
    //MessageBox.Show(ex.Message); 
} 
+1

非常感謝您發佈解決方案 –