Hi all,
with the help of various postings here on macros, I wrote a macro to email a report as text. Following is the code.
Sub Macro1()
Dim ObjMessage As CDO.Message
Dim doc As Document
Set doc = ActiveDocument
Dim docname, txtDocName, tempdoc As String
Dim i As Integer
Dim Rep As Report
doc.Save
'tempdoc = doc.Name & Format(Now, "MM-DD-YYYY hh\hmm")
'doc.SaveAs (tempdoc)
' txtDocName = Left(docname, (Len(docname) - 4)) + "_" + Format(Now, "MM-DD-YYYY hh\hmm") + ".txt"
' On Error Resume Next
For i = 1 To doc.Reports.Count
Set Rep = doc.Reports.Item(i)
Rep.ExportAsText ("C:\Reports\" & Rep.Name & Format(Now, "MM-DD-YYYY hh\hmm"))
Next i
On Error Resume Next
Call GeteMailInfo(doc)
Set ObjMessage = New CDO.Message
If Err.Number <> 0 Then WriteError (Now & " " & Err.Description)
ObjMessage.TextBody = email_textbody
ObjMessage.Subject = email_subject
ObjMessage.From = email_from
ObjMessage.To = email_to
On Error Resume Next
For i = 1 To doc.Reports.Count
ObjMessage.AddAttachment ("C:\Reports\" & Rep.Name & ".txt")
If Err.Number <> 0 Then
WriteError (Now & " " & Err.Description)
Else
WriteError (Now & " " & "C:\Reports\" & Rep.Name & ".txt" & " Added")
End If
Next i
On Error Resume Next
ObjMessage.Send
If Err.Number <> 0 Then
WriteError (Now & " " & Err.Description)
Else
WriteError (Now & " Message sent")
End If
On Error Resume Next
'Kill tempdoc
If Err.Number <> 0 Then
WriteError (Now & " " & Err.Description)
Else
WriteError (Now & " " & tempdoc & " deleted")
End If
End Sub
Public Sub GeteMailInfo(myDoc)
Dim MyVariable As busobj.DocumentVariable
Dim loop_counter
loop_counter = 1
Do While loop_counter <= myDoc.DocumentVariables.Count
Set MyVariable = myDoc.DocumentVariables.Item(loop_counter)
Select Case MyVariable.Name
Case "email_to"
email_to = MyVariable.Formula
Case "email_from"
email_from = MyVariable.Formula
Case "email_text"
email_textbody = MyVariable.Formula
Case "email_subject"
email_subject = MyVariable.Formula
End Select
loop_counter = loop_counter + 1
Loop
On Error Resume Next
End Sub
Public Sub WriteError(Err_Description As String)
'The error_text.txt file where the messages and potential error
'message are going to be saved.
Open "C:\Reports\error_text.txt" For Append As #1
Print #1, Err_Description
Close #1
End Sub
I got the following errors in my error log file for three different runs on full client.
Also when I look for help (by pressing F1, to see the parameters b’cos the first error in log says -file specified is not found ) on the function AddAttachment it gives me the following error:
Has anybody come across these errors before?
Also Can someone tell me what does the 2nd statement in the error log mean?
Thanks in advance for your help.
[/quote]
SGuda (BOB member since 2004-04-17)