有没有简单的方法复制word文档?用c#转换成另一个
本文关键字:转换 另一个 文档 简单 方法 复制 word 有没有 | 更新日期: 2023-09-27 17:52:13
如何使用c#复制一个word文档的内容并将其插入到另一个已有的word文档中?我四处看了看,但一切看起来都很复杂(我是个新手)。肯定有一个简单的解决办法吧?
我发现这段代码给我没有错误,但它似乎没有做任何事情。它当然没有复制到正确的word文档中。这么说吧。
Word.Application oWord = new Word.Application();
Word.Document oWordDoc = new Word.Document();
Object oMissing = System.Reflection.Missing.Value;
oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
oWordDoc.ActiveWindow.Selection.WholeStory();
oWordDoc.ActiveWindow.Selection.Copy();
oWord.ActiveDocument.Select();
oWordDoc.ActiveWindow.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
p。这些word文档是。doc
Word.Application oWord = new Word.Application();
Word.Document oWordDoc = new Word.Document();
Object oMissing = System.Reflection.Missing.Value;
object oTemplatePath = @"C:''Documents and Settings''Student''Desktop''ExportFiles''" + "The_One.docx";
oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
oWordDoc.ActiveWindow.Selection.WholeStory();
oWordDoc.ActiveWindow.Selection.Copy();
oWord.ActiveDocument.Select();
// The Visible flag is what you've missed. You actually succeeded in making
// the copy, but because
// Your Word app remained hidden and the newly created document unsaved, you could not
// See the results.
oWord.Visible = true;
oWordDoc.ActiveWindow.Selection.PasteAndFormat(Word.WdRecoveryType.wdPasteDefault);
看到现在所有c#的人都在问VBA开发人员已经回答了15年的问题,这很有趣。如果你必须处理Microsoft Office自动化问题,那么值得深入研究VB 6和VBA代码示例。
对于"什么也没发生"这一点很简单:如果您通过自动化启动Word,则必须将应用程序也设置为可见。如果你运行你的代码,它会工作,但Word仍然是一个不可见的实例(打开Windows任务管理器可以看到它)。
对于点"简单的解决方案",您可以尝试在给定范围内插入一个文档,使用该范围的InsertFile方法,例如:
static void Main(string[] args)
{
Word.Application oWord = new Word.Application();
oWord.Visible = true; // shows Word application
Word.Document oWordDoc = new Word.Document();
Object oMissing = System.Reflection.Missing.Value;
oWordDoc = oWord.Documents.Add(ref oMissing);
Word.Range r = oWordDoc.Range();
r.InsertAfter("Some text added through automation!");
r.InsertParagraphAfter();
r.InsertParagraphAfter();
r.Collapse(Word.WdCollapseDirection.wdCollapseEnd); // Moves range at the end of the text
string path = @"C:'Temp'Letter.doc";
// Insert whole Word document at the given range, omitting page layout
// of the inserted document (if it doesn't contain section breakts)
r.InsertFile(path, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
}