Tables In Word

D

Doctorjones_md

I've asked this question before -- I've learned quite a bit from some of the
help I've gotten, so I'll try to condense my original version/post:

Does anyone have some sample code I could use to do the following
==============================================
I have a series of forms with a variety of checkboxes and radio buttons.
For Example -- When checkboxes and radio buttons on frmMonAnaly are checked,
I need for data that spans (4) colums in a Word Table (a separate
document -- C:\Temp\Shane.doc) to populate in a table in the active document
(SOF.dot).

I created bookmarks for each row in the table (SOF.dot), but I'm not sure if
I need to duplicate these bookmarks in the Shane.doc Table.

I'm not sure which would be easiest (not that either is particularily easy
for me) -- to import the data from a Word Table, or from an Excel wb -- in
the SOF.doc, I'll untimately need to sum/calculate the totals in (3) of the
columns.

Thank -- I hope I've been concise here, and I appreciate whatever guidance
you can offer. Sample Code would help greatly :)

Shane
 
J

Jonathan West

Doctorjones_md said:
I've asked this question before -- I've learned quite a bit from some of
the help I've gotten, so I'll try to condense my original version/post:

Does anyone have some sample code I could use to do the following
==============================================
I have a series of forms with a variety of checkboxes and radio buttons.
For Example -- When checkboxes and radio buttons on frmMonAnaly are
checked, I need for data that spans (4) colums in a Word Table (a separate
document -- C:\Temp\Shane.doc) to populate in a table in the active
document (SOF.dot).

I created bookmarks for each row in the table (SOF.dot), but I'm not sure
if I need to duplicate these bookmarks in the Shane.doc Table.

I don't think you necessarily need to use bookmarks at all. If you know
which cell of the table in Shane.doc (i.e. the row & column number) you want
to take text from, you can retrieve it using the Cell(row,
column).Range.Text property of the table.

Similarly, if you know which cell you want to put the text into in SOF.dot,
you can assign a string to the Text property of the relevant Cell there.
 
D

Doug Robbins - Word MVP

I'm going to send you off in a different direction by suggesting that you
use a multi column listbox on the userform that you populate with the data
from Shane.doc and then, as Jonathon suggests, you grab the information from
each column in the listbox for the selected record and use it to populate
the respective cells in the document created from SOF.dot.

The following information will give you an idea how to go about this.

This routine loads a listbox with client details stored in a table in a
separate
document (which makes it easy to maintain with additions, deletions etc.),
that document being saved as Clients.Doc for the following code.

On the UserForm, have a list box (ListBox1) and a Command Button
(CommandButton1) and use the following code in the UserForm_Initialize() and
the CommandButton1_Click() routines

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 Clients.doc
Application.ScreenUpdating = False
' Open the file containing the client details
Set sourcedoc = Documents.Open(FileName:="e:\worddocs\Clients.doc")
' Get the number or clients = number of rows in the table of client
details less one
i = sourcedoc.Tables(1).Rows.Count - 1
' Get the number of columns in the table of client 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 client details
ListBox1.ColumnCount = j
' Define an array to be loaded with the client data
Dim MyArray() As Variant
'Load client 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

Private Sub CommandButton1_Click()
Dim i As Integer, Addressee As String
Addressee = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
Addressee = Addressee & ListBox1.Value & vbCr
Next i
ActiveDocument.Bookmarks("Addressee").Range.InsertAfter Addressee
UserForm2.Hide
End Sub

The Initialize statement will populate the listbox with the data from the
table and then when a client is selected in from the list and the command
button is clicked, the information for that client will be inserted into a
bookmark in the document. You may want to vary the manner in which it is
inserted to suit our exact requirements, but hopefully this will get you
started.

To make it easy for you, the code has been written so that it will deal with
any number of clients and any number of details about each client. It
assumes that the first row of the table containing the client details is a
header row.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

doctorjones_md

Doug,

I attempted to utilize your suggested code -- the List box code:
========================
On the UserForm, have a list box (ListBox1) and a Command Button
(CommandButton1) and use the following code in the UserForm_Initialize() and
the CommandButton1_Click() routines

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 Clients.doc
Application.ScreenUpdating = False
' Open the file containing the client details
Set sourcedoc = Documents.Open(FileName:="e:\worddocs\Clients.doc")
' Get the number or clients = number of rows in the table of client
details less one ....
===========================

(but couldn't get it to work) -- as far a functionality goes, I'd really
like to persue the my initial schema -- whereas, the user would make his or
her selection from checkboxes and radio buttons on a form, and data from a
corresponding Word Table would populate the Active Document.

I was hoping to be able to select a certain ROW from an underlying Word
Table and insert it into the Active Document, but I haven't figured out how
to master the RANGE issue you alluded to (any help would be greately
appreciated)

As a work-around, I broke the rows up into separate work documents and used
the following code (the problem I'm having with this is that it when I
select multiple checkboxes, the 1st selection performs ideal, but the second
selection nests within Cell 1 of the created table (any thoughts on how to
prevent this?)


Here's my code:

Dim sFilePath As String (I cut and pasted this code -- I really don't see
where I need this line -- the codes runs fine when I rem it out)

Private Sub cbxMattresses_Exit()
If Me.cbxSSLNotIncluded.Value = True Then

ActiveDocument.Bookmarks("MattressInsert").Range.InsertAfter
MattressInsert '***MattressInsert is a bookmark in my Active Document
sFilePath = ActiveDocument.AttachedTemplate.Path &
"\LineItems_Modified.doc"
Selection.InsertFile sFilePath, , False, False

Application.ScreenUpdating = True
'Selection.InsertFile sFilePath, Cells(1), False, False

Selection.InsertBreak Type:=wdSectionBreakNextPage

End If
End Sub



Private Sub cbxPillows_Click()

If Me.cbxPillows.Value = True Then

ActiveDocument.Bookmarks("PillowsInsert").Select
sFilePath = ActiveDocument.AttachedTemplate.Path &
"\LineItems_Modified.doc"
Selection.InsertFile sFilePath, , False, False

'cbxNotIncluded.Value = False
End If

End Sub



Private Sub cbxLinen_Click()

If Me.cbxLinen.Value = True Then

ActiveDocument.Bookmarks("LinenInsert").Select
sFilePath = ActiveDocument.AttachedTemplate.Path &
"\LineItems_Modified.doc"
Selection.InsertFile sFilePath, , False, False


'cbxNotIncluded.Value = False

End If

End Sub



Private Sub cbxNotIncluded_Click()
'cbxNotIncluded.Value = False

End Sub



Private Sub cmdCancel_Click()
Unload Me
End Sub



Private Sub cmdOK_Click()

Application.ScreenUpdating = True

Me.Hide

End Sub



Private Sub frmSSL_Initialize()
Me.cbxNotIncluded.Value = True
Me.cbxMattresses.Value = Null
Me.cbxPillows.Value = Null
Me.cbxLinen.Value = Null


End Sub





Here's what I have now -- I've broken the tables down into individual
 
D

Doctorjones_md

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
============================================================================
 

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