Document out of focus

  • Thread starter Cor van der Bliek
  • Start date
C

Cor van der Bliek

I tried this question in the general section, but someone suggested to put it
here.

My problem is the following:
I have several templates, from which users can choose. On opening the
document, based on the template(e.g. document10) a UserForm pops up. When
clicking OK the results are put in the form on the created document10. This
works fine the first time for every template, but the second time a new
document11 is indeed opened, but the focus is set to the first document10 (of
the same template), thus trying to fill in an already filled in document.
This happens with every template, even if I switch between templates.
 
C

Cor van der Bliek

After studying this behaviour more careful, it looks like the newly created
document gets the focus very shortly, but (due to some parameter?), the focus
is switched back to the first doument.
In the template I use bookmarks, which should be unique for every document
and a form with a protected and non-protected section.
 
D

David

Search for the title of this message... 'Obtain the index number of
the active document'

They talk about document numbering within Word. When a new document
gets loaded, the Document numbers change. If you are refering to the
docs by using the Document(index) property (or Activedocument) you may
be inadvertantly using the wrong index. Always better to name your
documents an abritrary name so you can keep up with them.
Document("SALES") for example.

Are you, in fact, creating documents based on templates? You are not
loading the template for editing???
 
C

Cor van der Bliek

I'm creating new documents with the File New command, where the templates are
listed. In every template there's a AutoNew macro, which loads the UserForm:
Public Sub AutoNew()
Brief_Nederlands.Show
End Sub

The UserForm is activated with the following lines, in which personal
settings are loaded from C:\huisstijl_persoonlijk.txt:
Private Sub UserForm_Activate()
Me.Controls("Textbox8").Text = Format(Date, "d mmmm yyyy")
Me.Controls("Textbox3").Text = "t.a.v. "
Const ForReading = 1, ForWriting = 2, ForAppending = 3
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fs, f, ts, s
Set fs = Application.FileSearch
With fs
.LookIn = "C:\"
.SearchSubFolders = False
.FileName = "huisstijl_persoonlijk.txt"
If .Execute() > 0 Then
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile("C:\huisstijl_persoonlijk.txt")
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
s = ts.ReadLine
Me.Controls("Textbox11").Text = s
s = ts.ReadLine
Me.Controls("Textbox12").Text = s
s = ts.ReadLine
Me.Controls("Textbox13").Text = s
s = ts.ReadAll
s1 = InStr(s, "#")
Me.Controls("Textbox23").Text = Left(s, s1 - 1)
ts.Close
End If
End With
End Sub

Thanks for the effort.
 
C

Cor van der Bliek

Here it is, but that doesn't seem relevant, because at this point (when the
Userform is displayed), in the background the filled-in document(say
document3) is shown, as it has already switched places with the newest
document(document4), which should be displayed.

Private Sub OK_Click()
ActiveDocument.ActiveWindow.View.TableGridlines = True
For j = 1 To 22
tekst$ = Me.Controls("Textbox" & j).Text
Selection.GoTo What:=wdGoToBookmark, Name:=V(j)
With Selection.FormFields(1)
With .TextInput
.EditType Type:=wdRegularText, Default:=tekst$, Format:=""
End With
End With
Next j
End Sub

where the V-array contains the name of the bookmarks in the document.
 
C

Cor van der Bliek

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
 
D

David

I was going to suggest that, since nothing in your code ever specified
the activedocument.

I was able to recreate (basically) the action of the your code, but I
never could duplicate the problem.

Sorry I wasn't more help.
 

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