O
onlyvimal
Hi,
Am working on a mail merge web application which generates documents
and saves them. Being a rookie in Mail Merge and Word Automation, have
to thank MVPs and other Google Groups Users for helping me solve quite
a few problems and I have recently moved my app to prod.
However, the app throws a few erratic errors which am not able to
reproduce in order to fix them. Request help in this regard as I feel
like am hitting against a wall as its now a prod issue.
A couple of errors are such:
1. The message filter indicated that the application is busy.
2. The remote server machine does not exist or is unavailable.
Am using VB 6.0 and Microsoft Word Library 9.0 on Win2K Server. The
following is the code I have. The GenerateDoc function takes an
instance of Word Application object and does the mail merge from a data
source while at the same time putting placeholders into documents for
form text fields. The doFindReplace Function replaces the placeholders
after merging the document document with a data source.
The above stated errors are thrown from the doFindReplace function.
Also, can someone please explain to me as to how WinWord manages
concurrency while its running on a server(invoked through a COM
component) for use in a web application.
Dim objWordApp As Word.Application
Set objWordApp = New Word.Application
Private Function GenerateDoc( ByVal strFileName as String,
ByRef objWordApp As Word.Application,
ByVal strDocToGenPath as String,
ByRef vError As Variant) As Boolean
On Error GoTo ErrHandler
Dim objBaseTemplate As Word.Document
Dim objGenDoc As Word.Document
Dim frmField As FormField
Dim strFieldText() As String
Dim iCount As Integer
Dim blnFormFieldsFlag As Boolean
blnFormFieldsFlag = False
Set objBaseTemplate = objWordApp.Documents.Add(Template:=strFileName,
Visible:=True)
objBaseTemplate.ActiveWindow.Visible = True
objBaseTemplate.MailMerge.OpenDataSource strDataSource
objBaseTemplate.MailMerge.Destination = wdSendToNewDocument
objBaseTemplate.MailMerge.SuppressBlankLines = True
If objBaseTemplate.MailMerge.fields.Count = 0 Then
objBaseTemplate.Protect wdAllowOnlyFormFields, True, strPassword
objBaseTemplate.SaveAs FileName:=strDocToGenPath
objBaseTemplate.Close
Set objBaseTemplate = Nothing
Else
For Each frmField In objBaseTemplate.FormFields
If frmField.Type = wdFieldFormTextInput Then
' Redim array to hold contents of text field.
ReDim Preserve strFieldText(1, iCount + 1)
' Place content and name of field into array.
strFieldText(0, iCount) = frmField.Result
strFieldText(1, iCount) = frmField.Name
' Select the form field.
frmField.Select
' Replace it with placeholder text.
objBaseTemplate.Application.Selection.TypeText "<" & strFieldText(1,
iCount) & "PlaceHolder>"
' Increment icount
iCount = iCount + 1
blnFormFieldsFlag = True
End If
Next frmField
objBaseTemplate.MailMerge.Execute
If blnFormFieldsFlag = True Then
doFindReplace iCount, frmField, strFieldText(), objGenDoc
End If
objBaseTemplate.Saved = True
objBaseTemplate.Close
Set objBaseTemplate = Nothing
Set objGenDoc = objWordApp.ActiveDocument
objGenDoc.Protect wdAllowOnlyFormFields, True, strPassword
objGenDoc.SaveAs FileName:=strDocToGenPath
objGenDoc.Close
Set objGenDoc = Nothing
End If
GenerateDoc = True
Exit Function
ErrHandler:
LogError strFunction, Err.Number, Err.Description & " - (Doc Id " &
CStr(strFileName) & ") ", Err.source
GenerateDoc = False
Err.Clear
If Not objBaseTemplate Is Nothing Then
objBaseTemplate.Close
Set objBaseTemplate = Nothing
End If
If Not objGenDoc Is Nothing Then
objGenDoc.Close
Set objGenDoc = Nothing
End If
End Function
Private Function doFindReplace(iCount As Integer, frmField As
FormField, fFieldText() As String, ByRef objWordDoc As Word.Document)
As Boolean
On Error GoTo ErrHandler:
Dim intLoop As Integer
Dim strFunction As String
Dim fField As FormField
strFunction = "doFindReplace"
' Go to top of document.
Selection.HomeKey Unit:=wdStory
' Initialize Find.
Selection.Find.ClearFormatting
With Selection.Find
..Forward = True
..Wrap = wdFindContinue
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False
' Loop form fields count.
For intLoop = 0 To iCount
' Execute the find.
Do While .Execute(FindText:="<" & fFieldText(1, intLoop) _
& "PlaceHolder>") = True
' Replace the placeholder with the form field.
Set fField = Selection.FormFields.Add _
(Range:=Selection.Range, Type:=wdFieldFormTextInput)
' Restore form field contents and bookmark name.
fField.Result = fFieldText(0, intLoop)
fField.Name = fFieldText(1, intLoop)
Loop
' Go to top of document for next find.
Selection.HomeKey Unit:=wdStory
Next
End With
doFindReplace = True
Exit Function
ErrHandler:
LogError strFunction, Err.Number, Err.Description, Err.source
Err.Clear
doFindReplace = False
End Function
Any help in this regard is highly appreciated. Thanks in advance.
Vimal
Am working on a mail merge web application which generates documents
and saves them. Being a rookie in Mail Merge and Word Automation, have
to thank MVPs and other Google Groups Users for helping me solve quite
a few problems and I have recently moved my app to prod.
However, the app throws a few erratic errors which am not able to
reproduce in order to fix them. Request help in this regard as I feel
like am hitting against a wall as its now a prod issue.
A couple of errors are such:
1. The message filter indicated that the application is busy.
2. The remote server machine does not exist or is unavailable.
Am using VB 6.0 and Microsoft Word Library 9.0 on Win2K Server. The
following is the code I have. The GenerateDoc function takes an
instance of Word Application object and does the mail merge from a data
source while at the same time putting placeholders into documents for
form text fields. The doFindReplace Function replaces the placeholders
after merging the document document with a data source.
The above stated errors are thrown from the doFindReplace function.
Also, can someone please explain to me as to how WinWord manages
concurrency while its running on a server(invoked through a COM
component) for use in a web application.
Dim objWordApp As Word.Application
Set objWordApp = New Word.Application
Private Function GenerateDoc( ByVal strFileName as String,
ByRef objWordApp As Word.Application,
ByVal strDocToGenPath as String,
ByRef vError As Variant) As Boolean
On Error GoTo ErrHandler
Dim objBaseTemplate As Word.Document
Dim objGenDoc As Word.Document
Dim frmField As FormField
Dim strFieldText() As String
Dim iCount As Integer
Dim blnFormFieldsFlag As Boolean
blnFormFieldsFlag = False
Set objBaseTemplate = objWordApp.Documents.Add(Template:=strFileName,
Visible:=True)
objBaseTemplate.ActiveWindow.Visible = True
objBaseTemplate.MailMerge.OpenDataSource strDataSource
objBaseTemplate.MailMerge.Destination = wdSendToNewDocument
objBaseTemplate.MailMerge.SuppressBlankLines = True
If objBaseTemplate.MailMerge.fields.Count = 0 Then
objBaseTemplate.Protect wdAllowOnlyFormFields, True, strPassword
objBaseTemplate.SaveAs FileName:=strDocToGenPath
objBaseTemplate.Close
Set objBaseTemplate = Nothing
Else
For Each frmField In objBaseTemplate.FormFields
If frmField.Type = wdFieldFormTextInput Then
' Redim array to hold contents of text field.
ReDim Preserve strFieldText(1, iCount + 1)
' Place content and name of field into array.
strFieldText(0, iCount) = frmField.Result
strFieldText(1, iCount) = frmField.Name
' Select the form field.
frmField.Select
' Replace it with placeholder text.
objBaseTemplate.Application.Selection.TypeText "<" & strFieldText(1,
iCount) & "PlaceHolder>"
' Increment icount
iCount = iCount + 1
blnFormFieldsFlag = True
End If
Next frmField
objBaseTemplate.MailMerge.Execute
If blnFormFieldsFlag = True Then
doFindReplace iCount, frmField, strFieldText(), objGenDoc
End If
objBaseTemplate.Saved = True
objBaseTemplate.Close
Set objBaseTemplate = Nothing
Set objGenDoc = objWordApp.ActiveDocument
objGenDoc.Protect wdAllowOnlyFormFields, True, strPassword
objGenDoc.SaveAs FileName:=strDocToGenPath
objGenDoc.Close
Set objGenDoc = Nothing
End If
GenerateDoc = True
Exit Function
ErrHandler:
LogError strFunction, Err.Number, Err.Description & " - (Doc Id " &
CStr(strFileName) & ") ", Err.source
GenerateDoc = False
Err.Clear
If Not objBaseTemplate Is Nothing Then
objBaseTemplate.Close
Set objBaseTemplate = Nothing
End If
If Not objGenDoc Is Nothing Then
objGenDoc.Close
Set objGenDoc = Nothing
End If
End Function
Private Function doFindReplace(iCount As Integer, frmField As
FormField, fFieldText() As String, ByRef objWordDoc As Word.Document)
As Boolean
On Error GoTo ErrHandler:
Dim intLoop As Integer
Dim strFunction As String
Dim fField As FormField
strFunction = "doFindReplace"
' Go to top of document.
Selection.HomeKey Unit:=wdStory
' Initialize Find.
Selection.Find.ClearFormatting
With Selection.Find
..Forward = True
..Wrap = wdFindContinue
..Format = False
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False
' Loop form fields count.
For intLoop = 0 To iCount
' Execute the find.
Do While .Execute(FindText:="<" & fFieldText(1, intLoop) _
& "PlaceHolder>") = True
' Replace the placeholder with the form field.
Set fField = Selection.FormFields.Add _
(Range:=Selection.Range, Type:=wdFieldFormTextInput)
' Restore form field contents and bookmark name.
fField.Result = fFieldText(0, intLoop)
fField.Name = fFieldText(1, intLoop)
Loop
' Go to top of document for next find.
Selection.HomeKey Unit:=wdStory
Next
End With
doFindReplace = True
Exit Function
ErrHandler:
LogError strFunction, Err.Number, Err.Description, Err.source
Err.Clear
doFindReplace = False
End Function
Any help in this regard is highly appreciated. Thanks in advance.
Vimal