Solved

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

Posted on 2016-07-22
11
81 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

696 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