Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-23-2017, 04:02 AM
gmayor's Avatar
gmayor gmayor is offline Do Loop bug Windows 10 Do Loop bug Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

OK I get the first part. In your document it collects:


Community Health Systems, Inc. (CYHHZ)

That part is easy enough. What I don't understand is the second part; and your revised explanation doesn't make any sense, when related to the document you have provided as an example.

If you grab the line that contains TOTAL REVENUE and then grab the complete paragraph, then if the document is any indication, you are not actually collecting anything. Are you saying that the document is not representative and there is more data on the same line as TOTAL REVENUE missing from your example. That would make sense. In which case the following will work:

Code:
Option Explicit

Sub Macro1()
'Graham Mayor - http://www.gmayor.com - Last updated - 23 Jul 2017
Dim oDoc As Document
Dim oNewDoc As Document
Dim oRng As Range, oRng2 As Range
Dim vFind As Variant
Dim fso As Object
Dim strPath As String
Const strFind As String = "Add to watchlist|TOTAL REVENUE"

    strPath = Environ("USERPROFILE") & "\Desktop\DataExtract.docx"     'The name of the document to save the extract
    Set fso = CreateObject("Scripting.FileSystemObject")
    vFind = Split(strFind, "|")
    Set oDoc = ActiveDocument
    If fso.FileExists(strPath) Then
        Set oNewDoc = Documents.Open(FileName:=strPath, AddToRecentFiles:=False)
    Else
        Set oNewDoc = Documents.Add
        oNewDoc.SaveAs FileName:=strPath, FileFormat:=12, AddToRecentFiles:=False
    End If
    Set oRng = oDoc.Range
    With oRng.Find
        Do While .Execute(FindText:=vFind(0))
            oRng.MoveStart wdParagraph, -2
            oNewDoc.Range.InsertAfter _
                    Left(oRng.Paragraphs(1).Range.Text, _
                         Len(oRng.Paragraphs(1).Range.Text) - 1)
            oRng.Collapse 0
        Loop
    End With
    Set oRng = oDoc.Range
    With oRng.Find
        Do While .Execute(FindText:=vFind(1))
            oRng.End = oRng.Paragraphs(1).Range.End - 1
            Set oRng2 = oNewDoc.Range
            oRng2.End = oRng2.End - 1
            oRng2.Collapse 0
            oRng2.Text = vbTab & Trim(Replace(LCase(oRng.Text), _
                                              LCase(vFind(1)), "")) & vbCr
            oRng.Collapse 0
        Loop
    End With
    oNewDoc.Close wdSaveChanges
lbl_Exit:
    Set fso = Nothing
    Set oDoc = Nothing
    Set oNewDoc = Nothing
    Set oRng = Nothing
    Set oRng2 = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #2  
Old 07-23-2017, 10:22 AM
donlincolnmsof donlincolnmsof is offline Do Loop bug Windows 7 64bit Do Loop bug Office 2003
Advanced Beginner
Do Loop bug
 
Join Date: Oct 2011
Posts: 36
donlincolnmsof is on a distinguished road
Default

its crashing at

oNewDoc.SaveAs FileName:=strPath, FileFormat:=12, AddToRecentFiles:=False

the other thing is how i suppose to run this macro

should i have both windows open 1) with the master data and the 2nd window blank so i t can extract from 1 and paste in the 2nd one.

btw i'm using word 2000 so DOCX i believe is for new version of files.

It would have been much easier if the DO LOOP code was just fixed in the file that i attached.

The macro that you have written is very sophisticated and advance for my knowledge.

Thanks a lot though for your help.
Reply With Quote
  #3  
Old 07-23-2017, 10:38 AM
donlincolnmsof donlincolnmsof is offline Do Loop bug Windows 7 64bit Do Loop bug Office 2003
Advanced Beginner
Do Loop bug
 
Join Date: Oct 2011
Posts: 36
donlincolnmsof is on a distinguished road
Default

Here is the output that i'm looking for

attached are both 1) macro file 2) the data file.

Thanks


Mexco Energy Corporation (MXC) Total Revenue 2,525.363 2,421.792 3,390.005
the Rubicon Project, Inc. (RUBI) Total Revenue 278,221 248,484 125,295
Liberty All-Star Growth Fund, Inc. (ASG) Total Revenue 985.629 28,621.59 13,942.108
Partner Communications Company Ltd. (PTNR) Total Revenue 921,000 1,057,000 1,131,000
SITO Mobile, Ltd. (SITO) Total Revenue 29,426.955 12,805.19 15,809.467
Echelon Corporation (ELON) Total Revenue 32,385 38,804 38,730
TerraForm Global, Inc. (GLBL) Total Revenue 214,317 124,116 39,449
Juniper Pharmaceuticals, Inc. (JNP) Total Revenue 54,573 38,287 33,393
Vivint Solar, Inc. (VSLR) Total Revenue 135,167 64,182 25,258
Attached Files
File Type: doc macro with data.doc (105.0 KB, 12 views)
File Type: doc output file.doc (20.0 KB, 10 views)
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Do Loop bug Macro Loop Help Twizzle008 Word VBA 15 09-18-2015 03:20 PM
Do Loop bug Loop - Row increment jrt Excel Programming 1 04-16-2015 01:46 PM
What's wrong with my loop? Irrma Word VBA 2 06-17-2014 06:25 AM
Do Loop bug How to a For loop in VBA Jennifer Murphy Word VBA 1 01-29-2013 03:30 AM
How to use for loop in formula in VBA? tinfanide Excel Programming 1 12-06-2011 08:33 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:57 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