Results 1 to 6 of 6

Thread: Create Word Document from Text File

  1. #1

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Create Word Document from Text File

    Does anyone have an example of how to read in a text file and export it to a Word document (from a VB program) ?

    Thanks for any help....
    VB6 Enterprise SP4

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Create Word Document from Text File

    Do you want the example in VB6 or Visual Basic for Applications (VBA)?

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Create Word Document from Text File

    Here is an example in VB6 (wrong Forum of course - but in accordance with your request )
    VB Code:
    1. Option Explicit
    2.  
    3. 'Reference Word Object Library 9.X
    4.  
    5. Private Sub form_load()
    6. Dim objWord As Word.Application
    7. Dim objDoc As Word.Document
    8.  
    9. On Error GoTo Err_Handler
    10.  
    11.     Set objWord = New Word.Application
    12.  
    13.     Set objDoc = objWord.Documents.Open("C:\test1.txt")
    14.     objWord.Visible = True
    15.  
    16.     Set objDoc = Nothing
    17.     Set objWord = Nothing
    18.  
    19. Exit Sub
    20.  
    21. Err_Handler:
    22.  
    23.     If Not (objDoc Is Nothing) Then Set objDoc = Nothing
    24.     If Not (objWord Is Nothing) Then Set objWord = Nothing
    25.    
    26.     MsgBox "Number: " & Err.Number & vbCrLf & _
    27.     "Description: " & Err.Description, vbOKOnly + vbCritical, "Error!"
    28.  
    29. End Sub

  4. #4

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Create Word Document from Text File

    Beautiful ! Thank you !

    I just need one more thing.... a command to save the file as a name that I would like to specify.

    Thanks!
    VB6 Enterprise SP4

  5. #5

    Thread Starter
    Addicted Member tcurrier's Avatar
    Join Date
    May 1999
    Location
    Northeastern Pa./USA
    Posts
    255

    Re: Create Word Document from Text File

    I found it:


    objDoc.SaveAs ("mynewfilename.doc")
    VB6 Enterprise SP4

  6. #6
    Lively Member fifo's Avatar
    Join Date
    Dec 2005
    Location
    HaiPhong, Vietnam
    Posts
    77

    Question Re: Create Word Document from Text File

    Quote Originally Posted by Bruce Fox
    Here is an example in VB6 (wrong Forum of course - but in accordance with your request )
    VB Code:
    1. Option Explicit
    2.  
    3. 'Reference Word Object Library 9.X
    4.  
    5. Private Sub form_load()
    6. Dim objWord As Word.Application
    7. Dim objDoc As Word.Document
    8.  
    9. On Error GoTo Err_Handler
    10.  
    11.     Set objWord = New Word.Application
    12.  
    13.     Set objDoc = objWord.Documents.Open("C:\test1.txt")
    14.     objWord.Visible = True
    15.  
    16.     Set objDoc = Nothing
    17.     Set objWord = Nothing
    18.  
    19. Exit Sub
    20.  
    21. Err_Handler:
    22.  
    23.     If Not (objDoc Is Nothing) Then Set objDoc = Nothing
    24.     If Not (objWord Is Nothing) Then Set objWord = Nothing
    25.    
    26.     MsgBox "Number: " & Err.Number & vbCrLf & _
    27.     "Description: " & Err.Description, vbOKOnly + vbCritical, "Error!"
    28.  
    29. End Sub
    But I want to add some object of MSWord such as : table, header & footer, line,... into this DOC file. How can I do? Can U help me? thanks
    !Have fun!

Posting Permissions

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



Click Here to Expand Forum to Full Width