Results 1 to 12 of 12
  1. #1
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Word to Text File (2003)

    I am using the following in macro to save documents as TXT files and then close them:

    ActiveDocument.SaveAs FileName:="C:AscendPrint" + strFilename + strDOCExt, FileFormat:=wdFormatDOSText
    ActiveDocument.Close

    However, when the documents are opened again in Word or Notepad, there are 12 extra lines at the end of the document and when they are processed by our interface are rejected because there appears to be some hidden coding. Any ideas? I have attached a sample TXT file.
    Attached Files Attached Files

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word to Text File (2003)

    Were all those | and ~ characters in the document before it was saved as a text file?

  3. #3
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Word to Text File (2003)

    Yes, you are looking at an HL7 message, new lines are segments, | field separators and ~ repeat characters for the fields...they are supposed to be there. It is the blank lines at the end of the report that are causing me the grief and at this point it is a LOT of grief.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word to Text File (2003)

    I would check carefully that those lines haven't crept into the Word document before saving as a text file - Word doesn't add them spontaneously.

  5. #5
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Word to Text File (2003)

    No, these lines are not being added. There is nothing at the end of the file and these are appearing on every document. The same thing happend in Office XP last fall when I tried this and we gave up and used another Word processor which did not have this problem. However, we are standardizing on Microsoft Word so we have to solve it. I have attached a copy of the Word file this document was created from. As you can see, there is just 1 blank line at the end.
    Attached Files Attached Files

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Word to Text File (2003)

    I believe that when you Save As to plain text, the headers and footers are tacked on at the end. Those could be the extra lines/codes you're seeing, although if you started from plain text, it's difficult to understand where those came from. Do you have any automation running (perhaps associated with a document management system) that touches your headers and footers?

    Plan B, of course, is to write the document contents out to a text file using one of the other methods at your disposal. These include Visual Basic's built in Print/Write/Put commands, and the more modern FileSystemObject's TextStream writing methods.

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Word to Text File (2003)

    I get strange ?? characters when I save that document as text. Hmmm. Well, here's another way to save. If the file already exists, it throws up an error, but presumably you already have a way to avoiding file name conflicts.
    <UL>Sub Test_SaveAsText()
    SaveAsText "c:testingtestfile.txt"
    End Sub

    Sub SaveAsText(strFileName As String)
    ' The following requires a reference to the Microsoft Scripting Runtime
    'Dim fso As Scripting.FileSystemObject, txt As Scripting.TextStream
    'Set fso = New FileSystemObject
    ' This "late binding" method does not require a reference
    Dim fso As Object, txt As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    ' Create new text file using name passed in, NO overwrite, NO unicode
    Set txt = fso.CreateTextFile(strFileName, False, False)
    txt.Write Replace(ActiveDocument.Content.Text, vbCr, vbCrLf)
    txt.Close
    If Not (txt Is Nothing) Then Set txt = Nothing
    If Not (fso Is Nothing) Then Set fso = Nothing
    End Sub[/list]Hope this helps.

  8. #8
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Word to Text File (2003)

    <P ID="edit" class=small>(Edited by jscher2000 on 29-Apr-05 13:44. Post too long... code moved to attachment.)</P>That may be it, I'll check the code myself, but the entire master template code is listed below. Don't laugh at my coding...I'm sure there are lots of things I could have done better. I would be happy for more information on the FileSystemOjbect TextStream writing method as that would likely solve the problem the quicket. Any examples?
    Attached Files Attached Files

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word to Text File (2003)

    An alternative using only Word VBA:

    Dim rng As Range, docOld As Document, docNew As Document
    Set docOld = ActiveDocument
    Set rng = docOld.Content
    rng.MoveEnd Unit:=wdCharacter, Count:=-1
    rng.Copy
    Set docNew = Documents.Add
    docNew.Content.Paste
    docNew.SaveAs FileName:="C:AscendPrint" & strFilename & strDOCExt, FileFormat:=wdFormatDOSText
    docNew.Close SaveChanges:=wdDoNotSaveChanges
    docOld.Close SaveChanges:=wdDoNotSaveChanges
    Set docNew = Nothing
    Set docOld = Nothing

    You may want to cut off more characters from the end by changing Count:=-1 to Count:=-2 or Count:=-3.

  10. #10
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Word to Text File (2003)

    Hans, I tested your code first since I am more comfortable with VBA and it worked. Jefferson and Hans, you both are fabulous mentors and your help is very much appreciated.

  11. #11
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Word to Text File (2003)

    OK, see if I am crazy or not, but I just tried running my original code while the document was in Normal View and it did not appear to insert the extra lines. Then I turned the view back to Print view and the extra lines were there. I will test further, but does that make sense?

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word to Text File (2003)

    I'm using Word 2002; it doesn't matter whether I am in Normal view or in Print Layour view when I save the document you attached as a text file; the extra lines are added in both cases...

Posting Permissions

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