Link to home
Start Free TrialLog in
Avatar of RIAS
RIASFlag for United Kingdom of Great Britain and Northern Ireland

asked on

'Application is Busy' and 'Call was Rejected By Callee' Errors

Hello,
I am trying to do word mailmerge and I get this error.
Any suggestion on how to fix it.
I am using visual studio 2015 vb.net desktop app.
my code for mailmerge is
Public Sub CreateWordDocumentWithTableFormat(ByVal strFaxAuth As String)
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        Dim dtSQL As New DataTable
        Dim outFile As String = String.Empty
        Dim directoryName As String = String.Empty
        Dim oWord As Microsoft.Office.Interop.Word.Application
        Dim oDoc As Microsoft.Office.Interop.Word.Document
        Dim clsFrmmain As New ClsFrmMainLoad
        '           @tableName varchar(100) = null,
        '@ColumnName1 varchar(100) = null,
        '@Value varchar(100) = null

        Try
            SQL.OpenConnection()
            Dim SQLobj As New SqlCommand("WordReport")
            Dim Str_Date As String = String.Empty
            SQLobj.Parameters.AddWithValue("@tablename", "Diy")
            SQLobj.Parameters.AddWithValue("@ColumnName1 ", "FaxAuth")
            SQLobj.Parameters.AddWithValue("@Value", strFaxAuth)
            SQLobj.CommandType = CommandType.StoredProcedure
            SQLobj.Connection = SQL.GetConnection
            clsFrmmain.Populate(SQLobj)
            dtSQL = clsFrmmain.Data_Table

            If dtSQL.Rows.Count <> 0 Then


                Dim oTemplate As String = ReturnWordTemplatePath() 
                oWord = CreateObject("Word.Application")
                oWord.Visible = True
                oDoc = oWord.Documents.Add
                If IsNothing(oTemplate) = True Then
                    WordClose(oWord, oDoc)
                    Exit Sub
                End If
                oDoc = oWord.Documents.Open(oTemplate)
                
                outFile = System.IO.Path.GetFileNameWithoutExtension(oTemplate)
                outFile = outFile + str_DateToday
                directoryName = System.IO.Path.GetDirectoryName(oTemplate)
                oDoc.SaveAs2(directoryName & "\" & outFile & ".doc")
                WordClose(oWord, oDoc)
            Else
                clsFrmmain.FormatMessage(18, "", str, " Word Letter Information")
            End If

Open in new window

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Which line resulted in the error ?
Avatar of RIAS

ASKER

If dtSQL.Rows(0)("Attn").ToString Is DBNull.Value = False Then
                    oDoc.Content.Find.Execute(FindText:="ATTN:",
                   ReplaceWith:="ATTN: " & dtSQL.Rows(0)("Attn").ToString, Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, MatchCase:=True)

it is this line in there ;dont know whether it is the matchcase causing it?
Avatar of RIAS

ASKER

looks like if it is not able to find FindText:="ATTN:", its giving an error.
Any workaround for this?
Hmmm.  That line giving the problem isn't in the code you posted.  Please post the correct code.
Avatar of RIAS

ASKER

Public Sub CreateWordDocumentWithTableFormat(ByVal strFaxAuth As String)
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        Dim dtSQL As New DataTable
        Dim outFile As String = String.Empty
        Dim directoryName As String = String.Empty
        Dim oWord As Microsoft.Office.Interop.Word.Application
        Dim oDoc As Microsoft.Office.Interop.Word.Document
        Dim clsFrmmain As New ClsFrmMainLoad

        Try
            MySQL.OpenConnection()
            Dim SQLobj As New SqlCommand("MY_WordReport")
            Dim Str_Date As String = String.Empty
            SQLobj.Parameters.AddWithValue("@tablename", "Dry")
            SQLobj.Parameters.AddWithValue("@ColumnName1 ", "FaxAuth")
            SQLobj.Parameters.AddWithValue("@Value", strFaxAuth)
            SQLobj.CommandType = CommandType.StoredProcedure
            SQLobj.Connection = MySQL.GetConnection
            clsFrmmain.Populate(SQLobj)
            dtSQL = clsFrmmain.Data_Table

            If dtSQL.Rows.Count <> 0 Then

                Dim oTemplate As String = ReturnWordTemplatePath() '"C:nts\Fapy.doc"
                oWord = CreateObject("Word.Application")
                oWord.Visible = True
                oDoc = oWord.Documents.Add
                If IsNothing(oTemplate) = True Then
                    WordClose(oWord, oDoc)
                    Exit Sub
                End If
                oDoc = oWord.Documents.Open(oTemplate)
                If dtSQL.Rows(0)("Title").ToString Is DBNull.Value = False Then
                    oDoc.Content.Find.Execute(FindText:="TO:",
                                      ReplaceWith:="TO: " & dtSQL.Rows(0)("Title").ToString & " " & dtSQL.Rows(0)("PatientName").ToString, Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, MatchCase:=True)
                End If

                If dtSQL.Rows(0)("Attn").ToString Is DBNull.Value = False Then
                    oDoc.Content.Find.Execute(FindText:="ATTN:",
                    ReplaceWith:="ATTN: " & dtSQL.Rows(0)("Attn").ToString, Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll)
                End If
                If dtSQL.Rows(0)("FaxNumber").ToString Is DBNull.Value = False Then
                    oDoc.Content.Find.Execute(FindText:="FAX NUMBER:",
                   ReplaceWith:="FAX NUMBER: " & dtSQL.Rows(0)("FaxNumber").ToString, Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, MatchCase:=True)
                End If
                If dtSQL.Rows(0)("Date").ToString Is DBNull.Value = False Then
                    Str_Date = clsFrmmain.ConvertDate(dtSQL.Rows(0)("Date").ToString, False)
                    oDoc.Content.Find.Execute(FindText:="DATE:",
                    ReplaceWith:="DATE: " & Str_Date, Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceOne)
                End If


                If dtSQL.Rows(0)("Ref").ToString Is DBNull.Value = False Then
                    oDoc.Content.Find.Execute(FindText:="SENDER'S REFERENCE NUMBER:",
                   ReplaceWith:=" SENDER'S REFERENCE NUMBER:  " & dtSQL.Rows(0)("Ref").ToString, Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, MatchCase:=True)

                End If
                If dtSQL.Rows(0)("Comments").ToString Is DBNull.Value = False Then
                    oDoc.Content.Find.Execute(FindText:="NOTES/COMMENTS ",
                   ReplaceWith:="NOTES/COMMENTS:   " & dtSQL.Rows(0)("Comments").ToString, Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, MatchCase:=True)
                End If
                If dtSQL.Rows(0)("Date").ToString Is DBNull.Value = False Then
                    oDoc.Content.Find.Execute(FindText:="Date:",
                    ReplaceWith:="Date: " & Str_Date, Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceOne, MatchCase:=True)
                End If
                If dtSQL.Rows(0)("Time").ToString Is DBNull.Value = False Then
                    oDoc.Content.Find.Execute(FindText:="At:",
                   ReplaceWith:="At: " & clsFrmmain.ConvTime(dtSQL.Rows(0)("Time").ToString), Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, MatchCase:=True)
                End If
                If dtSQL.Rows(0)("Comments").ToString Is DBNull.Value = False Then
                    oDoc.Content.Find.Execute(FindText:="For:",
                   ReplaceWith:="For: " & dtSQL.Rows(0)("Comments").ToString, Replace:=Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll, MatchCase:=True)

                End If
                Dim str_DateToday As String
                str_DateToday = clsFrmmain.ConvertDate(Date.Now, False)

                outFile = System.IO.Path.GetFileNameWithoutExtension(oTemplate)
                outFile = outFile + str_DateToday
                directoryName = System.IO.Path.GetDirectoryName(oTemplate)
                oDoc.SaveAs2(directoryName & "\" & outFile & ".doc")
                WordClose(oWord, oDoc)
            Else
                clsFrmmain.FormatMessage(18, "", strFaxAuth, " Word Letter Information")
            End If
              Catch ex As Exception
            MessageBox.Show(ex.ToString)
            Exit Sub

        End Try
oDoc = oWord.Documents.Open(oTemplate)
                If dtSQL.Rows(0)("Title").ToString Is DBNull.Value = False Then.....


You might want to check that oDoc is actually opened before attempting to use it.
Avatar of RIAS

ASKER

yes, it is open
oDoc = oWord.Documents.Open(oTemplate)
MsgBox.Show("wait for checking")
                If dtSQL.Rows(0)("Title").ToString Is DBNull.Value = False Then.....

Humour me please.  Try something like the above (not quite certain what the vb.net for showing a message box is of the top of my head) to make certain your code waits until the template is loaded into word.  Then click the messagebox away so your code continues.
Avatar of RIAS

ASKER

Andy,
That worked mate! But now how do I replace the messagebox?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RIAS

ASKER

Cheers mate for all your help,really appreciated!