Error 5852. Datasource is missing when automated.

A

_Alistair_

A script that has been working when it was used with Word 2000. When the
script is used with Word 2007 it gives the error 5852.

Word is automated by an external program written in VB6 which invokes the
program using this code
-----
Sub OpenWordDoc(Document As String, Macro As String)

Static WordObj As Variant
Static WordDoc As Variant

On Error Resume Next
Set WordObj = Nothing

Set WordObj = GetObject(, "Word.Application")

If Err.Number <> 0 Then 'If it's not running then start it
Set WordObj = CreateObject("Word.Application") '******
Err.Clear
End If

Set WordDoc = WordObj.Documents.Open(Document, , True)

WordObj.Visible = True
With WordObj
.Run Macro
End With
WordDoc.Close False
Set WordDoc = Nothing
Set WordObj = Nothing

End Sub
-----

The macro contains this code

-----
Public Sub OpenDeficiency()
Dim docname$
Dim category$
Dim firstwin$
Dim kpdoc$
Dim mypath

docname$ = ActiveDocument.MailMerge.DataSource.DataFields("appl_num").Value
docname$ = docname$ + ".doc"
docname$ = "J:\deficient\" + docname$
category$ = ActiveDocument.MailMerge.DataSource.DataFields("category").Value
-----

The last line of code here causes the problem and brings up the error 5852
only when Word 2007 isn't open before the external program automates word.
If it is open the Macro runs fine. The document uses a header and the Data
Source is a text file with fields delimited by '|'. When Word 2007 isn't
open before it is automated the DataSource doesn't even appear to be there.
The ActiveDocument.MailMerge.State property has the value wdNormalDocument
instead of having the value wdMainAndSourceAndHeader when the word program is
open before it is called.

Any ideas why the macro does not work when it is called by the program when
Word 2007 is not open and why it does work when Word is open?

Thanks
 
A

_Alistair_

Slight mistake, it is this line of code that gives the error:

docname$ = ActiveDocument.MailMerge.DataSource.DataFields("appl_num").Value
 

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