Word User Form "grabbing" active instance of Word

S

Steven G

I have set up a template with a VBA coded user form for data entry. I have
noticed that if the Word setting "Windows in Taskbar" is selected, the user
form when started will "Grab" the open instance of word. If the settings is
unselected the user form will allow two instances of Word to run, the problem
I am having is telling the "UserForm" instance to maintain it's focus when
the "Save" button in the form is activated. The code will run partially up
to the point where I need to format the NEW document based on the template.

Questions
1. Is there a way to let the NEW document based on the"UserForm" template
to maintain it's focus so the code will run on the correct document?
2. If not is there a way to prevent a multiple instance of Word while the
"UserForm" template is running?

Please help, the code in the template runs correctly unless there is another
open instance of word. I have tested the code considerably but have run into
this snag and need to see if it can be corrected.

Thanks
 
C

Chuck Henrich

Without seeing the code ...

It sounds like your code is referring to the ActiveDocument. You could try
creating a Document object and set that to whatever document you want to work
with - that way you've got a handle that points to the correct (vs what Word
thinks is "active") document:

Dim docDoc As Document

Set docDoc = Documents.Add 'to create a new document

'use docDoc instead of ActiveDocument
 
S

Steven G

Thank you for your reply Chuck, I'm now wondering where the code should be
placed. Will the code go in the "This Document" or the "User Form", here is
the code I found to use in the "This Document" section.

Option Explicit

Private Sub Document_New()
Dim AnForm As UserForm

On Error GoTo Error_DocumentNew

Set AnForm = New UserForm2
goSolarForm

Exit_DocumentNew:
Exit Sub

Error_DocumentNew:

On Error Resume Next
Unload AnForm
Set AnForm = Nothing
ActiveDocument.Close wdDoNotSaveChanges

Resume Exit_DocumentNew
ActiveDocument.Close wdDoNotSaveChanges
ActiveWindow.SetFocus
ActiveWindow.Close SaveChanges:=wdSaveChanges

End Sub

I found this code sample on the web and it seems to work ok, but I'm not
that well versed in VBA to COMPLETELY understand how the "Error Trapping"
works. I would greatly appreciate any help you could offer and "Show"
according to what I already have.
 
C

Chuck Henrich

I'm assuming that the code we're talking about is the code you posted. If
the point of the code is to:
1. create a new document based on the template it's contained in
2. open a user form that contains code that does other things
3. unload the userform and close the document if there's a problem
and you want the code to run whenever a document is created based on this
template, then ThisDocument is the module your code should be in.

The code you posted should use a document object ("docDoc") instead of
Activedocument because if as part of an error situation the new document
being created off the template doesn't exist, then the next open document
will become the "active" document andt that document will be closed. If you
use a document object then you're referring to the document you specify
whether it's active or not, instead of whatever document is active at a
particular moment.

I've posted amended code below. You don't need the "Exit_DocumentNew:"
subroutine since all it does is exit the sub.

In the "Error_DocumentNew:" subroutine: I'm not a big fan of "On Error
Resume Next" unless you know exactly what error you're expecting because it
can hide problems. In this case it's fine because the problems that might
occur are:
1. AnForm can't be unloaded (because it's not there)
2. AnForm can't be set to nothing (because it's already nothing)
3. The document can't be closed (because it's not open)

At the end of the posted code there's a few lines after Resume
Exit_DocumentNew. Do you want the document to close without saving changes
after running the "goSolarForm" sub? If so, place the line that closes the
document before the errorhandling subroutine for clarity sake - it's easier
to follow.

It's not clear what the point of the following lines are:
ActiveWindow.SetFocus
ActiveWindow.Close SaveChanges:=wdSaveChanges
What purpose do they serve (aside from the obvious) - why is it necessary to
close the active window?

Here's the amended code:

Option Explicit

Private Sub Document_New()
Dim AnForm As UserForm
Dim docDoc As Document

On Error GoTo Error_DocumentNew

'Activedocument is the document being
'created off this template
'by setting the docDoc object to the
'active document you can specify that
'you're working with that specific
'document whether it's active or not
Set docDoc = Documents(ActiveDocument.Name)

Set AnForm = New UserForm2
goSolarForm

'Close the document here for clarity's sake.
'Setting the Saved property to True
'helps prevent "do you want to save"
'messages - it doesn't actually save
'the document, just marks it as saved
docDoc.Saved = True
docDoc.Close wdDoNotSaveChanges

'Exit Sub needed here to stop
'execution before the error subroutine
Exit Sub

Error_DocumentNew:

On Error Resume Next
Unload AnForm
Set AnForm = Nothing
docDoc.Saved = True
docDoc.Close wdDoNotSaveChanges

End Sub

HTH
 
W

Word Heretic

G'day "Steven G" <[email protected]>,

Number of issues here.

1) Modal forms can float between documents - are you modal or not?

2) Magic forms are hopeless for multiple document usage - formally
declare your form objects and don't use the magic method.

Magic:

UserForm1.Show

Non-magic

Set MyForm = UserForm1
Myform = new UserForm1 ' triggers init event
Myform.Show

....
Set Myform = nothing


3) Your code can easily do something like

Dim Target as Document

Set Target = Documents.Add

With Target
.Do whatever
....
End With


Steve Hudson - Word Heretic

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


Steven G reckoned:
 
D

David Macdonald

G'day Heretic,
This must be the thread you referred me to in answering my post from the 6th
(took a while finding it as the "recent posts" link in the author profile
seems to list just a few at random).
I'm not totally clear on how to use your suggestion as I'm not familiar with
some of the functions you mention but I'll play around and use this thread if
I get into difficulties.
 
W

Word Heretic

G'day David Macdonald <[email protected]>,

Yep yep yep - well doned :)

Steve Hudson - Word Heretic

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


David Macdonald reckoned:
 
W

Word Heretic

G'day "Steven G" <[email protected]>,

If you run it from a doc open event, you need to use an Ontime command
to delay the form load.
Private Sub Document_New()
Application.ontime now + timevalue$("00:00:03"),"TheRealDeal"
End Sub

Private Sub TheRealDeal()
Dim AnForm As UserForm

On Error GoTo Error_DocumentNew

Set AnForm = New UserForm2
goSolarForm

Exit_DocumentNew:
Exit Sub

Error_DocumentNew:

On Error Resume Next
Unload AnForm
Set AnForm = Nothing
ActiveDocument.Close wdDoNotSaveChanges

Resume Exit_DocumentNew
ActiveDocument.Close wdDoNotSaveChanges
ActiveWindow.SetFocus
ActiveWindow.Close SaveChanges:=wdSaveChanges

End Sub


Steve Hudson - Word Heretic

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


Steven G reckoned:
 
Top