0
votes

I have a word file with some spaces, for example:

Word File XXXXX

Title: XXXXX

etc

And I have another word file which have that data that is missing:

Word File 20248

Title: Example of word file

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. 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.

Thank you so much for your answers. I'm pretty new in vba and I have never used it on word.

By now I have this code to choose the word file from which I want to copy the data:

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:

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
1

1 Answers

0
votes

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.,

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