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 docuement 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]

Me![Return].Enabled = 1
Me![Return].SetFocus
Me![Button28].Enabled = 0
Me![DocSent] = True
DoCmd.Close acForm, "PatientDocHistory"
End With
.Application.Visible = True
.WindowState = wdWindowStateMaximize
End With
Set appWord = Nothing

Exit sub
 
W

Word Heretic

G'day "Max" <[email protected]>,

I'm afraid I will have to fall back to the std support line here:
please reduce this mess to six lines of code that demonstrates the
problem if you want me tackle it line by line.

My suggested solution is to load Word from a VBA Shell statement with
the template name appended after a /t switch. Said template name has
the autoopen magic necc to kick off the goodies.

An alternate solution is to to have a global template that upon doc
open checks for a fixed semaphore file that then contains the keyword
necc to trigger off the appropriate automagic.


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Max reckoned:
 
M

Max

Thanks for the suggestions and I do normally only include the limited code.
In this case the problem are was not obvious and if I had posted:
.Application.Visible = True
.WindowState = wdWindowStateMaximize

you would then want to know what else was happening in the code.

Max

Word Heretic said:
G'day "Max" <[email protected]>,

I'm afraid I will have to fall back to the std support line here:
please reduce this mess to six lines of code that demonstrates the
problem if you want me tackle it line by line.

My suggested solution is to load Word from a VBA Shell statement with
the template name appended after a /t switch. Said template name has
the autoopen magic necc to kick off the goodies.

An alternate solution is to to have a global template that upon doc
open checks for a fixed semaphore file that then contains the keyword
necc to trigger off the appropriate automagic.


Steve Hudson - Word Heretic
Want a hyperlinked index? S/W R&D? See WordHeretic.com

steve from wordheretic.com (Email replies require payment)


Max reckoned:
I am using the following code from within Access 97 to automate a WordXP
document.
When word is opened instead or reading the docuement 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]

Me![Return].Enabled = 1
Me![Return].SetFocus
Me![Button28].Enabled = 0
Me![DocSent] = True
DoCmd.Close acForm, "PatientDocHistory"
End With
.Application.Visible = True
.WindowState = wdWindowStateMaximize
End With
Set appWord = Nothing

Exit sub
 

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