My application creates Word documents. The routine below takes an existing, open document (vobjSourceDocu ment) and makes a new document (robjNewDocumen t) similar to it. The routine works 999+ times out of 1000. I cannot find any pattern regarding document type (template) or qualities of the source document, but once I have a source document that fails it will fail consistenly on any workstation.
In the code sample below it is trying to write to the new document, but it will fail on any object method I use. When the routine works, the VarType = 8 (string) but when it fails the VarType = 9 (automation object). In either case, IsObject always = True.
Thank you in advance,
Glen
[CODE=vb]
' Make an exact duplicate of the currently open document, optionally to a different
' template and return reference to the new.
Public Function DuplicateDocume nt(ByVal vobjSourceDocum ent As Word.Document, _
ByRef robjNewDocument As Word.Document, _
Optional strNewTemplate As String = "") As Boolean
Dim I As Long
Dim strBookmarkName As String
Dim strTemplateName As String
Dim strTemplateLang uage As String
Dim strArrayBookmar kNames() As String
Dim strArrayBookmar kValues() As String
Dim objWordApplicat ion As Word.Applicatio n
Dim GGADebug As String
On Error GoTo Error
DuplicateDocume nt = False
Set objWordApplicat ion = vobjSourceDocum ent.Application
' Store all the bookmark values in temp array
ReDim strArrayBookmar kNames(vobjSour ceDocument.Book marks.COUNT + 1)
ReDim strArrayBookmar kValues(vobjSou rceDocument.Boo kmarks.COUNT + 1)
For I = 1 To vobjSourceDocum ent.Bookmarks.C OUNT
strBookmarkName = vobjSourceDocum ent.Bookmarks.I tem(I)
strArrayBookmar kNames(I) = strBookmarkName
vobjSourceDocum ent.Bookmarks(I ).Select
strArrayBookmar kValues(I) = Selection.Text
End If
Next I
' copy body text inc. Annexes
If strNewTemplate = "" Then
' Get the current document's template name
strTemplateName = vobjSourceDocum ent.AttachedTem plate.FullName
Else
strTemplateName = strNewTemplate
End If
' create a new document based on the template name
blnWordFileDupl icate = True
'Set robjNewDocument = New Word.Document
Set robjNewDocument = objWordApplicat ion.Documents.a dd(strTemplateN ame)
GGADebug = VarType(robjNew Document) 'When this = 8 (string) it works, fails on 9 (Automation Object)
'How this can sometimes be 8 and sometimes 9 is a mystery
'Why it works when it is a string and not an object I don't know
GGADebug = GGADebug & " " & IsObject(robjNe wDocument) ‘IsObject is always true, even when VarType is String
blnWordFileDupl icate = False
' paste existing content into new document - do not copy styles \rt !!!
For I = 1 To UBound(strArray BookmarkNames() ) - 1
If strArrayBookmar kNames(I) <> cstrWordBookmar kBody Then
‘Fails on the next line
writeToDocument robjNewDocument , strArrayBookmar kNames(I), _
strArrayBookmar kValues(I), ""
'With robjNewDocument
' .EditGoto strArrayBookmar kNames(I)
' .Insert strArrayBookmar kValues(I)
'End With
End If
Next I
DuplicateDocume nt = True
Exit Function
Error:
objWordApplicat ion.ScreenUpdat ing = True
MsgBox LoadDBString(my LanguageOffset + 250) & " !" & vbNewLine & vbNewLine & _
"Source = " & Err.Source & vbNewLine & vbNewLine & GGADebug & vbNewLine & vbNewLine & _
" Description = " & Err.Description & " " & Err.Number, vbOKOnly, "Duplicate Document"
End Function[/CODE]
In the code sample below it is trying to write to the new document, but it will fail on any object method I use. When the routine works, the VarType = 8 (string) but when it fails the VarType = 9 (automation object). In either case, IsObject always = True.
Thank you in advance,
Glen
[CODE=vb]
' Make an exact duplicate of the currently open document, optionally to a different
' template and return reference to the new.
Public Function DuplicateDocume nt(ByVal vobjSourceDocum ent As Word.Document, _
ByRef robjNewDocument As Word.Document, _
Optional strNewTemplate As String = "") As Boolean
Dim I As Long
Dim strBookmarkName As String
Dim strTemplateName As String
Dim strTemplateLang uage As String
Dim strArrayBookmar kNames() As String
Dim strArrayBookmar kValues() As String
Dim objWordApplicat ion As Word.Applicatio n
Dim GGADebug As String
On Error GoTo Error
DuplicateDocume nt = False
Set objWordApplicat ion = vobjSourceDocum ent.Application
' Store all the bookmark values in temp array
ReDim strArrayBookmar kNames(vobjSour ceDocument.Book marks.COUNT + 1)
ReDim strArrayBookmar kValues(vobjSou rceDocument.Boo kmarks.COUNT + 1)
For I = 1 To vobjSourceDocum ent.Bookmarks.C OUNT
strBookmarkName = vobjSourceDocum ent.Bookmarks.I tem(I)
strArrayBookmar kNames(I) = strBookmarkName
vobjSourceDocum ent.Bookmarks(I ).Select
strArrayBookmar kValues(I) = Selection.Text
End If
Next I
' copy body text inc. Annexes
If strNewTemplate = "" Then
' Get the current document's template name
strTemplateName = vobjSourceDocum ent.AttachedTem plate.FullName
Else
strTemplateName = strNewTemplate
End If
' create a new document based on the template name
blnWordFileDupl icate = True
'Set robjNewDocument = New Word.Document
Set robjNewDocument = objWordApplicat ion.Documents.a dd(strTemplateN ame)
GGADebug = VarType(robjNew Document) 'When this = 8 (string) it works, fails on 9 (Automation Object)
'How this can sometimes be 8 and sometimes 9 is a mystery
'Why it works when it is a string and not an object I don't know
GGADebug = GGADebug & " " & IsObject(robjNe wDocument) ‘IsObject is always true, even when VarType is String
blnWordFileDupl icate = False
' paste existing content into new document - do not copy styles \rt !!!
For I = 1 To UBound(strArray BookmarkNames() ) - 1
If strArrayBookmar kNames(I) <> cstrWordBookmar kBody Then
‘Fails on the next line
writeToDocument robjNewDocument , strArrayBookmar kNames(I), _
strArrayBookmar kValues(I), ""
'With robjNewDocument
' .EditGoto strArrayBookmar kNames(I)
' .Insert strArrayBookmar kValues(I)
'End With
End If
Next I
DuplicateDocume nt = True
Exit Function
Error:
objWordApplicat ion.ScreenUpdat ing = True
MsgBox LoadDBString(my LanguageOffset + 250) & " !" & vbNewLine & vbNewLine & _
"Source = " & Err.Source & vbNewLine & vbNewLine & GGADebug & vbNewLine & vbNewLine & _
" Description = " & Err.Description & " " & Err.Number, vbOKOnly, "Duplicate Document"
End Function[/CODE]
Comment