[Automation] Word Activate

  • Thread starter ALESSANDRO Baraldi
  • Start date
A

ALESSANDRO Baraldi

Hi all.
I use the code below to Open Word Application trought Office automation to
to generate my personal TestForm.Doc by a Template(Model.DOT).

All work good, but i don't know why the WordAppl don't stay on Top.

I execute the function on Click Button event's, and at the end of the
function the focus come back to Access and Word go on back.

I don't know absolutly why.

I set to Nothing the two variables [wordApp/wordDoc] on wordDoc_Close()
events.

Have you some solution to give me some idea....?

This is My Code(sorry for Italian comment):

Private WithEvents wordApp As Word.Application
Private WithEvents wordDoc As Word.Document

Private Function BuiltTestForm(strDOT As String, strDOC As String) As
Boolean
Dim ReplSel As Boolean
Dim fld As DAO.Field

On Error GoTo gestErrori

Screen.MousePointer = 11
Set wordApp = GetObject(, "Word.Application")

With wordApp
ReplSel = .Options.ReplaceSelection
.Options.ReplaceSelection = True
Set wordDoc = .Documents.Add(strDOT)
.WindowState = wdWindowStateMaximize
End With
'Loop trought Form Recordset Field's wich have the same Controls Name
'Also BookMark on Model.DOT have the same name, so the code it's easy:
For Each fld In Me.RecordsetClone.Fields
If Me.Controls(fld.Name).Tag <> "EXCLUDE" Then
If Not IsNothing(Me.Controls(fld.Name).Value) Then
If wordDoc.Bookmarks.Exists(fld.Name) Then
wordDoc.Bookmarks(fld.Name).Select
wordApp.Selection.TypeText Text:=Me.Controls(fld.Name).Value
End If
End If
End If
Next
wordDoc.Protect 2, True
wordApp.Options.ReplaceSelection = ReplSel

wordDoc.SaveAs strDOC
wordApp.Visible = True
wordApp.Activate
wordDoc.Activate

BuiltTestForm= True
Esci:
Screen.MousePointer = 0

Exit Function
gestErrori:
If err.Number = 429 Then
'se c'è stato un errore è perchè Word non era già aperto:
'aprilo adesso
Set wordApp = CreateObject("Word.Application")
Resume Next
Else
MsgBox err.Number & "-" & err.Description
GoTo Esci
End If
End Function

Lot of thanks.
 

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