简体   繁体   English

Select 来自一个 Word 文档的部分文本并复制到另一个 Word 文档中

[英]Select some parts of text from one Word document and copy into another Word document

I have a word file with some spaces, for example:我有一个带有一些空格的 word 文件,例如:

Word File XXXXX字文件XXXXXX

Title: XXXXX标题:XXXXX

etc ETC

And I have another word file which have that data that is missing:我还有另一个 word 文件,其中缺少该数据:

Word File 20248字文件 20248

Title: Example of word file标题:word文件示例

etc ETC

My question is, how can I use vba to recognize the data from the first file to be copied into the second file in the spaces I want.我的问题是,我怎样才能使用 vba 来识别从第一个文件中复制到我想要的空间中的第二个文件中的数据。 Furthermore I'd prefer that you can select the word file you want with a dialog box rather than putting in the code where the file is located as I have different files that can have the location changed.此外,我更希望您可以使用对话框 select 您想要的 word 文件,而不是放入文件所在的代码,因为我有不同的文件可以更改位置。

Thank you so much for your answers.非常感谢您的回答。 I'm pretty new in vba and I have never used it on word.我是 vba 的新手,我从来没有用过它。

By now I have this code to choose the word file from which I want to copy the data:现在我有这个代码来选择我想从中复制数据的word文件:

Sub CopyData()
 Dim DC As Document
 Dim wD As Document, strD As String, wDNumb As Variant

 Dim I As Long

 Set wD = ActiveDocument
DSelection:
 For I = 1 To Documents.Count
    strD = strD & Documents(I).Name & " - " & I & vbCrLf
 Next I

 wDNumb = InputBox("Please, choose the number of the word file from which you are choosing the data to copy:" & vbCrLf & _
                vbCrLf & strD, "Choose the word document from which you are copying the data!", 1)

If wDNumb <= Documents.Count And wDNumb >= 1 Then
    GoTo DSelection2
ElseIf wDNumb = "" Then MsgBox "Operation cancelled", vbCritical, "Cancelled"
    Exit Sub
ElseIf wDNumb > Documents.Count Or wDNumb < 1 Then MsgBox "Wrong number, input a correct number", vbExclamation, "Wrong number"
    Exit Sub
End If

DSelection2:
If IsNumeric(wDNumb) Then
    Set DC = Documents(CLng(wDNumb))
Else
    MsgBox "Please choose the number on the right of the document chosen!": GoTo DSelection
End If
End Sub

I have the following part of the code to copy some part of the Word to the other using bookmarks:我有以下代码部分,可以使用书签将 Word 的某些部分复制到另一个部分:

DC.Activate

Set Rng = DC.Range
With Rng.Find
    .ClearFormatting
    .Execute FindText:="TITLE:", Forward:=True, _
             Format:=False, Wrap:=wdFindStop
    Fnd = .Found
End With

If Fnd = True Then
    With Rng
        .MoveStart wdCharacter, 10
        .MoveEnd wdSentence, 1
    End With
End If

Rng.Select
Selection.Copy

wD.Activate
Selection.GoTo What:=wdGoToBookmark, Name:="TITLE"
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Paste

There are multiple possible ways of approaching this, but your problem description lacks sufficient detail.有多种可能的方法来解决这个问题,但您的问题描述缺乏足够的细节。 For example, one could insert:例如,可以插入:

  • bookmarks;书签;
  • content controls;内容控制;
  • Section breaks;分节符;
  • tables;表格;
  • etc., ETC。,

into the target document so that content from the source document can be inserted there.到目标文档中,以便可以在其中插入源文档中的内容。

Alternatively, one might use Find/Replace to locate a predefined string that can be replaced with the desired content.或者,可以使用查找/替换来查找可以替换为所需内容的预定义字符串。

With your updated problem description, you might use:使用更新的问题描述,您可以使用:

Dim RngDC As Range, wDRng As Range, BkMkNm As String
BkMkNm "TITLE"
With DC
  With .Range.Find
    .ClearFormatting
    .Execute FindText:=BkMkNm, Forward:=True, Format:=False, Wrap:=wdFindStop
  End With
  If .Found = True Then
    .MoveStart wdCharacter, 10
    .MoveEnd wdSentence, 1
    Set RngDC = .Duplicate
  End If
End With
With wD
  Set wDRng = .Bookmarks(BkMkNm).Range
  wDRng.FormattedText = RngDC.FormattedText
  .Bookmarks.Add BkMkNm, wDRng
End With

暂无
暂无

声明:本站的技术帖子网页,遵循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 select 使用 vba 并复制到另一个文档的末尾并保留格式的一个 Word 文档中的一系列文本 - select a range of text from one Word doc using vba and copy to end of another document and RETAIN formatting 使用 Word 宏/VBA 将表格从一个 Word 文档复制到另一个 Word 文档 - Use Word Macro/VBA to Copy Tables from One Word Document to Another Word Document 使用vba将ComboBox从Word文档复制到另一个Word文档 - Copy ComboBox from a Word Document to another Word Document with vba VBA 将段落和表格从一个 Word 文档复制和处理到另一个 - VBA to copy and process paragraphs and tables from one Word document to another 如何将文本从Word文档转换为另一个Word文档 - How to get text from a Word document into another Word Document 试图从另一个 Word 文档控制一个 Word 文档 - Trying to control one Word document from another Word document 复制word文档中所有突出显示的文本并将其粘贴到另一个文档中 - copy all highlighted text in a word document and paste it to another document 将文本从Excel中的范围复制到Word文档 - Copy Text from Range in Excel into Word Document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM