2011-11-16 63 views
2

我從我的應用程序中插入一些數據在Word文檔中,並再次保存。我現在的代碼對於放在word文檔中的表格中的文本可以正常工作,但它不會獲取不在表格中的文本。例如,單詞文檔的第1頁不在表格中,代碼跳過第1頁並立即轉到第2頁,其中有一個文本放在表格中,並且它正在替換文本。 這裏是我的代碼:從c中的word文檔取代正常的文本#

Document docc = app.Documents.Open(ref path, ref o, ref readOnly, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o); 
docc.Activate(); 
try 
{ 
     foreach (Paragraph p in docc.Paragraphs) 
     { 
      Range rng = p.Range; 
      Style sty = (Style)p.get_Style();      

      if ((bool)rng.get_Information(WdInformation.wdWithInTable) == true) 
      { 
       foreach (Cell c in rng.Cells) 
       { 
        if (rng.Cells.Count > 0) 
        { 
          string testtt = c.Range.Text.ToString(); 
          if (c.Range.Text.ToString().Contains("[Company_Name]\r\a")) 
           // c.Next.Range.Text = "Sacramento"; 
           c.Range.Text = "Sacramento"; 
        } 
       } 
       docc.Save();       
      } 
      docc.Close(ref o, ref o, ref o); 
     } 
} 

我知道這行:

if ((bool)rng.get_Information(WdInformation.wdWithInTable) == true) 

獲取與只表的頁面,但我想知道這樣我可以從網頁中獲取數據沒有表格並修改那裏的文字。

回答

3

首先,要更改每個單元格後保存文件 - 有不需要這樣做。其次,更重要的是你在第一段後關閉文檔,所以下一段(段落)會拋出異常。

我會推薦使用類似休閒代碼的東西,它會將所有出現的「[Company_Name]」替換爲「Sacramento」(在整個文檔中)。

using Word = Microsoft.Office.Interop.Word; 
using System.Reflection; 
using System.Runtime.InteropServices; 

...

object o = Missing.Value; 
object oFalse = false; 
object oTrue = true; 

Word._Application app = null; 
Word.Documents docs = null; 
Word.Document doc = null; 

object path = @"C:\path\file.doc"; 

try 
{ 
    app = new Word.Application(); 
    app.Visible = false; 
    app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone; 

    docs = app.Documents; 
    doc = docs.Open(ref path, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o); 
    doc.Activate(); 

    foreach (Word.Range range in doc.StoryRanges) 
    { 
     Word.Find find = range.Find; 
     object findText = "[Company_Name]"; 
     object replacText = "Sacramento"; 
     object replace = Word.WdReplace.wdReplaceAll; 
     object findWrap = Word.WdFindWrap.wdFindContinue; 

     find.Execute(ref findText, ref o, ref o, ref o, ref oFalse, ref o, 
      ref o, ref findWrap, ref o, ref replacText, 
      ref replace, ref o, ref o, ref o, ref o); 

     Marshal.FinalReleaseComObject(find); 
     Marshal.FinalReleaseComObject(range); 
    } 

    doc.Save(); 
    ((Word._Document)doc).Close(ref o, ref o, ref o); 
    app.Quit(ref o, ref o, ref o); 
} 
finally 
{ 
    if (doc != null) 
     Marshal.FinalReleaseComObject(doc); 

    if (docs != null) 
     Marshal.FinalReleaseComObject(docs); 

    if (app != null) 
     Marshal.FinalReleaseComObject(app); 
} 

有兩個重要的事情:

1)切勿使用兩個點與COM對象:

// might be a problem soon: 
app.Documents.Open(.... 

// better way: 
Documents docs = app.Documents; 
Document doc = docs.Open(... 

2)儘快釋放他們因爲您不需要按相反順序排列它們:

if (doc != null) 
    Marshal.FinalReleaseComObject(doc); 

if (docs != null) 
    Marshal.FinalReleaseComObject(docs); 
+0

謝謝,這就是我所需要的。祝你有美好的一天 – Laziale

0

你在正確的軌道上。您需要添加一個else到您的大型if:

else { 
    if (rng.Text.Contains("[Company_Name]\r\a")) 
      rng.Text = "Sacramento"; 
} 

文本已經是一個字符串,BTW。

這符合現有的代碼(因爲我複製並粘貼),但我想你想的東西有點不同:

rng.Text.Replace ("[CompanyName]","Sacramento"); 
+0

感謝您的幫助,上面的建議幫助我解決了我的問題。祝你有個美好的一天 – Laziale

相關問題