【发布时间】:2020-06-04 06:07:37
【问题描述】:
我有一个带有一些空格的word文件,例如:
Word文件XXXXX
标题:XXXXX
等
我还有另一个 word 文件缺少该数据:
Word 文件 20248
标题:word文件示例
等
我的问题是,如何使用 vba 识别第一个文件中的数据,以便将其复制到我想要的空间中的第二个文件中。此外,我希望您可以使用对话框选择所需的 word 文件,而不是输入文件所在的代码,因为我有不同的文件可以更改位置。
非常感谢您的回答。我是 vba 的新手,我从来没有在 word 上使用过它。
现在我有这个代码来选择我要从中复制数据的 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
我有以下部分代码可以使用书签将 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
【问题讨论】: