Automation with Word

  • Thread starter Thread starter Richard T via AccessMonster.com
  • Start date Start date
R

Richard T via AccessMonster.com

Hi, when I automate a procedure to send data from Access to Word (bookmarks)
then I find that the procedure runs smoothly but at the end the focus ends up
on the Database Window rather than the new doc. The focus does not even land
on the Access form. The procedure does include objWord.visible=true and
objWord.activate, but it makes no difference. Why should Access need to take
the focus to the Database Window? Has anyone heard of this?
Yours hopefully,
Richard
 
Thanks for your reply Arvin, I have inserted my code below and also checked
out your code. Apart form the method of adding to the bookmarks and your
printing etc, they seem similar. I have tried several options like
hardcoding the variables, adding & removing appWord.visible=False, etc. The
only thing that seems to hold the focus in Word is if I insert a msgbox after
End With (visible only when returning to Access from the Taskbar), but of
course the focus goes back to the Database Window after pressing Ok. I hope
that reveals where the problem might lie.
Best wishes
Richard

Code:

Dim appWord As Object
Dim strDocName As String, Filepath As String, strFileNameDate as String,
strCon As String

On Error GoTo err_AnnualAgreement

DoCmd.Hourglass True


Filepath = Forms!frmActiveCheckStatus!txtFilePath.Value
strFileNameDate = Format(Date, "dmmmyyyy")
strDocName = Me.txtStationeryFilepath.Value & "\AnnualAgreement.doc"
strCon = Me.cmboConsultant.Value
Set appWord = CreateObject("Word.Application")

With appWord
.Visible = False
.Documents.Open (strDocName)
.ActiveDocument.Range.Copy
.ActiveDocument.Close
.Documents.Add
.ActiveDocument.Range.Paste
.ActiveDocument.SaveAs Filepath & "\" & strCon &
"_AnnualAgreement " & _
strFileNameDate & ".doc"
.Visible = True
.Activate
End With


DoCmd.Hourglass False

cleanUp:
Set appWord = Nothing

exit_AnnualAgreement:
Exit Sub

err_AnnualAgreement:
MsgBox "An error occurred during the creation of the letter, if problem
proceeds please report the error description below" & vbCrLf & vbCrLf & Err.
Description, vbCritical + vbOKOnly, "Error..."
Resume Next
 
Could I add to my last posts as I've noticed a few more behaviours that are
quite strange:
Even whilst in my database when I open a form from another form, the focus is
not on the newly-opened form nor even the first one - but the database window
- until I click on the form of course. What could be pulling Access back to
the Db Window each time?
R
 
This is the line in my code which closes Word and lets the focus move back
to Access:

..ActiveDocument.Close (wdDoNotSaveChanges)

If you remove (or comment out) that line (and perhaps the one before it that
directs Word to print) the data fills and the document stays open. I had
some similar code in VBScript which works between Outlook and Word, and it
does the same thing, allowing the client to add some comments to the Word
document.
--
Arvin Meyer, MCP, MVP
Microsoft Access
Free Access downloads
http://www.datastrat.com
http://www.mvps.org/access
 
Hi Arvin, many thanks for your help. You know, I found out what the problem
was. There was not actually a problem with my code it was another factor
that I had completely overlooked - I had added # to the hyperlink address of
the command button in order to invoke the hand-pointer on mouse-over. I
didn't realise it was actually having an effect on the action (okay I know
that's quite an oversight). Not wanting to let go of the web-feel I have
added some code at the end of the procedure, which makes the hyperlink
address the same as the open doc, this nullifies it apart from adding the web
toolbar to Word which is quite handy anyway for the using to click Back to
return to the db. You don't think that would cause any other problem do you?
Best wishes
Richard
 
Back
Top