Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pull line from word file

Status
Not open for further replies.

proteome

Technical User
May 14, 2003
115
US
I need to pull a lines froma word file. The lines all begin with >. I would like to copy all lines that begin with > into another document.

Thanks..
 
Hi proteome,

Lines are awkward in Word - there isn't a line object. Do you really mean line, or will paragraph do? If so, try this.

Code:
Dim p As Paragraph

For Each p In ActiveDocument.Paragraphs

    If Left(p.Range.Text, 1) = ">" Then
        p.Range.Copy
Code:
' and paste it where you want it
Code:
    End If

Next

If it must be lines, post back and I will try and knock something up.

Enjoy,
Tony
 
Here is an example of the data.

>143B_HUMAN (P31946) 14-3-3 protein beta/alpha (Protein kinase C inhibitor protein-1) (KCIP-1) (Protein 1054)
TMDKSELVQKAKLAEQAERYDDMAAAMKAVTEQGHELSNEERNLLSVAYKNVVGARRSSW
RVISSIEQKTERNEKKQQMGKEYREKIEAELQDICNDVLELLDKYLIPNATQPESKVFYL
KMKGDYFRYLSEVASGDNKQTTVSNSQQAYQEAFEISKKEMQPTHPIRLGLALNFSVFYY
EILNSPEKACSLAKTAFDEAIAELDTLNEESYKDSTLIMQLLRDNLTLWTSENQGDEGDA
GEGEN
>143E_HUMAN (P42655) 14-3-3 protein epsilon (Mitochondrial import stimulation factor L subunit) (Protein kinase C inhibitor protein-1) (KCIP-1) (14-3-3E)
MDDREDLVYQAKLAEQAERYDEMVESMKKVAGMDVELTVEERNLLSVAYKNVIGARRASW
RIISSIEQKEENKGGEDKLKMIREYRQMVETELKLICCDILDVLDKHLIPAANTGESKVF
YYKMKGDYHRYLAEFATGNDRKEAAENSLVAYKAASDIAMTELPPTHPIRLGLALNFSVF
YYEILNSPDRACRLAKAAFDDAIAELDTLSEESYKDSTLIMQLLRDNLTLWTSDMQGDGE
EQNKEALQDVEDENQ
>143F_HUMAN (Q04917) 14-3-3 protein eta (Protein AS1)
GDREQLLQRARLAEQAERYDDMASAMKAVTELNEPLSNEDRNLLSVAYKNVVGARRSSWR
VISSIEQKTMADGNEKKLEKVKAYREKIEKELETVCNDVLSLLDKFLIKNCNDFQYESKV
FYLKMKGDYYRYLAEVASGEKKNSVVEASEAAYKEAFEISKEQMQPTHPIRLGLALNFSV
FYYEIQNAPEQACLLAKQAFDDAIAELDTLNEDSYKDSTLIMQLLRDNLTLWTSDQQDEE
AGEGN


All I need is the line that contains the >

Thanks
 
Hi proteome,

I was going to say I liked your name, now I see why you chose it.

Neither 'lines' nor 'paragraphs' then. It won't be tonight now, but I will come back with something for you.

Enjoy,
Tony
 
Hi proteome,

Sorry I didn’t have time to do this properly on Friday night, but here it is.

My guess is that your data is created outside Word. Your lines are ended by manual line breaks (ASCII character code 11) which, although you can type them in Word with <Shift><Enter> (and search for them using “^l”), are unusual and have no significance in the Word Object Model. They are described by Help as ‘not useful in Microsoft Windows or on the Macintosh’ and can be referred to in VBA as vertical tabs.

The only option I know, in VBA code, is to process the whole text of the document as one long string, picking off each line-break-delimited (sub-)string in turn. The code below checks for paragraph marks as well, not least because it is necessary to correctly process the end of the document, although your sample only had one, right at the end of the document (and I don’t think you can have a document without any at all).

Code:
 Sub Proteome()
    
    Dim docSource As Document
    Dim docTarget As Document
    Dim rngSource As Range
    Dim rngTarget As Range
    
    Set docSource = ActiveDocument
    Set docTarget = Documents.Add
    Set rngSource = docSource.Range(0, 0)
    Set rngTarget = docTarget.Range
    
    Do While rngSource.End < docSource.Range.End - 1
    
        If rngSource.MoveEndUntil(vbVerticalTab) = 0 Then
            rngSource.MoveEndUntil vbNewLine
        End If
        rngSource.MoveEnd wdCharacter
    
        If rngSource.Paragraphs.Count > 1 Then
            rngSource.Collapse wdCollapseStart
            rngSource.MoveEndUntil vbNewLine
            rngSource.MoveEnd wdCharacter, 2
        End If
    
        If Left(rngSource.Text, 1) = &quot;>&quot; Then
            rngSource.Copy
            rngTarget.Paste
            rngTarget.Collapse wdCollapseEnd
        End If
        
        rngSource.Collapse wdCollapseEnd
        
    Loop

End Sub

Enjoy,
Tony

 
Thanks Tony.

The code works well. But I believe that my file is just too big, the computer locks up. I may need to look at another way of doing this.

[thumbsup2]
 
Hi proteome,

Just a thought. Don't know if it'll help but it might use less resources if you worked on a copy of your document and deleted the bits you don't want rather than trying to build a new document with the bits you do.

Enjoy,
Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top