将格式化文本从一个文档复制到另一个文档 [英] Copying Formatted Text from One Document To Another

查看:85
本文介绍了将格式化文本从一个文档复制到另一个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的示例来重现我的问题。 我有一个MS Word 2010文档,有两个段落:
I have created a simple sample to recreate my problem.  I have a MS Word 2010 document with two paragraphs:

推荐答案

我希望更有经验的Word对象模型开发人员能够耐心等待我使用这个富有的笨拙的尝试对象模型。坦率地说,我感到非常迷茫并欢迎一些指向一些好的资源的指示,这些资源可以显示一些示例代码
,而不是相当简单的样本是MSDN。



我使用Find对象并使用空字符串(""")替换自定义标记,使用下面的代码解决了部分问题。但我还有一个额外的"\r"返回到新文档中粘贴到内容控件的结尾。如果有人能告诉我如何设置源Range的结束范围来截断"\r",我会非常感激b $ b。在我复制过去之前,我最终只在内容控制中使用了一个段落。



我怀疑我必须设置我要复制的源范围然后再做Range.Copy()后跟oContentControl.Range.PasteSpecial()。也许我会尽快解决这个问题,但如果有人知道快速回答我会非常感激。




代码:



I hope the more experienced Word object model developers will have patience with my rather bumbling attempts to use this rich object model. Frankly, I feel very lost and would welcome some pointers to some good recources that can show some sample code beyond the rather simplistic samples is MSDN.

I have solved part of my problem with the code below using the Find object and replacing the custom markup with an empty string (""). But I stil have an extra "\r" return on the end that is getting Pasted into the Content Control in the new document. I would really appreciate if someone could show me how to either set the source Range's end range to truncate the "\r" before I do the copy past so I end up with only one Paragraph in the Content Control.

I suspect I must set the source Range I am copying from and then do the Range.Copy() followed by the oContentControl.Range.PasteSpecial(). Perhaps I will solve this soon, but if anyone knows a quick answer I would be very grateful.


Code:


private void btnTest4_Click(object sender, EventArgs e)
        {
            MyWord.Application oWordAppSource = null;
            MyWord.Document oWordDocSource = null;

            MyWord.Application oWordAppTarget = null;
            MyWord.Document oWordDocTarget = null;

            try
            {
                MyWord.ContentControl oContentControl = null;
                MyWord.Range oEndOfDocRng = null;
                object oEndOfDoc = "\\endofdoc";
                object oRng;

                string wordDocName1 = "paper.test.docx";
                string wordDocName2 = "paper.test2.docx";

                object fileName1 = wordDocumentRawDir + wordDocName1;
                object fileName2 = basePath + wordDocName2;

                _wp.ReadOnly = false;
                _wp.Visible = true;
                _wp.Replace = MyWord.WdReplace.wdReplaceAll;
                _wp.ScreenUpdating = true;
                _wp.SaveChanges = MyWord.WdSaveOptions.wdDoNotSaveChanges;

                UBCommon.LoadDocument(fileName1, ref oWordAppSource, ref oWordDocSource, _wp);

                this.PaperName = oWordDocSource.Paragraphs[1].Range.Text.Replace("<section.0>", "").Trim();

                // Create a reference to Microsoft Word application
                oWordAppTarget = new MyWord.Application(SaveChanges: MyWord.WdSaveOptions.wdSaveChanges);
                oWordAppTarget.Visible = true;

                // Creates a reference to a Word document
                oWordDocTarget = oWordAppTarget.Documents.Add();
                oEndOfDocRng = oWordDocTarget.Bookmarks.get_Item(ref oEndOfDoc).Range;

                Regex rgxSectionML = new Regex("^<section.\\d*>");
                Regex rgxPageML = new Regex("^<ppg.\\d*>");

                object replace = MyWord.WdReplace.wdReplaceAll;

                foreach (MyWord.Paragraph para in oWordDocSource.Paragraphs)
                {
                    MyWord.Range newRange = null;

                    string text = para.Range.Text;

                    if (rgxSectionML.IsMatch(text))
                    {
                        string s = rgxSectionML.Match(text).Value;

                        MyWord.Find f = para.Range.Find;
                        f.Text = s;
                        f.MatchWholeWord = false;
                        f.Replacement.Text = "";
                        if (f.Execute(Replace: replace))
                        {
                            MessageBox.Show(String.Format("Replaced {0} at position {1},{2}.",
                                f.Text, para.Range.Start, para.Range.End));
                        }
                        text = text.Replace(rgxSectionML.Match(text).Value, "").Trim();
                    }
                    else if (rgxPageML.IsMatch(text))
                    {
                        string s = rgxPageML.Match(text).Value;

                        MyWord.Find f = para.Range.Find;
                        f.Text = s;
                        f.MatchWholeWord = true;
                        f.Replacement.Text = "";
                        if (f.Execute(Replace: replace))
                        {
                            MessageBox.Show(String.Format("Replaced {0} at position {1},{2}.",
                                f.Text, para.Range.Start, para.Range.End));
                        }
                        text = text.Replace(rgxPageML.Match(text).Value, "").Trim();
                    }

                    oRng = oWordDocTarget.Bookmarks.get_Item(ref oEndOfDoc).Range;

                    oContentControl = oWordDocTarget.ContentControls.Add(Range: oRng);
                    oContentControl.Title = "title";
                    oContentControl.Tag = "tag";

                    para.Range.Copy();
                    oContentControl.Range.PasteSpecial();

                    oContentControl.Range.Font.Color = MyWord.WdColor.wdColorBlack;
                    oContentControl.Range.ParagraphFormat.Alignment = MyWord.WdParagraphAlignment.wdAlignParagraphJustify;

                    oContentControl.LockContentControl = true;

                    oRng = oWordDocTarget.Bookmarks.get_Item(ref oEndOfDoc).Range;
                    MyWord.Paragraph oParagraph = oWordDocTarget.Content.Paragraphs.Add(Range: oRng);
                    oRng = oWordDocTarget.Bookmarks.get_Item(ref oEndOfDoc).Range;

                }
                oWordDocTarget.SaveAs(fileName2);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                _wp.SaveChanges = MyWord.WdSaveOptions.wdDoNotSaveChanges;
                UBCommon.CloseDocument(ref oWordAppSource, ref oWordDocSource, _wp);
                _wp.SaveChanges = MyWord.WdSaveOptions.wdSaveChanges;
                UBCommon.CloseDocument(ref oWordAppTarget, ref oWordDocTarget, _wp);
            }
        }


这篇关于将格式化文本从一个文档复制到另一个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆