#1   Report Post  
Cor van der Bliek
 
Posts: n/a
Default Document out of focus

I create documents, based on a special template. A Userforms collect all the
text in the fields and puts them on the appropriate places in the document.
This works fine the first time I do this. The second time, with a document
showing I have filled this way, a new document is created, but doesn't get
the focus. In other words when the Userform pops up, the first document is
still showing as the active document and the process of putting the input
text in the the new document fails, since Word tries to put the text in the
already filled in document.
The new document has been created, it shows as second in the list under the
Windows command.
How do I get the new document to become the active document before showing
the Userform?
  #2   Report Post  
AA2e72E
 
Posts: n/a
Default

try inserting this in your code just before you begin to populate the newly
created document:

Application.Documents(2).Activate


"Cor van der Bliek" wrote:

I create documents, based on a special template. A Userforms collect all the
text in the fields and puts them on the appropriate places in the document.
This works fine the first time I do this. The second time, with a document
showing I have filled this way, a new document is created, but doesn't get
the focus. In other words when the Userform pops up, the first document is
still showing as the active document and the process of putting the input
text in the the new document fails, since Word tries to put the text in the
already filled in document.
The new document has been created, it shows as second in the list under the
Windows command.
How do I get the new document to become the active document before showing
the Userform?

  #3   Report Post  
Cor van der Bliek
 
Posts: n/a
Default

This leads Word to Document2, if this exists. Otherwise there's an error, and
it doesn't work when my active document is say document10.

It should be something like Application.Documents(Last+1).Activate

"AA2e72E" wrote:

try inserting this in your code just before you begin to populate the newly
created document:

Application.Documents(2).Activate


"Cor van der Bliek" wrote:

I create documents, based on a special template. A Userforms collect all the
text in the fields and puts them on the appropriate places in the document.
This works fine the first time I do this. The second time, with a document
showing I have filled this way, a new document is created, but doesn't get
the focus. In other words when the Userform pops up, the first document is
still showing as the active document and the process of putting the input
text in the the new document fails, since Word tries to put the text in the
already filled in document.
The new document has been created, it shows as second in the list under the
Windows command.
How do I get the new document to become the active document before showing
the Userform?

  #4   Report Post  
AA2e72E
 
Posts: n/a
Default

Application.Documents.Count returns the number of documents open.

I don't think you can be sure of the index of the target document all of the
time unless you open/create the target document before you do anything else:
then,

application.documents(1).Activate

"Cor van der Bliek" wrote:

This leads Word to Document2, if this exists. Otherwise there's an error, and
it doesn't work when my active document is say document10.

It should be something like Application.Documents(Last+1).Activate

"AA2e72E" wrote:

try inserting this in your code just before you begin to populate the newly
created document:

Application.Documents(2).Activate


"Cor van der Bliek" wrote:

I create documents, based on a special template. A Userforms collect all the
text in the fields and puts them on the appropriate places in the document.
This works fine the first time I do this. The second time, with a document
showing I have filled this way, a new document is created, but doesn't get
the focus. In other words when the Userform pops up, the first document is
still showing as the active document and the process of putting the input
text in the the new document fails, since Word tries to put the text in the
already filled in document.
The new document has been created, it shows as second in the list under the
Windows command.
How do I get the new document to become the active document before showing
the Userform?

  #5   Report Post  
Stefan Blom
 
Posts: n/a
Default

Is the new document created with code? If it is, you could make use of
the return value from the Add method to get a reference to it.

Set mydoc = Documents.Add()

With mydoc
'Code here...
End With

If you are not creating the new document with code, maybe you should,
just to be able to reference it.

--
Stefan Blom


"Cor van der Bliek" wrote
in message ...
I create documents, based on a special template. A Userforms collect

all the
text in the fields and puts them on the appropriate places in the

document.
This works fine the first time I do this. The second time, with a

document
showing I have filled this way, a new document is created, but

doesn't get
the focus. In other words when the Userform pops up, the first

document is
still showing as the active document and the process of putting the

input
text in the the new document fails, since Word tries to put the text

in the
already filled in document.
The new document has been created, it shows as second in the list

under the
Windows command.
How do I get the new document to become the active document before

showing
the Userform?







  #6   Report Post  
Cor van der Bliek
 
Posts: n/a
Default

I tried this, but can't figure it out, Stefan.
Could you detail this for me?


"Stefan Blom" wrote:

Is the new document created with code? If it is, you could make use of
the return value from the Add method to get a reference to it.

Set mydoc = Documents.Add()

With mydoc
'Code here...
End With

If you are not creating the new document with code, maybe you should,
just to be able to reference it.

--
Stefan Blom


"Cor van der Bliek" wrote
in message ...
I create documents, based on a special template. A Userforms collect

all the
text in the fields and puts them on the appropriate places in the

document.
This works fine the first time I do this. The second time, with a

document
showing I have filled this way, a new document is created, but

doesn't get
the focus. In other words when the Userform pops up, the first

document is
still showing as the active document and the process of putting the

input
text in the the new document fails, since Word tries to put the text

in the
already filled in document.
The new document has been created, it shows as second in the list

under the
Windows command.
How do I get the new document to become the active document before

showing
the Userform?






  #7   Report Post  
Stefan Blom
 
Posts: n/a
Default

My point was that you shouldn't depend on what document is currently active,
if this is possible. But I haven't seen your code, and I'm definitely not a
programming expert, so I can't guarantee that I would know the solution even
if I had. If you need more information about the Add method of the Document
class, you could read about it in Word VBA Help. If you do find something
that could be useful, I suggest that you ask the question again, providing a
code sample in a vba newsgroup such as microsoft.public.word.vba.general.

Stefan Blom


"Cor van der Bliek" wrote:

I tried this, but can't figure it out, Stefan.
Could you detail this for me?


"Stefan Blom" wrote:

Is the new document created with code? If it is, you could make use of
the return value from the Add method to get a reference to it.

Set mydoc = Documents.Add()

With mydoc
'Code here...
End With

If you are not creating the new document with code, maybe you should,
just to be able to reference it.

--
Stefan Blom


"Cor van der Bliek" wrote
in message ...
I create documents, based on a special template. A Userforms collect

all the
text in the fields and puts them on the appropriate places in the

document.
This works fine the first time I do this. The second time, with a

document
showing I have filled this way, a new document is created, but

doesn't get
the focus. In other words when the Userform pops up, the first

document is
still showing as the active document and the process of putting the

input
text in the the new document fails, since Word tries to put the text

in the
already filled in document.
The new document has been created, it shows as second in the list

under the
Windows command.
How do I get the new document to become the active document before

showing
the Userform?






  #8   Report Post  
Cor van der Bliek
 
Posts: n/a
Default

I managed to solve the problem through this workaround:
on opening the template (and before showing the UserForm) I record the name
of the newly made document and save it to oDoc.
After closing the UserForm I activate oDoc. This seems to do the trick.
I still don't understand why the focus was lost on opening the UserForm.

Public oDoc As Document
Public Sub AutoNew()
Set oDoc = ActiveDocument
UserForm.Show
oDoc.Activate
End Sub
-------------
Private Sub OK_Click()
UserForm.Hide
oDoc.Activate
'overnemen invulvelden
End Sub
----------------
"Stefan Blom" wrote:

My point was that you shouldn't depend on what document is currently active,
if this is possible. But I haven't seen your code, and I'm definitely not a
programming expert, so I can't guarantee that I would know the solution even
if I had. If you need more information about the Add method of the Document
class, you could read about it in Word VBA Help. If you do find something
that could be useful, I suggest that you ask the question again, providing a
code sample in a vba newsgroup such as microsoft.public.word.vba.general.

Stefan Blom


"Cor van der Bliek" wrote:

I tried this, but can't figure it out, Stefan.
Could you detail this for me?


"Stefan Blom" wrote:

Is the new document created with code? If it is, you could make use of
the return value from the Add method to get a reference to it.

Set mydoc = Documents.Add()

With mydoc
'Code here...
End With

If you are not creating the new document with code, maybe you should,
just to be able to reference it.

--
Stefan Blom


"Cor van der Bliek" wrote
in message ...
I create documents, based on a special template. A Userforms collect
all the
text in the fields and puts them on the appropriate places in the
document.
This works fine the first time I do this. The second time, with a
document
showing I have filled this way, a new document is created, but
doesn't get
the focus. In other words when the Userform pops up, the first
document is
still showing as the active document and the process of putting the
input
text in the the new document fails, since Word tries to put the text
in the
already filled in document.
The new document has been created, it shows as second in the list
under the
Windows command.
How do I get the new document to become the active document before
showing
the Userform?





  #9   Report Post  
Stefan Blom
 
Posts: n/a
Default

I'm glad that you managed to find a solution.

Note that some of the VBA groups probably knows why opening the UserForm
caused the document to lose the focus.

Stefan Blom
Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Opening a document in a protected form CheeryNan Microsoft Word Help 5 August 1st 22 07:34 AM
Help! Word document Protection Pinky Page Layout 11 December 13th 06 09:21 AM
Can you save individual document pages as seperate word files? John Calligy Mailmerge 2 April 12th 05 09:14 AM
Newbie document question please Dudley Henriques New Users 4 January 9th 05 09:38 PM
Document object in protected form Bruce Tables 2 December 6th 04 08:09 PM


All times are GMT +1. The time now is 01:56 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"