ListBox in a Word Document

D

Doctorjones_md

Please excuse this re-posting -- it's from a thread some weeks back which
may have run out of steam:

Doug,

You seemed rather emphatic about this approach -- I'm sure you derrive a
certain amount of pleasure when someone is successful in using your advice
(as would I) :)

When I run the code you suggested, I get a Run Time Error 5941 -- "The
requested member of the collection does not exist." -- I believe it has
something to do with this line in the code:

' Modify the path in the following line so that it matches where you _
Saved LineItems.doc
Application.ScreenUpdating = False

What am I doing wrong here? I modified the path to my sourcedoc in the next
line:

' Open the file containing the client details
Set sourcedoc = Documents.Open(FileName:="E:\LineItems.doc")


Here's my code for UserForm1 of DougListBoxTest.doc:
=========================
Private Sub CommandButton1_Click()
Dim i As Integer, Product As String
Product = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
Product = Product & ListBox1.Value & vbCr
Next i
ActiveDocument.Bookmarks("Product").Range.InsertAfter Product
UserForm1.Hide

End Sub

Private Sub UserForm_Initialize()
Dim sourcedoc As Document, i As Integer, j As Integer, myitem As Range, _
m As Long, n As Long
' Modify the path in the following line so that it matches where you _
Saved LineItems.doc
Application.ScreenUpdating = False
' Open the file containing the client details
Set sourcedoc = Documents.Open(FileName:="E:\LineItems.doc")
' Get the number of Products = number of rows in the table of Product _
details less one
i = sourcedoc.Tables(1).Rows.Count - 1
' Get the number of columns in the table of Product details
j = sourcedoc.Tables(1).Columns.Count
' Set the number of columns in the Listbox to match
' the number of columns in the table of Product details
ListBox1.ColumnCount = j
' Define an array to be loaded with the Product data
Dim MyArray() As Variant
'Load Product data into MyArray
ReDim MyArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Next m
Next n
' Load data into ListBox1
ListBox1.List() = MyArray
' Close the file containing the client details
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges

End Sub
 
J

Jean-Guy Marcil

Doctorjones_md was telling us:
Doctorjones_md nous racontait que :
Please excuse this re-posting -- it's from a thread some weeks back
which may have run out of steam:

Doug,

You seemed rather emphatic about this approach -- I'm sure you
derrive a certain amount of pleasure when someone is successful in
using your advice (as would I) :)

When I run the code you suggested, I get a Run Time Error 5941 -- "The
requested member of the collection does not exist." -- I believe it
has something to do with this line in the code:

' Modify the path in the following line so that it matches where
you _ Saved LineItems.doc
Application.ScreenUpdating = False


Why do you believe that?

Is that line highlighted when you hit "Debug" on the error message?
If it isn't this one, which it then?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top