Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening built-in dialog changes modality of UserForm 1

Status
Not open for further replies.

duGly

Technical User
Nov 13, 2006
52
US
I am using VBA with Word 2003. I have a UserForm (frmGraphicInsert) that automates the insertion of graphics into the document. The form opens modally (default).

Clicking an image on the UserForm opens the wdDialogInsertPicture built-in dialog (dialogs collection). When this happens, the UserForm mysteriously becomes modeless and the document window becomes active (ie. the UserForm loses focus).

1) Why does this happen?
2) Is there a workaround?
3) Once a UserForm is loaded, can I programmatically change its modality? (Something in the dialog obviously is.)
4) If not, how can I programmatically return the focus to the UserForm?

The code that opens the dialog is located in a Class Module:


' •g_ImageEvents_Click Class Event Handler•
' Purpose: Set picture for selected image.
Private Sub g_ImageEvents_Click()
'
' Object variables:
' imgMe = Control to be edited.
Dim imgMe As Image

' Set imgMe to selected control.
Set imgMe = frmGraphicInsert.Controls("imgPicture" & g_ctrlIndex)

' Display wdDialogInsertPicture dialog box to select picture.
On Error GoTo HandleErr
With Dialogs(wdDialogInsertPicture)
.Display
If .Name = "" Then Exit Sub

' Load picture; change ControlTipText; update g_strAryGraphics.
imgMe.Picture = LoadPicture(.Name)
imgMe.ControlTipText = "Click to Change Picture"
g_strAryGraphics(g_ctrlIndex, 0) = .Name
End With

' Repaint frmGraphicInsert.
frmGraphicInsert.Repaint

HandleErr:
Exit Sub

End Sub
 
1. Why is the code in a Class module, rather than the userform? Just curious.

2. Dialogs are Application events. Firing the dialog gives focus to the application. Hide the userform at the start of the code, then Show it again.
Code:
Sub Image1_Click()
UserForm1.Hide
  With Dialogs(wdDialogInsertPicture)
    .Display
  End With
UserForm1.Show
End Sub[/vba]
[quote]Once a UserForm is loaded, can I programmatically change its modality? (Something in the dialog obviously is.)[/quote]I don't believe the userform actually has its modality changed.  The application is given focus, and as it is at the top of the heap, it is not going to give it up unless you tell it to.  By hiding the userform, and [b]then[/b] firing the application event, you can give an explicit instruction to pass focus to the userform.  But you have to Hide it first.

Gerry
[url=http://www3.telus.net/public/fumei/]My paintings and sculpture[/url]
 
Thank you, Gerry. Your solution worked great. I actually placed the Hide and Show methods one after another after displaying the dialog because I wanted the UserForm to be visible (for reference) while the dialog was open. Doing it in this order, I was actually able to delete the Repaint method because the Show method (evidently) repaints the UserForm automatically.

Here's a portion of my code:

Code:
' Display wdDialogInsertPicture dialog box to select picture.
    With Dialogs(wdDialogInsertPicture)
        .Display
        If .Name = "" Then Exit Sub
        
        ' Load picture; change ControlTipText; update array.
        imgMe.Picture = LoadPicture(.Name)
        imgMe.ControlTipText = "Click to Change Picture"
        g_strAryGraphics(g_ctrlIndex, 0) = .Name
    End With
    
    ' Hide and then reshow frmGraphicInsert.
    With frmGraphicInsert
        .Hide
        .Show
    End With    ' frmGraphicInsert

As to your question about why I'm using a class module:

My UserForm uses between 1 and 16 image controls, each with five distinct label controls. Each image control and its respective labels need to respond to click events. The number of image controls needed depends on the user's selection in a combo box. Quite often, only one or two are needed and the rest (along with their labels) remain hidden.

I originally placed all the controls on the UserForm. But that many controls really slowed down initialization. Plus, my code was huge as each image and label needed its own event handler.

So I deleted all the controls and set them to be created programmatically as needed. I associated them with an array of controls (as shown below) and placed the event handlers in a class module.

Code:
' Update arrays.
ReDim Preserve m_aryImage(g_intGraphics)
Set m_aryImage(g_intGraphics).g_ImageEvents = imgPicture
m_aryImage(g_intGraphics).g_ctrlIndex = g_intGraphics

As to your comment regarding the modality:

Perhaps I am using the term modality incorrectly. Here is what was happening.

Prior to displaying the dialog, the UserForm was modal (ie. I could not click on or edit the document, nor access Words menus, etc.). This was my desired behavior.

After displaying the dialog, the Word application became the active window and I had to click the UserForm again to select it. I could click on and edit the document and access Word's menus. This was not my desired behavior.

Like I said, however, your suggestion worked great. Thank you so much for your help! [smile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top