简体   繁体   English

select 使用 vba 并复制到另一个文档的末尾并保留格式的一个 Word 文档中的一系列文本

[英]select a range of text from one Word doc using vba and copy to end of another document and RETAIN formatting

I have a document "mydoc1" which has headers "Take the Exam" and "Ask a Question" and within those headers is the selection of text that I want to copy to end of another document, "mydoc2".我有一个文档“mydoc1”,其中包含“参加考试”和“提出问题”标题,在这些标题中选择了要复制到另一个文档“mydoc2”末尾的文本。 However, that selection has particular formatting which I want to retain when I copy and paste into another document.但是,该选择具有特定的格式,当我复制并粘贴到另一个文档中时,我想保留这些格式。 Its working ok EXCEPT the formatting is NOT being preserved when copied.它工作正常,除了复制时不保留格式。

Sub CutSection()
'
' CutSection Macro
'
' Purpose: display the text between (but not including)
' the words "Take the Exam" and "Ask a Question" if they both appear.
Dim rng1 As Range
Dim rng2 As Range

Dim strTheText As String

Documents.Open FileName:="/Users/xxx/Desktop/mydoc1.docx"

Set rng1 = ActiveDocument.Range
If rng1.Find.Execute(FindText:="Take the Exam") Then
    Set rng2 = ActiveDocument.Range(rng1.End, 
    ActiveDocument.Range.End)
    If rng2.Find.Execute(FindText:="Ask a Question") Then
        strTheText = ActiveDocument.Range(rng1.End, rng2.Start).Text
        MsgBox strTheText
    End If
End If

Documents("/Users/xxx/Desktop/mydoc2.docx").Activate
ActiveDocument.Content.InsertAfter strTheText

End Sub

Strings don't contain any formatting data, only text.字符串不包含任何格式数据,只有文本。

You could have simply copied and pasted the text:您可以简单地复制并粘贴文本:

Sub CutSection2()
   Dim doc1 As Document, doc2 As Document
   Dim rng1 As Range, rng2 As Range

   Set doc1 = Documents.Open(FileName:="/Users/xxx/Desktop/mydoc1.docx")
   Set doc2 = Documents("/Users/xxx/Desktop/mydoc2.docx")

   Set rng1 = doc1.Range
   If rng1.Find.Execute(FindText:="Take the Exam") Then
      Set rng2 = doc1.Range(rng1.End, doc1.Range.End)
      If rng2.Find.Execute(FindText:="Ask a Question") Then
         doc1.Range(rng1.End, rng2.Start).Copy
         doc2.Characters.Last.PasteAndFormat wdFormatOriginalFormatting
      End If
   End If
End Sub

Or, the best option, you could use the FormattedText property to transfer the text without using the clipboard.或者,最好的选择是,您可以使用FormattedText属性来传输文本,而无需使用剪贴板。

Sub CutSection3()
   Dim doc1 As Document, doc2 As Document
   Dim rng1 As Range, rng2 As Range

   Set doc1 = Documents.Open(FileName:="/Users/xxx/Desktop/mydoc1.docx")
   Set doc2 = Documents("/Users/xxx/Desktop/mydoc2.docx")

   Set rng1 = doc1.Range
   If rng1.Find.Execute(FindText:="Take the Exam") Then
      Set rng2 = doc1.Range(rng1.End, ActiveDocument.Range.End)
      If rng2.Find.Execute(FindText:="Ask a Question") Then
         doc2.Characters.Last.FormattedText = doc1.Range(rng1.End, rng2.Start).FormattedText
      End If
   End If
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Xcel Vba中从一个Word文档中选择一系列文本,然后复制到另一个Word文档中 - from Xcel Vba select a range of text from one Word document and copy into another Word document 从一个 Word 文档中选择一系列文本并复制到另一个 Word 文档中 - select a range of text from one Word document and copy into another Word document VBA从嵌入式Word文档复制内容并保留格式 - VBA to Copy Contents from Embedded Word document and retain formatting VBA Word 从特定单词复制文本到文档末尾 - VBA Word Copy text from specific word till the end of the document Select 来自一个 Word 文档的部分文本并复制到另一个 Word 文档中 - Select some parts of text from one Word document and copy into another Word document 如何使用vba将一个word文档的内容复制到另一个word文档的末尾? - How do I copy the contents of one word document to the end of another using vba? 将VBA与Powerpoint一起使用可在Word Doc中搜索标题并将文本复制到另一个Word文档中 - Use VBA with Powerpoint to Search titles in a Word Doc and Copy Text into another Word Document VBA 将段落和表格从一个 Word 文档复制和处理到另一个 - VBA to copy and process paragraphs and tables from one Word document to another VBA,正则表达式:从 Word 文档中识别和复制文本以在模板中的新文档中使用 - VBA, Regex: Identifying and copy text from Word doc to use in new document from template 如何将文本从一个文档复制到另一个具有相同格式的文档? - How to copy text from one document to another with same formatting?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM