2012-12-20 137 views
9

我正在閱讀單詞文件中的文本,並將替換文本中的某些文本替換爲單詞文件。將單詞文件中的文本複製到新單詞中

var wordApp = new Microsoft.Office.Interop.Word.Application(); 
object file = path; 

object nullobj = System.Reflection.Missing.Value; 

var doc = wordApp.Documents.Open(ref file, ref nullobj, ref nullobj, 
               ref nullobj, ref nullobj, ref nullobj, 
               ref nullobj, ref nullobj, ref nullobj, 
               ref nullobj, ref nullobj, ref nullobj); 
doc.ActiveWindow.Selection.WholeStory(); 

doc.ActiveWindow.Selection.Copy(); 

IDataObject data = Clipboard.GetDataObject(); 
var text =data.GetData(DataFormats.Text); 

所以我必須從原始的Word文件的文本,現在我需要它傳遞給一個新詞文件,該文件不存在的(新文本)。

我試圖

ProcessStartInfo startInfo = new ProcessStartInfo(); 
startInfo.FileName = "WINWORD.EXE"; 
Process.Start(startInfo); 

這將打開它不是在文件系統中這是很好的身體保存新的Word文件。但我不確定如何將文本值傳遞給這個新文件。

更新

上面的代碼運行後我試圖

var wordApp = new Microsoft.Office.Interop.Word.Application();    
var doc = wordApp.ActiveDocument; 

來了「因爲沒有文檔是打開此命令不可用。」

+0

你見過這樣的:HTTP://計算器。 com/questions/5160964/copy-contents-of-word-doc-and-paste-into-another-c-sharp – MUG4N

+0

@ MUG4N:我看到類似的東西[這裏](http://pastebin.com/1sV8es7b),但我不確定什麼是'worddocpromo'。沒有解釋 –

+0

@huMptyduMpty你應該依靠word interop來做這件事,而不是process.start。創建一個新的Word文檔,設置內容,將其保存到另一個位置,現在使用process.start等從那裏打開它。不要忘記妥善處理COM對象 – nawfal

回答

4

這是一個簡單的示例,它將整個文本和格式從一個Word文檔複製到新文檔。在新文檔,文本然後使用詞查找&替換功能代替:

using System; 
using System.Linq; 
using Word = Microsoft.Office.Interop.Word; 

namespace WordCopy 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var fileName = args[0]; 

      var wordApp = new Word.Application(); 
      wordApp.Visible = true; 
      var document = wordApp.Documents.Open(fileName); 

      var newDocument = CopyToNewDocument(document); 

      SearchAndReplaceEverywhere(newDocument, "this", "that"); 
     } 

     static Word.Document CopyToNewDocument(Word.Document document) 
     { 
      document.StoryRanges[Word.WdStoryType.wdMainTextStory].Copy(); 

      var newDocument = document.Application.Documents.Add(); 
      newDocument.StoryRanges[Word.WdStoryType.wdMainTextStory].Paste(); 
      return newDocument; 
     } 

     static void SearchAndReplaceEverywhere(
      Word.Document document, string find, string replace) 
     { 
      foreach (Word.Range storyRange in document.StoryRanges) 
      { 
       var range = storyRange; 
       while (range != null) 
       { 
        SearchAndReplaceInStoryRange(range, find, replace); 

        if (range.ShapeRange.Count > 0) 
        { 
         foreach (Word.Shape shape in range.ShapeRange) 
         { 
          if (shape.TextFrame.HasText != 0) 
          { 
           SearchAndReplaceInStoryRange(
            shape.TextFrame.TextRange, find, replace); 
          } 
         }       
        } 
        range = range.NextStoryRange; 
       } 
      } 
     } 

     static void SearchAndReplaceInStoryRange(
      Word.Range range, string find, string replace) 
     { 
      range.Find.ClearFormatting(); 
      range.Find.Replacement.ClearFormatting(); 
      range.Find.Text = find; 
      range.Find.Replacement.Text = replace; 
      range.Find.Wrap = Word.WdFindWrap.wdFindContinue; 
      range.Find.Execute(Replace: Word.WdReplace.wdReplaceAll); 
     } 
    } 
} 
4

所有你需要做的是這樣的:

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

namespace ConsoleApplication6 
{ 
    class Program 
    { 
     static void Main() 
     { 
      var application = new MSWord.Application(); 
      var originalDocument = application.Documents.Open(@"C:\whatever.docx"); 

      originalDocument.ActiveWindow.Selection.WholeStory(); 
      var originalText = originalDocument.ActiveWindow.Selection; 

      var newDocument = new MSWord.Document(); 
      newDocument.Range().Text = originalText.Text; 
      newDocument.SaveAs(@"C:\whateverelse.docx"); 

      originalDocument.Close(false); 
      newDocument.Close(); 

      application.Quit(); 

      Marshal.ReleaseComObject(application); 
     } 
    } 
} 
相關問題