|
-
Jun 14th, 2000, 09:49 PM
#1
Thread Starter
Addicted Member
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 ?
-
Jun 14th, 2000, 11:52 PM
#2
Addicted Member
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
-
Jun 15th, 2000, 12:04 AM
#3
Thread Starter
Addicted Member
Thanks a lot HDR, thats exact what I want !
-
Jun 15th, 2000, 11:14 AM
#4
Thread Starter
Addicted Member
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 ?
-
Jun 19th, 2000, 12:23 AM
#5
Addicted Member
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
-
Jun 19th, 2000, 03:11 AM
#6
Thread Starter
Addicted Member
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
-
Jun 19th, 2000, 03:56 AM
#7
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|