Solved

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

Posted on 2016-07-22
11
31 Views
Last Modified: 2016-07-22
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

0
Comment
Question by:RIAS
  • 6
  • 5
11 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41724225
Which line resulted in the error ?
0
 

Author Comment

by:RIAS
ID: 41724263
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?
0
 

Author Comment

by:RIAS
ID: 41724281
looks like if it is not able to find FindText:="ATTN:", its giving an error.
Any workaround for this?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41724427
Hmmm.  That line giving the problem isn't in the code you posted.  Please post the correct code.
0
 

Author Comment

by:RIAS
ID: 41724452
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41724458
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.
0
 

Author Comment

by:RIAS
ID: 41724459
yes, it is open
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41724464
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.
0
 

Author Comment

by:RIAS
ID: 41724474
Andy,
That worked mate! But now how do I replace the messagebox?
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 41724513
Offhand I don't know.
Maybe there is something that will return a boolean value if the word object is busy - keep testing at one second intervals until available.

You could try using something like Sleep(1000) to make the thread wait for 1 second and hope.
0
 

Author Closing Comment

by:RIAS
ID: 41724522
Cheers mate for all your help,really appreciated!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now