Automatically display UserForm when Word Document Opens

P

Pamela Cheek

I have a UserForm which has pulls information from a Word
document that contains nothing but two tables of
information.

The UserForm works great, and exits gracefully, but will
not automatically display when the end user opens the word
document it is built upon. I don't want the tables to be
visible to the user, just the form. Code follows:

Private Sub ThisDocument_Document_Open()
Load UserForm1
UserForm1.Show vbModeless
End Sub

Private Sub CommandButton2_Click()
Dim Msg, Style 'set dim for message box when Exit is
pressed

Msg = "Please wait while program shuts down" ' define
message
Style = vbYesOk + vbCritical + vbDefaultButton2 '
define message response buttons
Response = MsgBox(Msg, Style) ' Display message

If True Then Unload UserForm1
Application.DisplayAlerts = False

Word.Application.Quit

End Sub

Private Sub UserForm_Initialize()


Dim MyArray() As String
RowCount = ActiveDocument.Tables(1).Rows.Count
RowCount = ActiveDocument.Tables(1).Rows.Count
ColCount = ActiveDocument.Tables(1).Columns.Count
ReDim MyArray(RowCount - 1, ColCount - 1)
For i = 1 To RowCount
For j = 1 To ColCount
'Select each cell in the table
Celldata = ActiveDocument.Tables(1).Cell(i, j)
'Remove the paragraph and end-of-cell markers
'as we lod the array

MyArray(i - 1, j - 1) = Left(Celldata, Len
(Celldata) - 2)
Next
Next
ListBox1.ColumnCount = ColCount
ListBox1.List() = MyArray


Dim MyArrayExternal() As String
RowCount = ActiveDocument.Tables(2).Rows.Count
RowCount = ActiveDocument.Tables(2).Rows.Count
ColCount = ActiveDocument.Tables(2).Columns.Count
ReDim MyArrayExternal(RowCount - 1, ColCount - 1)
For m = 1 To RowCount
For n = 1 To ColCount
'Select each cell in the table
Celldata = ActiveDocument.Tables(2).Cell(m, n)
'Remove the paragraph and end-of-cell markers
'as we load the array
MyArrayExternal(m - 1, n - 1) = Left(Celldata,
Len(Celldata) - 2)
Next
Next
ListBox2.ColumnCount = ColCount
ListBox2.List() = MyArrayExternal


End Sub

Private Sub ListBox1_Click()

For x = 0 To ListBox1.ListCount

If ListBox1.Selected(x) = True Then
MsgBox ListBox1.List(x) & ListSeparator
& " " & " Office Symbol: " & ListSeparator
& " " & ListBox1.List(x, 1) & " Project: " &
ListBox1.List(x, 2)
End If
Next x
End Sub

Private Sub ListBox2_Click()

For x = 0 To ListBox2.ListCount

If ListBox2.Selected(x) = True Then
MsgBox ListBox2.List(x) & ListSeparator
& " " & " Office Symbol: " & ListSeparator
& " " & ListBox2.List(x, 1) & " Project: " &
ListBox2.List(x, 2)
End If
Next x
End Sub


Obviously code is rough and not fully documented yet, but
will be at end product.

How do I automatically Display a UserForm when the Word
Document is Opened?

Any ideas?

Thanks

PAM
 
J

JGM

Hi Pamela,

Change
Private Sub ThisDocument_Document_Open()

to

Private Sub Document_Open()

and make sure that the Document_Open procedure is in the ThisDocument
module.

HTH
Cheers!
 
G

Guest

Thanks it works like a charm
-----Original Message-----
Hi Pamela,

Change


to

Private Sub Document_Open()

and make sure that the Document_Open procedure is in the ThisDocument
module.

HTH
Cheers!

--
_______________________________________
Jean-Guy Marcil
(e-mail address removed)

"Pamela Cheek" <[email protected]> a écrit dans le message
de news: [email protected]...


.
 

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