Saving as TXT file adds blank lines/paragraphs

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Saving as TXT file adds blank lines/paragraphs

Post by Robie »

Hi

I need to save some information as text after running some VBA. It could have any number of lines.

But, it seems to add blank lines at the end of the file. I am saving the document as text with LF Only as follows:

Code: Select all

        TDoc.SaveAs2 FileName:=sLocalFileName, FileFormat:=wdFormatText, LineEnding:=wdLFOnly
Now, I did a test by opening a document using Normal.dotm and entering just one paragraph and saving it as above. This is what is saves:
44.png
Unfortunately, the output text file is processed by UNIX and it doesn't like extra line feeds. Any ideas?

Thanks.
Robie
You do not have the required permissions to view the files attached to this post.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Saving as TXT file adds blank lines/paragraphs

Post by Rudi »

Hi Robie,

Word VBA is not my strong point and as a matter of fact I don't really understand your code. I just get an idea that you don't want empty paragraphs at the end of your text file.

Can you use the following code to help resolve your issue.
I strung it together with a few Google results and tweaked it to remove paragraphs at the end of the last text of the document.

Code: Select all

Sub CleanEnd()
Dim i As Integer
Dim range As range
    For i = ActiveDocument.Paragraphs.Count To i Step -1
        Set range = ActiveDocument.Paragraphs(i).range
        If range.Characters.Count = 1 Then
            range.Delete
        Else
            Exit For
        End If
    Next i
End Sub
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Saving as TXT file adds blank lines/paragraphs

Post by Robie »

Rudi wrote:Hi Robie,

Word VBA is not my strong point and as a matter of fact I don't really understand your code. I just get an idea that you don't want empty paragraphs at the end of your text file.

Can you use the following code to help resolve your issue.
I strung it together with a few Google results and tweaked it to remove paragraphs at the end of the last text of the document.

Code: Select all

Sub CleanEnd()
Dim i As Integer
Dim range As range
    For i = ActiveDocument.Paragraphs.Count To i Step -1
        Set range = ActiveDocument.Paragraphs(i).range
        If range.Characters.Count = 1 Then
            range.Delete
        Else
            Exit For
        End If
    Next i
End Sub
Thanks Rudi. Even creating a new document, adding a paragraph and saving it as text file from Word (no VBA) *adds* blank lines at the end :sad:

It means having to re-open the .TXT file, removing the blank paragraphs and re-saving it. When I saved the existing document as .TXT file, it *doesn't* contain any extra blank paragraphs. It's the process of saving a Word document as text file that adds the blank paragraphs. That is where the problem lies.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Saving as TXT file adds blank lines/paragraphs

Post by Rudi »

Oh gosh...sorry I was looking at the solution from the wrong end... :stupidme:
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Guessed
2StarLounger
Posts: 102
Joined: 04 Feb 2010, 22:44
Location: Melbourne Australia

Re: Saving as TXT file adds blank lines/paragraphs

Post by Guessed »

It appears that what is happening is the final paragraph mark (which contains the header and footer information) is getting exported with all those contents as well. A workaround would be to save everything except the final paragraph mark

Code: Select all

  Dim rng As Range
  Set rng = ActiveDocument.StoryRanges(wdMainTextStory)
  rng.MoveEnd Unit:=wdCharacter, Count:=-1
  rng.ExportFragment FileName:="C:\Test.txt", Format:=wdFormatText
Andrew Lockton
Melbourne Australia

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Saving as TXT file adds blank lines/paragraphs

Post by Rudi »

Guessed wrote:It appears that what is happening is the final paragraph mark (which contains the header and footer information) is getting exported with all those contents as well.
Very interesting. I didn't know that about the last paragraph mark.
TX for that info Andrew :cheers:
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Saving as TXT file adds blank lines/paragraphs

Post by Robie »

Guessed wrote:It appears that what is happening is the final paragraph mark (which contains the header and footer information) is getting exported with all those contents as well. A workaround would be to save everything except the final paragraph mark

Code: Select all

  Dim rng As Range
  Set rng = ActiveDocument.StoryRanges(wdMainTextStory)
  rng.MoveEnd Unit:=wdCharacter, Count:=-1
  rng.ExportFragment FileName:="C:\Test.txt", Format:=wdFormatText
Thanks. The only question is that for this test, I opened a new Normal.dotm document, added a paragraph and saved it as text file - there are no headers and footers.

But, I will try that out.

User avatar
Guessed
2StarLounger
Posts: 102
Joined: 04 Feb 2010, 22:44
Location: Melbourne Australia

Re: Saving as TXT file adds blank lines/paragraphs

Post by Guessed »

Every section contains 3 headers (first page, odd, even) and 3 footers. Even when they are empty each one contains a paragraph mark. In the sample you posted there were an additional 6 paragraphs - coincidence??

If you put some content into one of the headers and then try your original line of code you will see that the text in the header has crept into your text output.
Andrew Lockton
Melbourne Australia

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Saving as TXT file adds blank lines/paragraphs

Post by Robie »

Guessed wrote:Every section contains 3 headers (first page, odd, even) and 3 footers. Even when they are empty each one contains a paragraph mark. In the sample you posted there were an additional 6 paragraphs - coincidence??

If you put some content into one of the headers and then try your original line of code you will see that the text in the header has crept into your text output.
Doh. Okay, now it makes sense.

Thanks.