Results 1 to 7 of 7

Thread: listbox to word

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192
    Can anyone tell how to put the contents of a listbox from VB to Word and open the Word (document1 or a given name), and how to make the fontheigh 8 ?

  2. #2
    Addicted Member
    Join Date
    Jan 2000
    Location
    Fresno, California, USA
    Posts
    195
    Add a reference to your project for the Microsoft Word 8.0 Object Library, then add the following:

    Private Sub Command1_Click()

    Dim WordApp As Word.Application
    Dim nCount As Integer

    'Starting Word
    Set WordApp = New Word.Application
    WordApp.Application.Visible = False
    WordApp.Documents.Add
    WordApp.ActiveDocument.Words(1).Font.Size = 8
    For nCount = 0 To List1.ListCount - 1
    WordApp.ActiveDocument.Words(1).InsertAfter List1.List(nCount)
    Next
    WordApp.Application.Visible = True
    WordApp.ActiveDocument.PrintOut Background:=False
    WordApp.Documents.Close wdDoNotSaveChanges
    WordApp.Application.Quit
    Set WordApp = Nothing

    End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192
    Thanks a lot HDR, thats exact what I want !

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192
    I have made a try-out of the code from you HDR, but the lines put in the word document are written in reverse !
    Here is my example:

    Private Sub Command1_Click()
    Dim WordApp As Word.Application
    Dim nCount As Integer: Dim x As Integer

    'Starting Word
    Set WordApp = New Word.Application
    WordApp.Application.Visible = True
    WordApp.Documents.Add
    WordApp.ActiveDocument.Words(1).Font.Size = 10
    WordApp.ActiveDocument.Words(1).InsertAfter "1-Name" & vbCrLf
    WordApp.ActiveDocument.Words(1).InsertAfter "2-Adress" & vbCrLf
    WordApp.ActiveDocument.Words(1).InsertAfter "3-City" & vbCrLf
    For x = 1 To 5
    WordApp.ActiveDocument.Words(1).InsertAfter " " & vbCrLf 'empty line
    Next x
    WordApp.ActiveDocument.Words(1).Font.Size = 8
    For nCount = 0 To List1.ListCount - 1
    WordApp.ActiveDocument.Words(1).InsertAfter List1.List(nCount) & vbCrLf
    Next
    WordApp.Application.Visible = True

    Set WordApp = Nothing
    End Sub

    Private Sub Form_Load()
    'fill the listbox
    For x = 0 To 5
    List1.AddItem "This is line " & x
    Next x
    End Sub

    Can anyone try this out and tell me what to do to get it right ?

  5. #5
    Addicted Member
    Join Date
    Jan 2000
    Location
    Fresno, California, USA
    Posts
    195
    What you are doing is more complicated. You need to use a Range object to keep things in order. This is not the ASCII text world anymore. Object must be utilized to make this stuff work.


    Private Sub Command1_Click()
    Dim WordApp As Word.Application
    Dim WordRange As Word.Range
    Dim nCount As Integer: Dim x As Integer

    'Starting Word
    Set WordApp = New Word.Application
    WordApp.Application.Visible = True
    WordApp.Documents.Add
    Set WordRange = WordApp.ActiveDocument.Range
    WordRange.Font.Size = 10
    WordRange.InsertAfter "1-Name" & vbCrLf
    WordRange.InsertAfter "2-Address" & vbCrLf
    WordRange.InsertAfter "3-City" & vbCrLf
    For x = 1 To 5
    WordRange.InsertAfter " " & vbCrLf 'empty line
    Next x
    WordRange.Collapse (wdCollapseEnd)
    WordRange.Font.Size = 8
    For nCount = 0 To List1.ListCount - 1
    WordRange.InsertAfter List1.List(nCount) & vbCrLf
    Next

    WordApp.Documents.Close (wdDoNotSaveChanges)
    WordApp.Quit
    Set WordApp = Nothing
    End Sub

    Private Sub Form_Load()
    'fill the listbox
    Dim x As Integer
    For x = 0 To 5
    List1.AddItem "This is line " & x
    Next x
    End Sub


  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 1999
    Location
    Ruinen, Drente, Netherlands
    Posts
    192

    listbox to word t

    HDR, thanks very much, this is what I like and it works well, except one thing:
    - de fontsize keeps the first size (10), is that to change ?

    regards, K. Lensen

  7. #7
    Addicted Member
    Join Date
    Jan 2000
    Location
    Fresno, California, USA
    Posts
    195
    Isn't it kind of late where you are? Like middle of the night late.

    Try this:


    Private Sub Command1_Click()
    Dim WordApp As Word.Application
    Dim WordRange As Word.Range
    Dim nCount As Integer: Dim x As Integer
    Dim nlRangeStart As Long
    Dim nlRangeEnd As Long

    'Starting Word
    Set WordApp = New Word.Application
    WordApp.Application.Visible = True
    WordApp.Documents.Add
    Set WordRange = WordApp.ActiveDocument.Range
    WordRange.Font.Size = 10
    WordRange.InsertAfter "1-Name" & vbCrLf
    WordRange.InsertAfter "2-Address" & vbCrLf
    WordRange.InsertAfter "3-City" & vbCrLf
    For x = 1 To 5
    WordRange.InsertAfter " " & vbCrLf 'empty line
    Next x
    nlRangeStart = WordRange.End
    For nCount = 0 To List1.ListCount - 1
    WordRange.InsertAfter List1.List(nCount) & vbCrLf
    Next
    nlRangeEnd = WordRange.End
    WordRange.SetRange nlRangeStart, nlRangeEnd
    WordRange.Font.Size = 8

    WordApp.Documents.Close (wdDoNotSaveChanges)
    WordApp.Quit
    Set WordApp = Nothing
    End Sub

    Private Sub Form_Load()
    'fill the listbox
    Dim x As Integer
    For x = 0 To 5
    List1.AddItem "This is line " & x
    Next x
    End Sub


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