Puzzling behaviour

  • Thread starter Jean-Guy Marcil
  • Start date
J

Jean-Guy Marcil

Hi all!

I have a template that contains a complicated userform that lets the user
create a document according to the type of information they want and
according to corporate policies. Also, the template has customized toolbars
that offer assistance for later editing. The toolbar content is drastically
different based on the type of document created.

So, I built 4 toolbars by code, finished them by hand and added a
DocumentChange event to display the appropriate toolbar depending on the
type of document (which contains a DocVariable to tell the Change event
which toolbar to display).

So far, so good, everything works as I wanted.

I have, however, just discovered a small annoyance and after spending a few
hours on this I need help!

Let's say I create a document and save it (calling it Test1.doc)
I close Word.
I open Test1.doc
I double click on the template name in Windows Explorer to create a new
document based on the same template that Test1.doc is based on.
The procedure runs fine, I select my info and type of document through the
userform.
I click OK on the said userform.
The code runs and does its job.
When it has finished, the Windows taskbar highlights Document1 which has
just been created, but the screen displays Test1.doc.
This only happens in this case. If I create a document based on the template
and not save (leave it on the screen unsaved) and create a second document I
never see this behaviour.
If I keep creating documents based on the same template when Test1.doc is
first opened, then the document preceding the cone being created ends up
being displayed (i.e., when creating Document2, Document 1 ends up on the
screen while Document2 is highlighted on the taskbar, etc.)

I have tried adding ActiveWindow.SetFocus and/or ActivePane.Activate using
either ActiveDocument or Documents("DocumentName") to no avail. The funny
thing is that sometimes it does not do that.

Is this a coding, a Word or a display problem?

TIA

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
W

Word Heretic

G'day "Jean-Guy Marcil" <NoSpam@LeaveMeAlone>,

I suspect the userform code is playing with the windows. Don't. The
userform has handle to the original window (document). Thus you must
close the userform (me.hide) and then let the calling code do the
window / document stuff.

Eg

CODE MODULE
------------------------

Public Report as Document

Public Sub MyDummy()
....

MyForm.Show
Report.Activate
....

Set Report = Nothing
End Sub


EG FORM CODE
-------------------------

....
Set Report = New Document
Report.Insert MyStuff
....

Private Sub UserForm1_Terminate()
Me.Hide
End SUb

Ideally, the only stuff in the form is concerned with the interface of
the form itself. All other code is then isolated as public routines,
or methods of a custom object that is invoked by the form.

Another alternative is to have the report a child of the form, this is
not fully compliant with my ideal solution, but would still work:

FORM CODE
--------------------

Public Report as Document

.....

Set Report = New Document
Report.Insert MyStuff


CODE MODULE
------------------------

Public Sub MyDummy()
....

MyForm.Show
Myform.Report.Activate
Set MyForm.Report = Nothing
Set MyForm = Nothing
End Sub


So the ideal would be more like the first one, with

EG FORM CODE
-------------------------

....
MyPublicCodeModule.UpdateRoutine

....

Private Sub UserForm1_Terminate()
Me.Hide
End Sub


CODE MODULE MyPublicCodeModule
--------------------------------------------------------

Public Sub UpdateRoutine()
Set Report = New Document
Report.Insert MyStuff
End Sub

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Jean-Guy Marcil reckoned:
 
J

Jean-Guy Marcil

Word Heretic was telling us:
Word Heretic nous racontait que :
G'day "Jean-Guy Marcil" <NoSpam@LeaveMeAlone>,

I suspect the userform code is playing with the windows. Don't. The
userform has handle to the original window (document). Thus you must
close the userform (me.hide) and then let the calling code do the
window / document stuff.

That was it.

This is a monster of a template that started as a simple report. The client
has been asking for changes for 18 months and it now has over a thousand
lines of code.

In the OK click event I originally had a few lines of code to insert a few
items of text in the document at bookmarked areas.

Since then I have been adding code to this userform, but all of it is in a
standard module where I manipulate all the userform controls.
I had forgotten about those few lines in the OK button.
I moved them out and now I do not see the behaviour anymore.

Thanks for the info.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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