有没有复制word文档的简单方法。使用C#转换成另一个?
如何使用C#复制一个word文档的内容并将其插入另一个预先存在的word文档中。我环顾了一下四周,但一切看起来都很复杂(我是个新手)。肯定有一个简单的解决办法 我已经找到了这个代码,它没有给我任何错误,但它似乎没有做任何事情。这肯定不是复制到正确的单词doc中。这样说吧有没有复制word文档的简单方法。使用C#转换成另一个?,c#,ms-word,copy,office-interop,paste,C#,Ms Word,Copy,Office Interop,Paste,如何使用C#复制一个word文档的内容并将其插入另一个预先存在的word文档中。我环顾了一下四周,但一切看起来都很复杂(我是个新手)。肯定有一个简单的解决办法 我已经找到了这个代码,它没有给我任何错误,但它似乎没有做任何事情。这肯定不是复制到正确的单词doc中。这样说吧 Word.Application oWord = new Word.Application(); Word.Document oWordDoc = new Word.Document(); Object oMissing =
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);
注:这些word文档是.doc很有趣,看到所有的C#家伙现在都在问VBA开发人员15年来一直在回答的问题。如果您必须处理Microsoft Office自动化问题,那么值得深入研究VB6和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);
}
注意:我在这个例子中使用了Framework4.0,它允许可选参数。有答案吗?上面的代码是垃圾还是…?是否要将两个单词的文档合并为单个文档?哪种文档格式。doc或.docx?doc或.docx,有区别吗?
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);