私はいくつかのテキストと画像で単語の文書を持っています。私はC#を使用して、単語の文書の内容を別の単語の文書にコピーしたいと思います。ある単語の文書の内容を別の単語の文書にコピーする方法は?
ありがとうございました。
私はいくつかのテキストと画像で単語の文書を持っています。私はC#を使用して、単語の文書の内容を別の単語の文書にコピーしたいと思います。ある単語の文書の内容を別の単語の文書にコピーする方法は?
ありがとうございました。
これを試してください。それはトリックを行う必要があります。 これは、すべての内容を最初の文書から2番目の文書にコピーします。両方の文書が存在することを確認してください。機能以下
using (WordprocessingDocument firstDocument = WordprocessingDocument.Open(@"E:\firstDocument.docx", false))
using (WordprocessingDocument secondDocument = WordprocessingDocument.Create(@"E:\secondDocument.docx", WordprocessingDocumentType.Document))
{
foreach (var part in firstDocument.Parts)
{
secondDocument.AddPart(part.OpenXmlPart, part.RelationshipId);
}
}
がオープンする方法を紹介します - Word文書から近いとコピー。
using MsWord = Microsoft.Office.Interop.Word;
private static void MsWordCopy()
{
var wordApp = new MsWord.Application();
MsWord.Document documentFrom = null, documentTo = null;
try
{
var fileNameFrom = @"C:\MyDocFile.docx";
wordApp.Visible = true;
documentFrom = wordApp.Documents.Open(fileNameFrom, Type.Missing, true);
MsWord.Range oRange = documentFrom.Content;
oRange.Copy();
var fileNameTo = @"C:\MyDocFile-Copy.docx";
documentTo = wordApp.Documents.Add();
documentTo.Content.PasteSpecial(DataType: MsWord.WdPasteOptions.wdKeepSourceFormatting);
documentTo.SaveAs(fileNameTo);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (documentFrom != null)
documentFrom.Close(false);
if (documentTo != null)
documentTo.Close();
if (wordApp != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
wordApp = null;
documentFrom = null;
documentTo = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
以下のコードを試してください。これはあなたを助けるかもしれません。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var app = new Microsoft.Office.Interop.Word.Application();
var sourceDoc = app.Documents.Open(@"D:\test.docx");
sourceDoc.ActiveWindow.Selection.WholeStory();
sourceDoc.ActiveWindow.Selection.Copy();
var newDocument = new Microsoft.Office.Interop.Word.Document();
newDocument.ActiveWindow.Selection.Paste();
newDocument.SaveAs(@"D:\test1.docx");
sourceDoc.Close(false);
newDocument.Close();
app.Quit();
Marshal.ReleaseComObject(app);
Marshal.ReleaseComObject(sourceDoc);
Marshal.ReleaseComObject(newDocument);
}
}
}
ファイルを新しいファイルにコピーしますか? – kurakura88