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