PDA

View Full Version : [SOLVED:] form show Modal to Modeless and back again



dbowlds
05-29-2018, 09:16 AM
Hello, I have a userform that is shown in modal form.
On that form I have a help button that, when clicked, opens up another word document (user guide) and, using bookmarks, goes to the right area in the document that pertains to that userform, thereby supplying the ability for the user to read detailed guidance information.

The problem, of course, is the userform needs to be modal to prevent the user from off-clicking the form and doing other things in word that will confuse the program.

I have tried the below code which successfully hides the userform, opens the user guide, goes to the proper bookmark, and then, once the user closes the user guide (or switches back to the original document), redisplays the userform, but the userform is now displayed modeless form. If I try to change it back to modal form (Me.Show 1), it displays instead of the user guide. Is there any way, once focus is returned to the userform, to then change the form back to a modal state?

I know that this may seem like a strange exercise, but the user guide is 200+ pages long so I cannot simply add all that data into the program itself. The program has dozens of userforms for a variety of functions, all covered in detail in the user guide. From what I have read, trying to use MSFT's help facility would be really painful. I also toyed with the idea of converting the user guide to Adobe PDF which would then be readable when a Word userform is in a modal state, but Adobe doesn't support Word bookmarks.

Any suggestions?


Private Sub cmdHelp_Click()
Me.Hide
Dim wdApp As Object
Dim wdDoc As Object
Dim strUserGuide As String
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err Then
Set wdApp = CreateObject("Word.Application")
End If
On Error GoTo 0
strUserGuide = Environ("USERPROFILE") & "\My Documents\[folder]\[user guide name].docx"
Set wdDoc = wdApp.Documents.Open(FileName:=strUserGuide)
wdDoc.range.Bookmarks("[BookmarkName]").Select
wdApp.Activate
Me.Show 0
End Sub

Dave
06-03-2018, 08:05 AM
Maybe run a timer to check when the Word document has closed? Call the sub before the end of the command sub. Untested. HTH. Dave

Public Sub CheckWordApp()
Dim Temp As Object
Before:
Call Waitatime
'action to shut off loop
On Error Resume Next
Set Temp = WdApp.ActiveDocument
If Err.Number <> 0 Then
On Error GoTo 0
Me.Show 1 '** replace Me with actual userform name
Set Temp = Nothing
Exit Sub
Else
GoTo Before
End If
End Sub

Function Waitatime()
Dim PauseTime, start
PauseTime = 1 ' Set duration.
start = Timer ' Set start time.
Do While Timer < start + PauseTime
DoEvents ' Yield to other processes.
Loop
End Function

Paul_Hossler
06-04-2018, 08:15 AM
If you're already in Word when you show the first UF, instead of creating another instance of Word, could you open a second (i.e. The Help doc), and do what you need to do?

I'm guessing (bold, underlined) that the second instance might be causing the problem

dbowlds
06-05-2018, 02:01 PM
Thanks Paul and Dave, for your ideas. After playing around with this more, I decided to bite the bullet and build an HTML help file. The code links to it quite nicely and it all is working well. I went to send it out to my team today and discovered that our IT dept strips .chm attachment files in Outlook (but allows .dotm files!). The pain, the pain.