2

ある単語文書を別の単語文書に動的にコピーしたい。このプロセスは、ボタン クリックで実行できます。ドキュメント (docs) にテキストが含まれているので、ドキュメント (docs2) にコピーしたい

public void ReadMsWord()
    {
        string filePath = null;
        OpenFileDialog file = new OpenFileDialog();
        file.Title = "Word File";
        file.InitialDirectory = "c:\\";
        file.RestoreDirectory = true;
        if (file.ShowDialog() == DialogResult.OK)
        {
            filePath = file.FileName.ToString();
        }
        try
        {
            //Microsoft.Office.Interop.Word.Application Oword = new Microsoft.Office.Interop.Word.Application();
            //Oword.Visible = true;
            var templatepath = filePath;
            var wordapp = new Microsoft.Office.Interop.Word.Application();
            var orgnldoc = wordapp.Documents.Open(templatepath);
            orgnldoc.ActiveWindow.Selection.WholeStory();
            orgnldoc.ActiveWindow.Selection.Copy();
            var newdcmnt=new Microsoft.Office.Interop.Word.Document();
            newdcmnt.ActiveWindow.Selection.Paste();
            newdcmnt.SaveAs(@"C:\Users\Documents\TestDoc2.docx");
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wordapp);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(orgnldoc);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(newdcmnt);
            GC.Collect();
        }
        catch (Exception ex) { MessageBox.Show(ex.ToString()); }
    }
4

1 に答える 1

0

ここに私がテストしたものがあります。Marshal.Release code and GC.Collect

私はオフィス2010を持っていますが、あなたの使用セクションに以下を追加してください

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

これはあなたが実装する方法ですnamespace Aliasing

ここに以下のコードがあります

var fileName = "TestDoc.docx";
Object oMissing = System.Reflection.Missing.Value;
var oTemplatePath = @"C:\Documents\wrkDocuments\" + fileName;
var wordApp = new Word.Application();
var originalDoc = wordApp.Documents.Open(@oTemplatePath);

originalDoc.ActiveWindow.Selection.WholeStory();
originalDoc.ActiveWindow.Selection.Copy();

var newDocument = new Word.Document();
newDocument.ActiveWindow.Selection.Paste();
newDocument.SaveAs(@"C:\Users\Documents\wrkDocuments\TestDoc2.docx");
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(originalDoc);
System.Runtime.InteropServices.Marshal.ReleaseComObject(newDocument);
GC.Collect();

私があなたのために作成したこのメソッドを変更し、適切なパラメーターを渡します

private static void CopyWordDoc()
{
    var fileName = "TestDoc.docx";
    Object oMissing = System.Reflection.Missing.Value;
    var oTemplatePath = @"C:\Documents\wrkDocuments\" + fileName;
    var wordApp = new Word.Application();
    var originalDoc = wordApp.Documents.Open(@oTemplatePath);
    // you can do the line above by passing ReadOnly=False like this as well
    //var originalDoc = wordApp.Documents.Open(@oTemplatePath, oMissing, false);
    originalDoc.ActiveWindow.Selection.WholeStory();
    originalDoc.ActiveWindow.Selection.Copy();

    var newDocument = new Word.Document();
    newDocument.ActiveWindow.Selection.Paste();
    newDocument.SaveAs(@"C:\Documents\wrkDocuments\TestDoc2.docx");
    System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(originalDoc);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(newDocument);
    GC.Collect();
}
于 2013-03-20T15:05:21.863 に答える