2012-12-20 28 views
9

Tôi đang đọc văn bản từ tệp từ và thay thế một số văn bản từ văn bản đã đọc.Sao chép văn bản từ tệp từ vào một từ mới

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); 

Vì vậy, tôi có văn bản từ tệp gốc, và bây giờ tôi cần nó để chuyển sang tệp từ mới không tồn tại (Văn bản mới).

tôi đã cố gắng

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

này mở file word mới mà không được lưu chất trong hệ thống tập tin mà là tốt. Nhưng tôi không chắc chắn làm thế nào có thể vượt qua giá trị văn bản cho tập tin mới này.

Cập nhật

Sau khi chạy trên mã tôi đã cố gắng

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

nào đi kèm với "Lệnh này không có sẵn vì không có tài liệu đang mở."

+0

Bạn đã thấy điều này: http: // stackoverflow. com/questions/5160964/copy-content-of-word-doc-và-paste-into-another-c-sharp – MUG4N

+0

@ MUG4N: Tôi thấy một cái gì đó tương tự [ở đây] (http://pastebin.com/1sV8es7b), nhưng tôi không chắc 'worddocpromo' là gì. Không có giải thích –

+0

@huMptyduMpty bạn nên dựa vào từ interop để làm điều này thay vì process.start. Tạo một tài liệu word mới, thiết lập nội dung, lưu nó vào một vị trí khác, bây giờ mở nó từ đó bằng cách sử dụng process.start hoặc như vậy. Đừng quên vứt bỏ các đối tượng com đúng cách – nawfal

Trả lời

4

Đây là mẫu đơn giản sao chép toàn bộ văn bản và định dạng từ một tài liệu Word sang tài liệu mới. Trong tài liệu mới, văn bản sau đó được thay thế bằng từ Tìm & Thay tính năng:

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

Tất cả bạn cần làm là:

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); 
     } 
    } 
}