Document Variables

M

Martinunique

I am developping an application using Visual Basic 6.
The application is creating a Word document from a Document template.
I am trying to put a larger amount of text into a document variable and
then save the document.
My idea was that a macro should read this data when the document is opened.

Somehow this doesnt work, WHY?

The code to create the document:

Public Function SaveFile(TemplateFileName As String, FileName As String) As
Boolean
On Error GoTo errhnd

Dim objWordApp As word.Application
Dim objWordDoc As word.Document
Dim FS As Scripting.FileSystemObject

Set objWordApp = New word.Application

If Not objWordApp Is Nothing Then
Set FS = New Scripting.FileSystemObject
If FS.FileExists(TemplateFileName) Then
Set objWordDoc = objWordApp.Documents.Add(TemplateFileName)
If FS.FileExists(FileName) Then
FS.DeleteFile FileName, True
End If
objWordDoc.SaveAs FileName, word.wdFormatDocument
End If
End If

'Just for test purpose,
objWordDoc.Variables.Add "tst", "Test Message"

objWordDoc.save

objWordDoc.Close True
Set objWordDoc = Nothing
objWordApp.Quit False
Set objWordApp = Nothing

SaveFile = True

Exit Function
errhnd:

SaveFile = False

If Not objWordDoc Is Nothing Then
objWordDoc.Close False
Set objWordDoc = Nothing
End If
If Not objWordApp Is Nothing Then
objWordApp.Quit False
Set objWordApp = Nothing
End If

End Function

The macro in the document is defined in the .dot file and is present in the
created .doc. It does run but raises an error.


Private Sub Document_Open()
On Error GoTo errhnd
MsgBox Variables.Count
MsgBox Variables.Item("tst").Value, , "tst"
Exit Sub
errhnd:
MsgBox Err.Description, , "Error " & Err.Number
End Sub

The Variables.Count returns 0 an as a consequence:
I get error 5825 "Object has been deleted." trying to fetch the "tst"
variable.

Any help is apperciated.
Martin
 
M

martinunique

I tried to add a TextBox shape and fill it with the info. The TextBox appears
but can't be found from the Document_Open macro.
I tried to move the code to the Document_Close event and the it worked!?
Both to find the TextBox shape and my original Docuemnt variable.
It seems like things just aren't instanciated properly when the
Document_Open event fires.
Is there a way to run a macro when an document is opened and still retrieve
the document variables?

Martin
 
H

Helmut Weber

Hi,
according to my frustrating experience, all properties
of a document are available only after "autoopen" has
been completed. Similar problems apply to other autoXYZ macros.
At least I couldn't find neither a solution nor a workaraound.
The thing I wanted to do, was positioning toolbars in autopen.
Even if I could see them on the screen, Word maintained,
there weren't existent. Probably due to something like "asynchronous
execution", maybe a relative of multitasking, which I abhor,
as you never know what happens when.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
C

Chuck

Hi Martin

I've been working with document variables on open when using
Documents.Open("mydocname.doc"). Not sure that is helpful to you given where
you're running your code from (not within Word I assume) but if you have
Documents.Open available to you perhaps you could try it?

Hope that helps...

Chuck
 

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