RIAS
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
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
Which line resulted in the error ?
ASKER
If dtSQL.Rows(0)("Attn").ToSt ring Is DBNull.Value = False Then
oDoc.Content.Find.Execute( FindText:= "ATTN:",
ReplaceWith:="ATTN: " & dtSQL.Rows(0)("Attn").ToSt ring, Replace:=Microsoft.Office. Interop.Wo rd.WdRepla ce.wdRepla ceAll, MatchCase:=True)
it is this line in there ;dont know whether it is the matchcase causing it?
oDoc.Content.Find.Execute(
ReplaceWith:="ATTN: " & dtSQL.Rows(0)("Attn").ToSt
it is this line in there ;dont know whether it is the matchcase causing it?
ASKER
looks like if it is not able to find FindText:="ATTN:", its giving an error.
Any workaround for this?
Any workaround for this?
Hmmm. That line giving the problem isn't in the code you posted. Please post the correct code.
ASKER
Public Sub CreateWordDocumentWithTabl eFormat(By Val 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.W ord.Applic ation
Dim oDoc As Microsoft.Office.Interop.W ord.Docume nt
Dim clsFrmmain As New ClsFrmMainLoad
Try
MySQL.OpenConnection()
Dim SQLobj As New SqlCommand("MY_WordReport" )
Dim Str_Date As String = String.Empty
SQLobj.Parameters.AddWithV alue("@tab lename", "Dry")
SQLobj.Parameters.AddWithV alue("@Col umnName1 ", "FaxAuth")
SQLobj.Parameters.AddWithV alue("@Val ue", strFaxAuth)
SQLobj.CommandType = CommandType.StoredProcedur e
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.Applica tion")
oWord.Visible = True
oDoc = oWord.Documents.Add
If IsNothing(oTemplate) = True Then
WordClose(oWord, oDoc)
Exit Sub
End If
oDoc = oWord.Documents.Open(oTemp late)
If dtSQL.Rows(0)("Title").ToS tring Is DBNull.Value = False Then
oDoc.Content.Find.Execute( FindText:= "TO:",
ReplaceWith:="TO: " & dtSQL.Rows(0)("Title").ToS tring & " " & dtSQL.Rows(0)("PatientName ").ToStrin g, Replace:=Microsoft.Office. Interop.Wo rd.WdRepla ce.wdRepla ceAll, MatchCase:=True)
End If
If dtSQL.Rows(0)("Attn").ToSt ring Is DBNull.Value = False Then
oDoc.Content.Find.Execute( FindText:= "ATTN:",
ReplaceWith:="ATTN: " & dtSQL.Rows(0)("Attn").ToSt ring, Replace:=Microsoft.Office. Interop.Wo rd.WdRepla ce.wdRepla ceAll)
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.Wo rd.WdRepla ce.wdRepla ceAll, MatchCase:=True)
End If
If dtSQL.Rows(0)("Date").ToSt ring Is DBNull.Value = False Then
Str_Date = clsFrmmain.ConvertDate(dtS QL.Rows(0) ("Date").T oString, False)
oDoc.Content.Find.Execute( FindText:= "DATE:",
ReplaceWith:="DATE: " & Str_Date, Replace:=Microsoft.Office. Interop.Wo rd.WdRepla ce.wdRepla ceOne)
End If
If dtSQL.Rows(0)("Ref").ToStr ing Is DBNull.Value = False Then
oDoc.Content.Find.Execute( FindText:= "SENDER'S REFERENCE NUMBER:",
ReplaceWith:=" SENDER'S REFERENCE NUMBER: " & dtSQL.Rows(0)("Ref").ToStr ing, Replace:=Microsoft.Office. Interop.Wo rd.WdRepla ce.wdRepla ceAll, MatchCase:=True)
End If
If dtSQL.Rows(0)("Comments"). ToString Is DBNull.Value = False Then
oDoc.Content.Find.Execute( FindText:= "NOTES/COM MENTS ",
ReplaceWith:="NOTES/COMMEN TS: " & dtSQL.Rows(0)("Comments"). ToString, Replace:=Microsoft.Office. Interop.Wo rd.WdRepla ce.wdRepla ceAll, MatchCase:=True)
End If
If dtSQL.Rows(0)("Date").ToSt ring Is DBNull.Value = False Then
oDoc.Content.Find.Execute( FindText:= "Date:",
ReplaceWith:="Date: " & Str_Date, Replace:=Microsoft.Office. Interop.Wo rd.WdRepla ce.wdRepla ceOne, MatchCase:=True)
End If
If dtSQL.Rows(0)("Time").ToSt ring Is DBNull.Value = False Then
oDoc.Content.Find.Execute( FindText:= "At:",
ReplaceWith:="At: " & clsFrmmain.ConvTime(dtSQL. Rows(0)("T ime").ToSt ring), Replace:=Microsoft.Office. Interop.Wo rd.WdRepla ce.wdRepla ceAll, 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.Wo rd.WdRepla ce.wdRepla ceAll, MatchCase:=True)
End If
Dim str_DateToday As String
str_DateToday = clsFrmmain.ConvertDate(Dat e.Now, False)
outFile = System.IO.Path.GetFileName WithoutExt ension(oTe mplate)
outFile = outFile + str_DateToday
directoryName = System.IO.Path.GetDirector yName(oTem plate)
oDoc.SaveAs2(directoryName & "\" & outFile & ".doc")
WordClose(oWord, oDoc)
Else
clsFrmmain.FormatMessage(1 8, "", strFaxAuth, " Word Letter Information")
End If
Catch ex As Exception
MessageBox.Show(ex.ToStrin g)
Exit Sub
End Try
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.W
Dim oDoc As Microsoft.Office.Interop.W
Dim clsFrmmain As New ClsFrmMainLoad
Try
MySQL.OpenConnection()
Dim SQLobj As New SqlCommand("MY_WordReport"
Dim Str_Date As String = String.Empty
SQLobj.Parameters.AddWithV
SQLobj.Parameters.AddWithV
SQLobj.Parameters.AddWithV
SQLobj.CommandType = CommandType.StoredProcedur
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.Applica
oWord.Visible = True
oDoc = oWord.Documents.Add
If IsNothing(oTemplate) = True Then
WordClose(oWord, oDoc)
Exit Sub
End If
oDoc = oWord.Documents.Open(oTemp
If dtSQL.Rows(0)("Title").ToS
oDoc.Content.Find.Execute(
ReplaceWith:="TO: " & dtSQL.Rows(0)("Title").ToS
End If
If dtSQL.Rows(0)("Attn").ToSt
oDoc.Content.Find.Execute(
ReplaceWith:="ATTN: " & dtSQL.Rows(0)("Attn").ToSt
End If
If dtSQL.Rows(0)("FaxNumber")
oDoc.Content.Find.Execute(
ReplaceWith:="FAX NUMBER: " & dtSQL.Rows(0)("FaxNumber")
End If
If dtSQL.Rows(0)("Date").ToSt
Str_Date = clsFrmmain.ConvertDate(dtS
oDoc.Content.Find.Execute(
ReplaceWith:="DATE: " & Str_Date, Replace:=Microsoft.Office.
End If
If dtSQL.Rows(0)("Ref").ToStr
oDoc.Content.Find.Execute(
ReplaceWith:=" SENDER'S REFERENCE NUMBER: " & dtSQL.Rows(0)("Ref").ToStr
End If
If dtSQL.Rows(0)("Comments").
oDoc.Content.Find.Execute(
ReplaceWith:="NOTES/COMMEN
End If
If dtSQL.Rows(0)("Date").ToSt
oDoc.Content.Find.Execute(
ReplaceWith:="Date: " & Str_Date, Replace:=Microsoft.Office.
End If
If dtSQL.Rows(0)("Time").ToSt
oDoc.Content.Find.Execute(
ReplaceWith:="At: " & clsFrmmain.ConvTime(dtSQL.
End If
If dtSQL.Rows(0)("Comments").
oDoc.Content.Find.Execute(
ReplaceWith:="For: " & dtSQL.Rows(0)("Comments").
End If
Dim str_DateToday As String
str_DateToday = clsFrmmain.ConvertDate(Dat
outFile = System.IO.Path.GetFileName
outFile = outFile + str_DateToday
directoryName = System.IO.Path.GetDirector
oDoc.SaveAs2(directoryName
WordClose(oWord, oDoc)
Else
clsFrmmain.FormatMessage(1
End If
Catch ex As Exception
MessageBox.Show(ex.ToStrin
Exit Sub
End Try
oDoc = oWord.Documents.Open(oTemp late)
If dtSQL.Rows(0)("Title").ToS tring Is DBNull.Value = False Then.....
You might want to check that oDoc is actually opened before attempting to use it.
If dtSQL.Rows(0)("Title").ToS
You might want to check that oDoc is actually opened before attempting to use it.
ASKER
yes, it is open
oDoc = oWord.Documents.Open(oTemp late)
MsgBox.Show("wait for checking")
If dtSQL.Rows(0)("Title").ToS tring 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.
MsgBox.Show("wait for checking")
If dtSQL.Rows(0)("Title").ToS
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.
ASKER
Andy,
That worked mate! But now how do I replace the messagebox?
That worked mate! But now how do I replace the messagebox?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cheers mate for all your help,really appreciated!