Results 1 to 5 of 5
  1. #1
    JustCallMeAl
    Guest

    Stumped with extra paragraphs

    Taking Charlotte's advice to heart, I am using a text file to load an array. The text file is a text version of a word document.

    I would like to be able to convert the word document to the text file via VBA. I have been successful up to this last point.

    The text file, when saved, closed, and reopened, contains several blank paragraphs at the end of the document. This causes the loading of information from the text file into the Arrays to crash. The solution, I have found, is to get rid of all those extra paragraphs.

    So, I would like to delete them. My initial thought was to test what the last character is, if it is a paragraph mark, delete it. But that didn't work.

    Could someone point me in a direction to head? I am not particularly fond of using Find/Replace; though it could be done with F/R.

    Thanks.

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Stumped with extra paragraphs

    It depends on how you are reading the file- in one big piece or bit by bit.

    Here's the way I'd try it:
    <pre>Dim iFileNo As Integer
    Dim lngCount As Long
    Dim strLine As String
    Dim strArray() As String

    iFileNo = FreeFile
    Open "c:temp.txt" For Input As #iFileNo

    lngCount = 0
    Do Until EOF(iFileNo)
    Line Input #iFileNo, strLine
    If Trim$(strLine) <> "" Then
    ReDim Preserve strArray(lngCount)
    strArray(lngCount) = strLine
    lngCount = lngCount + 1
    End If
    Loop
    Close #iFileNo
    </pre>

    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    JustCallMeAl
    Guest

    Re: Stumped with extra paragraphs

    Thanks.

    I just remembered that I had an Empty Paragraph procedure:

    <pre>Dim aPara As Paragraph
    For Each aPara In ActiveDocument.Paragraphs
    If Asc(aPara.Range.Characters.FIRST) = 13 Then
    aPara.Range.Delete
    End If
    Next aPara
    </pre>


    Thanks anyway.

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Stumped with extra paragraphs

    Hi Al,

    If you're going to do it that way, there are probably lots of ways to get at empty paragraphs; here's another one:

    If Len(aPara.Range) = 1 Then

    But keep in mind that deleting members of a collection can lead to unexpected results, unless you step through the collection from end to start - kind of shame since looping a counter is so much slower than a For Each, but anyway......

    Gary

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Stumped with extra paragraphs

    The best way to do this is to navigate thru the document 1 paragraph at a time using the Expand method via the Range object

    For each paragraph, check whether it is at end of document. If it is, just write the first n-1 bytes to the text file. If at the end of the document, write all n bytes.

    But also check for the length of the stuff. Most documents do contain valid empty paragraphs.

    Note: Check whether Expand includes the final paragraph mark, I'm too groggy to recall right now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •