![]() |
|
|
|
#1
|
|||
|
|||
|
I drew this up. See if you can adjust the variables to make it work. The arrays, arr1 and arr2, will ensure that the country names remain in order when pasting.
Code:
Sub GetCountriesFrom2Docs_AddToNewDoc()
Dim doc_name1 As String: doc_name1 = "c:\scratch\Doc1.docx"
Dim doc1 As Document: Set doc1 = Documents.Open(doc_name1)
Dim rng1 As Range: Set rng1 = doc1.Content
Dim arr1(1 To 1000)
Dim doc_name2 As String: doc_name2 = "c:\scratch\Doc2.docx"
Dim doc2 As Document: Set doc2 = Documents.Open(doc_name2)
Dim rng2 As Range: Set rng2 = doc2.Content
Dim arr2(1 To 1000)
Dim new_doc_name As String: new_doc_name = "c:\scratch\NewDoc.docx"
Dim new_doc As Document: Set new_doc = Documents.Add
Dim new_doc_rng As Range: Set new_doc_rng = new_doc.Content
new_doc.SaveAs2 new_doc_name
Dim x As Integer
x = 1
With rng1.Find
.ClearFormatting
.Forward = True
.Text = "\(*\)"
.MatchWildcards = True
.Execute
Do While .Found
arr1(x) = Mid(rng1.Text, 2, Len(rng1.Text) - 2)
x = x + 1
.Execute
Loop
End With
doc1.Close
x = 1
With rng2.Find
.ClearFormatting
.Forward = True
.Text = "\(*\)"
.MatchWildcards = True
.Execute
Do While .Found
arr2(x) = Mid(rng2.Text, 2, Len(rng2.Text) - 2)
x = x + 1
.Execute
Loop
End With
doc2.Close
new_doc_rng.Text = "Countries from Doc1:" & vbCrLf
new_doc_rng.Move wdParagraph
For x = 1 To UBound(arr1)
If arr1(x) <> "" Then
new_doc_rng.Text = arr1(x) & vbCrLf
new_doc_rng.Move wdParagraph
End If
Next x
new_doc_rng.Text = vbCrLf & vbCrLf
new_doc_rng.Text = "Countries from Doc2:" & vbCrLf
new_doc_rng.Move wdParagraph
For x = 1 To UBound(arr2)
If arr2(x) <> "" Then
new_doc_rng.Text = arr2(x) & vbCrLf
new_doc_rng.Move wdParagraph
End If
Next x
new_doc.SaveAs2 new_doc_name
End Sub
|
|
#2
|
|||
|
|||
|
I have set the path for the 2 Docs to be searched, It starts searching but I get an empty new Doc. Not even the text in () is extracted.
Do I also have to enter the list of countries in new_doc_rng.Text = "Countries from Doc1:" & vbCrLf and new_doc_rng.Text = "Countries from Doc2:" & vbCrLf |
|
#3
|
|||
|
|||
|
Something like this. In the Locals window, you can see that arr1 is filled with values.
|
|
#4
|
|||
|
|||
|
I get a
Visual Bais error 9 Subscript out of range i dont know how to enter through the document while debugging or set a break point, can you tell me or direct me to a tutorial for that is there a way to enter the list manually into the macro ? |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Need macro to make list of "defined terms"
|
glnz | Word VBA | 8 | 09-07-2022 02:09 PM |
| How to extract names from a list | flyoverrunner | Excel | 3 | 06-06-2018 04:54 AM |
Best Practice for Indexing Multiple Word Terms and Sub-Terms
|
jhy001 | Word | 4 | 11-06-2017 02:08 PM |
How to Combine a Table of Contents Page with Another Document
|
Dante49 | Word | 2 | 10-24-2016 01:53 PM |
Need to extract two word domains from a list (BULK)
|
Maxwell314 | Excel | 3 | 12-08-2014 06:17 PM |