Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-28-2018, 01:39 PM
d4okeefe d4okeefe is offline Extract List of terms and combine on a Table Windows 10 Extract List of terms and combine on a Table Office 2016
Advanced Beginner
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

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
Reply With Quote
  #2  
Old 06-28-2018, 02:12 PM
XRope XRope is offline Extract List of terms and combine on a Table Windows 7 64bit Extract List of terms and combine on a Table Office 2010 32bit
Novice
Extract List of terms and combine on a Table
 
Join Date: Jun 2018
Posts: 17
XRope is on a distinguished road
Default

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
Reply With Quote
  #3  
Old 06-28-2018, 02:26 PM
d4okeefe d4okeefe is offline Extract List of terms and combine on a Table Windows 10 Extract List of terms and combine on a Table Office 2016
Advanced Beginner
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

Something like this. In the Locals window, you can see that arr1 is filled with values.
Attached Images
File Type: png Screenshot (40).png (50.2 KB, 47 views)
Reply With Quote
  #4  
Old 06-28-2018, 02:58 PM
XRope XRope is offline Extract List of terms and combine on a Table Windows 7 64bit Extract List of terms and combine on a Table Office 2010 32bit
Novice
Extract List of terms and combine on a Table
 
Join Date: Jun 2018
Posts: 17
XRope is on a distinguished road
Default

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 ?
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract List of terms and combine on a Table 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
Extract List of terms and combine on a Table Best Practice for Indexing Multiple Word Terms and Sub-Terms jhy001 Word 4 11-06-2017 02:08 PM
Extract List of terms and combine on a Table How to Combine a Table of Contents Page with Another Document Dante49 Word 2 10-24-2016 01:53 PM
Extract List of terms and combine on a Table Need to extract two word domains from a list (BULK) Maxwell314 Excel 3 12-08-2014 06:17 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:33 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft