Solved

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

Posted on 2016-07-22
11
45 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 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