Access automation error

M

Max

I am using the following code from within Access 97 to automate a WordXP
document.
When word is opened instead or reading the document and being left to work
on it the focus of the window comes back to access. I have added these lines
at just before the last end with and still get the problem:
.Application.Visible = True
.WindowState = wdWindowStateMaximize
I have checked the code with break points and nothing runs after this sub is
exited.

Thanks,

Max

Private Sub Button28_Click()
Dim DBPath As String, Template As String, DocPath As String
Dim WordPath As String, TemplatePath As String, Msg As String

Dim appWord As Object 'Word.Application
Dim wdWindowStateMaximize As Long
Dim Quotes As String
wdWindowStateMaximize = 1
Quotes = """"

WordPath = GetWordPath()
TemplatePath = GetTemplatePath()
DocPath = GetDocPath()

WordPath = Quotes & WordPath & Quotes

On Error Resume Next

Template = TemplatePath & Me![DocType].Column(2)

'Get an Application object so you can automate Word.
On Error Resume Next
Set appWord = GetObject(, "Word.Application")
If appWord Is Nothing Then
' If no instance running, start word
Set appWord = CreateObject("Word.Application")
End If

'Open a document based on the memo template, turn off the automatic spell
check
DoCmd.Hourglass False
With appWord
.Activate
.Application.Visible = True
.Application.ChangeFileOpenDirectory (DocPath)
.Documents.Add Template
.WindowState = wdWindowStateMaximize
.Application.CommandBars("Mail Merge").Visible = False
With .ActiveDocument
.ShowSpellingErrors = False
.View.ShowFieldCodes = False
.MailMerge.ViewMailMergeFieldCodes = False
.Fields.unlink
.SaveAs Filename:=DocPath & Me![DocFileName]

DoCmd.Close acForm, "PatientDocHistory"
End With
.Application.Visible = True
.WindowState = wdWindowStateMaximize
End With
Set appWord = Nothing

Exit sub
 
C

Cindy M -WordMVP-

Hi Max,

Have you also tried application.Activate? (For the Word application object, of
course. It's not clear from what you show if you're actually addressing that
object.)
I am using the following code from within Access 97 to automate a WordXP
document.
When word is opened instead or reading the document and being left to work
on it the focus of the window comes back to access. I have added these lines
at just before the last end with and still get the problem:
.Application.Visible = True
.WindowState = wdWindowStateMaximize
I have checked the code with break points and nothing runs after this sub is
exited.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
M

Max

Hi Cindy,
Yes I was.

The code I use is:

Set appWord = GetObject(, "Word.Application")
If appWord Is Nothing Then
' If no instance running, start word
Set appWord = CreateObject("Word.Application")
End If

'Open a document based on the memo template, turn off the automatic spell
check
DoCmd.Hourglass False
With appWord
.Activate
.Application.Visible = True
.Application.ChangeFileOpenDirectory (DocPath)
.Documents.Add Template
.WindowState = wdWindowStateMaximize
.Application.CommandBars("Mail Merge").Visible = False
With .ActiveDocument
.ShowSpellingErrors = False
.View.ShowFieldCodes = False
.MailMerge.ViewMailMergeFieldCodes = False
.Fields.unlink
.SaveAs Filename:=DocPath & Me![DocFileName]
DoCmd.Close acForm, "PatientDocHistory"
End With
.Application.Visible = True
.WindowState = wdWindowStateMaximize
End With
Set appWord = Nothing
 
C

Cindy M -WordMVP-

Hi Max,

I do see one thing that I'd change. No idea if this will help what you're
seeing, but it's worth a try. change the following two lines:

Set doc = .Documents.Add Template

With doc

Also, Set appWord = GetObject() should be returning an error (429) if Word
isn't running. So I assume you got an On Error Resume Next in your code
somewhere. This is extrememly dangerous, as you've no idea when an error
might be occurring that could be causing a problem. Try putting an On Error
GoTo 0 after using CreatObject and see if some error is occuring in the
code. Encountering a "silent" error could be what's causing focus to come
back into Access.

Note for the future that the better place to ask automation questions would
be one of the word.vba newsgroups (this one is for end-users)
The code I use is:

Set appWord = GetObject(, "Word.Application")
If appWord Is Nothing Then
' If no instance running, start word
Set appWord = CreateObject("Word.Application")
End If

'Open a document based on the memo template, turn off the automatic spell
check
DoCmd.Hourglass False
With appWord
.Activate
.Application.Visible = True
.Application.ChangeFileOpenDirectory (DocPath)
.Documents.Add Template
.WindowState = wdWindowStateMaximize
.Application.CommandBars("Mail Merge").Visible = False
With .ActiveDocument
.ShowSpellingErrors = False
.View.ShowFieldCodes = False
.MailMerge.ViewMailMergeFieldCodes = False
.Fields.unlink
.SaveAs Filename:=DocPath & Me![DocFileName]
DoCmd.Close acForm, "PatientDocHistory"
End With
.Application.Visible = True
.WindowState = wdWindowStateMaximize
End With
Set appWord = Nothing

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
M

Max

Thanks for your help Cindy.
Problem solved.

Max

Cindy M -WordMVP- said:
Hi Max,

I do see one thing that I'd change. No idea if this will help what you're
seeing, but it's worth a try. change the following two lines:

Set doc = .Documents.Add Template

With doc

Also, Set appWord = GetObject() should be returning an error (429) if Word
isn't running. So I assume you got an On Error Resume Next in your code
somewhere. This is extrememly dangerous, as you've no idea when an error
might be occurring that could be causing a problem. Try putting an On
Error
GoTo 0 after using CreatObject and see if some error is occuring in the
code. Encountering a "silent" error could be what's causing focus to come
back into Access.

Note for the future that the better place to ask automation questions
would
be one of the word.vba newsgroups (this one is for end-users)
The code I use is:

Set appWord = GetObject(, "Word.Application")
If appWord Is Nothing Then
' If no instance running, start word
Set appWord = CreateObject("Word.Application")
End If

'Open a document based on the memo template, turn off the automatic spell
check
DoCmd.Hourglass False
With appWord
.Activate
.Application.Visible = True
.Application.ChangeFileOpenDirectory (DocPath)
.Documents.Add Template
.WindowState = wdWindowStateMaximize
.Application.CommandBars("Mail Merge").Visible = False
With .ActiveDocument
.ShowSpellingErrors = False
.View.ShowFieldCodes = False
.MailMerge.ViewMailMergeFieldCodes = False
.Fields.unlink
.SaveAs Filename:=DocPath & Me![DocFileName]
DoCmd.Close acForm, "PatientDocHistory"
End With
.Application.Visible = True
.WindowState = wdWindowStateMaximize
End With
Set appWord = Nothing

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)


This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

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