
Form becomes active window but won't accept input with Wizard running behind it
As per Doug's request, here is the code of the macro.
'============================================================
'The following forms the NewMacros Module. Macro execution
'begins with the sub Insert_Client_Address()...
'============================================================
Public conn As Object, rs As Object
Sub Insert_Client_Address()
If Windows.Count >= 1 Then
Set conn = CreateObject("ADODB.Connection")
conn.Open "TheDatabase", "TheUser", "ThePassword"
SQL = "SELECT * " _
& "FROM Clients " _
& "WHERE Clients.[Address Line One] IS NOT NULL " _
& "ORDER BY Clients.[Client Name] "
Set rs = CreateObject("ADODB.Recordset")
rs.Open SQL, conn, 3, 1
Call Form_Initialize(rs)
formPasteInClient.Show
Else
MsgBox ("You must have an open document to use this macro.")
End If
End Sub
Sub Form_Initialize(rs)
Dim I
For I = 0 To rs.RecordCount - 1
formPasteInClient.comboClients.AddItem rs.Fields(1).Value
rs.MoveNext
Next I
End Sub
'=============================================================
'The following is the code for the form. The form itself has only
'the combo box "comboClients" and the command button
'"commandPasteInClient". The combo box displays the list of
'clients and the command button pastes in the address.
'=============================================================
Private Sub commandPasteInClient_Click()
Dim Index, I
Index = formPasteInClient.comboClients.ListIndex
NewMacros.rs.MoveFirst
For I = 0 To Index - 1
NewMacros.rs.MoveNext
Next I
If NewMacros.rs.Fields(1) <> "" Then
Selection.InsertAfter Text:=NewMacros.rs.Fields(1).Value
Selection.Collapse Direction:=wdCollapseEnd
Selection.InsertParagraphAfter
End If
Selection.InsertAfter Text:=NewMacros.rs.Fields(5).Value
Selection.Collapse Direction:=wdCollapseEnd
Selection.InsertParagraphAfter
If NewMacros.rs.Fields(6) <> "" Then
Selection.InsertAfter Text:=NewMacros.rs.Fields(6).Value & " "
Selection.Collapse Direction:=wdCollapseEnd
Selection.InsertParagraphAfter
End If
' Some repetitous code removed here...
formPasteInClient.Hide
NewMacros.rs.Close
NewMacros.conn.Close
End Sub
----------------------------------------------------------------------------
-------------------------
Ian,
Please paste the code of your macro into an email message rather than as an
attachment.
Please post any follow-up or new questions to the Newsgroups so that others
may benefit therefrom or contribute thereto.
Hope this helps,
Doug Robbins - Word MVP
Quote:
> I don't know if I'm approaching this problem the right way or not. (This
is
> my first real macro, so please bear with me.) Any help is greatly
> appreciated.
> I wrote a macro to pull a list of client names from an Access database.
The
> client names form a dropdown list, and a command button pastes the
client's
> address into the current document. This seems to work fine.
> However, if I assign a keystroke combination to the macro so that I can
run
> it after calling up the "Envelopes and Labels" tool, the form pops up and
> becomes the active window, but won't accept input (it just dings back at
me
> whenever I click anywhere). I cannot change the focus to another window,
> and since I cannot use or close the form window, the only way out it to
end
> the whole Word task. Other running applications are unaffected. Ideally,
> I'd like to be able to use the macro to paste an address into the
Envelopes
> and Labels wizard.
> Again, if anyone can explain this to me, that'd be great. If anyone can
> help me fix it, that'd be even better. Sample code is attached.
> Ian