Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-28-2018, 07:08 AM
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 Extract List of terms and combine on a Table

Hi everyone,

I would like to make a macros to extract text within parentheses and all countries names in order of apearence within 2 open word documents and combine them on a new document for revision.
I suppose I have to provide the list of countries

I have a macro to extract and put on a table text within parentheses but it only works for one document. I want to add extraction of all countries listed in the doc too. and that it processes both open documents. There is no need for autosave since its only for revision.

Here the macro for extracting text within parentheses.

Sub ExtractText()
Dim cDoc As Word.Document, nDoc As Word.Document
Dim cRng As Word.Range, nRng As Word.Range

Set cDoc = ActiveDocument
Set nDoc = Documents.Add

Set cRng = cDoc.Content
Set nRng = nDoc.Content

cRng.Find.ClearFormatting
With cRng.Find
.Forward = True
.Text = "("
.Wrap = wdFindStop
.Execute
Do While .Found


cRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
cRng.MoveEndUntil Cset:=")", Count:=Word.wdForward
nRng.FormattedText = cRng.FormattedText
nRng.InsertParagraphAfter
nRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
cRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
.Execute
Loop
End With
End Sub


thanks a lot
Reply With Quote
  #2  
Old 06-28-2018, 12:23 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

The sub that you pasted pretty much does what you want, right? It accounts for 2 documents, the first one that you search, and the second one the code creates, and that you paste to. If you want to search 2 existing documents, open them by their file locations rather than using ActiveDocument:

Code:
Sub open_docs()
    Dim doc_name1 As String
    Dim doc1 As Document
    doc_name1 = "c:\[location]\Doc1.docx"
    Set doc1 = Documents.Open(doc_name1)
    
    Dim doc_name2 As String
    Dim doc2 As Document
    doc_name1 = "c:\[location]\Doc2.docx"
    Set doc2 = Documents.Open(doc_name1)
End Sub
You can then access the range objects of both "doc1" and "doc2".

You can get a list of country names here, but I'm not sure why you would need it.
Reply With Quote
  #3  
Old 06-28-2018, 01:08 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

Hi d4okeefe,



thanks for replying. Yes i want 2 docs to be searched and results to be shown in a new doc with 2 columns for comparison.


The macro should be a "Search and Extract" for text in () and countries listed in both Doc 1 and Doc 2, thats why I need to integrate a list of countries into the macro but I dont know where and how.


Its important that entries sould be extracted and combined in order of appearance in both docs.


I hope it's clear.
Reply With Quote
  #4  
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
  #5  
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
  #6  
Old 06-28-2018, 02:22 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

Hmm. Do you know how to step through document while debugging?

If so, set the breakpoint at "doc2.Close", then check the two arrays (arr1 & arr2) to see if they have any values loaded. There could be a problem with the search.
Reply With Quote
  #7  
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, 45 views)
Reply With Quote
  #8  
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
  #9  
Old 06-28-2018, 03:07 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

Error 9 means that the arr1 or arr2 variable has a problem. Make sure that these lines:
Code:
Dim arr1(1 To 1000)
Dim arr2(1 To 1000)
... appear in your code. Or, possibly, there are more than 1000 countries in your search. If so, you could increase the size of the array to 10000.

Better, yet paste your code, and let's take a look. If you can, click the # sign above, so that your code is formatted.

Here's Microsoft's VBA tutorial.
Reply With Quote
  #10  
Old 06-28-2018, 03:25 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

You were right, more than 1000 countries thats why it didnt work.


tried with a smaller file, what I got was the text in () from both documents but not on 2 columns facing each other.


No countries though and I think its because I didnt enter them in arrays 1 and 2, how do I do that ?
Reply With Quote
  #11  
Old 06-28-2018, 04:01 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 managed to set a break point where you told me, the values in arr1 and arr2 are the text in () from both Docs but no country names.
Reply With Quote
  #12  
Old 06-29-2018, 11:11 AM
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

To get the text into two columns, you can do something like this. Notice that variables arr1 and arr2 already need to be filled with your data.

I hope that helps. Better yet, as I mentioned, it might be good to see your code. Since you aren't terribly used to working with VBA, it would be easy to have a variable misnamed or out of place.

Code:
    Dim tbl As Table
    Set tbl = new_doc_rng.Tables.Add(new_doc_rng, 2, 2)
    
    tbl.Cell(1, 1).Range.Text = "Countries from Doc1:"
    tbl.Cell(1, 2).Range.Text = "Countries from Doc2:"
    
    Dim countries1_as_string As String
    For x = 1 To UBound(arr1)
        If arr1(x) <> "" Then
            countries1_as_string = _
                countries1_as_string & arr1(x) & Chr(10)
        End If
    Next x
    tbl.Cell(2, 1).Range.Text = countries1_as_string
    
    Dim countries2_as_string As String
    For x = 1 To UBound(arr1)
        If arr1(x) <> "" Then
            countries2_as_string = _
                countries2_as_string & arr2(x) & Chr(10)
        End If
    Next x
    tbl.Cell(2, 2).Range.Text = countries2_as_string
Reply With Quote
  #13  
Old 06-29-2018, 11:17 AM
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

The code is basicaly the same as what you gave me.
Its working fine, I get the 2 column with text in () except I dont get the countries included


here the code


Code:
Sub GetCountriesFrom2Docs_AddToNewDoc()
    Dim doc_name1 As String: doc_name1 = "C:\Users\Desktop\test\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:\Users\Desktop\test\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:\Users\Sanja\Desktop\test\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
    
    Dim tbl As Table
    Set tbl = new_doc_rng.Tables.Add(new_doc_rng, 2, 2)
    
    tbl.Cell(1, 1).Range.Text = "Countries from Doc1:"
    tbl.Cell(1, 2).Range.Text = "Countries from Doc2:"
    
    Dim countries1_as_string As String
    For x = 1 To UBound(arr1)
        If arr1(x) <> "" Then
            countries1_as_string = _
                countries1_as_string & arr1(x) & Chr(10)
        End If
    Next x
    tbl.Cell(2, 1).Range.Text = countries1_as_string
    
    Dim countries2_as_string As String
    For x = 1 To UBound(arr1)
        If arr1(x) <> "" Then
            countries2_as_string = _
                countries2_as_string & arr2(x) & Chr(10)
        End If
    Next x
    tbl.Cell(2, 2).Range.Text = countries2_as_string

    new_doc.SaveAs2 new_doc_name
End Sub
Reply With Quote
  #14  
Old 06-29-2018, 11:38 AM
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

Have you ever seen the countries printed to the new document? Or, does the new document contain no data?
Reply With Quote
  #15  
Old 06-29-2018, 11:50 AM
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

Then new Doc contains only the text in () as expected in 2 columns


no country names, logivwly i dont expect them to appear untill i "tell" the macro to namely look for them.



I could add them for Doc1 and Doc2 as "Albania", "Algeria",...... but where ???


in rng1 and rng2 ?

.Text ="\(*\)"


or here
If arr1(x) <> "" Then
Reply With Quote
Reply



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 12:06 AM.


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