Solved

Auto email an attachment

Posted on 2004-10-13
12
329 Views
Last Modified: 2012-06-27
To the VB Expert

I have what i believe to be a small problem.

I have created a small Utilities that collect Numbers to send via e-mail. It goes as followed

1.- the utility collect info on some text files then create a vbscript:

VB Script sample:
---------------------------------------------
Option Explicit
Dim Row, XL, WshShell, FileSystem, RegularExpression, Dummy, TheNVFile, TheLine
Dim Whacks, WhacksFound, WhacksPattern,  Flag, Location
Dim TheDate, Filename, Book
Const ForReading = 1
Row = 2
Set XL = WScript.CreateObject("Excel.Application")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Set RegularExpression = New RegExp
Dummy = WshShell.Popup ("Collecting info.  Please Wait...",1,"LMC Flash Utility",64)
Call BuildSpreadSheet()
Call SaveSpreadSheet()
Wscript.Quit
Sub BuildSpreadSheet()
   Set Book = XL.WorkBooks.Add
   XL.Columns(1).ColumnWidth = 12
   XL.Columns(2).ColumnWidth = 12
   XL.Columns(3).ColumnWidth = 12
   XL.Columns(4).Columnwidth = 12
   XL.Columns(5).Columnwidth = 12
   XL.Columns(6).Columnwidth = 12
   XL.Columns(7).Columnwidth = 12
   XL.Columns(8).Columnwidth = 12
   XL.Cells(1, 1).Value = "Location"
   XL.Cells(1, 2).Value = "Count"
   XL.Cells(1, 3).Value = "CSI"
   XL.Cells(1, 4).Value = "Man Hours"
   XL.Cells(1, 5).Value = "V/Hour"
   XL.Cells(1, 6).Value = "Total Disc"
   XL.Cells(1, 7).Value = "AVG Disc"
   XL.Cells(1, 8).Value = "Report Time"
   XL.Cells(3, 1).Value = "SITE0001"
   XL.Cells(3, 2).Value = "45"
   XL.Cells(3, 3).Value = "$65.78"
   XL.Cells(3, 4).Value = "35"
   XL.Cells(3, 5).Value = "12"
   XL.Cells(3, 6).Value = "$300.60"
   XL.Cells(3, 7).Value = "$3.05"
   XL.Cells(3, 8).Value = "9:00 PM"

You get the idea similar numbers up to row 22

XL.Range("A1:A25").Select
   XL.Selection.Font.Bold = True
   XL.Selection.Font.Size = 10
   XL.Cells(26, 1).Value = "Total"
   XL.Cells(26, 2).Value = "=SUM(B3:B24)"
XL.Cells(26,3).Value = "=(B3*C3+B4*C4+B5*C5+B6*C6+B7*C7+B8*C8+B9*C9+B10*C10+B11*C11+B12*C12+B13*C13+B14*C14+B15*C15+B16*C16+B17*C17+B18*C18+B19*C19+B20*C20+B21*C21+B22*C22+B23*C23+B24*C24)/B26"
   XL.Cells(26, 4).Value = "=SUM(D3:D24)"
   XL.Cells(26, 5).Value = "=SUM(E3:E24)"
   XL.Cells(26, 6).Value = "=SUM(f3:f24)"
   XL.Cells(26, 7).Value = "=SUM(g3:g24)"
   XL.Range("A1:h1").Select
   XL.Selection.Font.Bold = True
   XL.Selection.Font.Size = 10
   XL.Range("A26:h26").Select
   XL.Selection.Font.Bold = True
   XL.Selection.Font.Size = 10
End Sub
Sub SaveSpreadSheet()
   TheDate = Date
   TheDate = Replace(TheDate, "/", "-")
FileName = "D:\Report.xls"
          Book.SaveAs(FileName)
End Sub

2.- the utility call the VB script.


This is the problems that I am having:

 

I want to overwrite any existing reports.xls without any prompt.

I need to automatically e-mail the file Report.xls via SMTP or Exchange.


I am not a VB expert. So please try to make your answer explicit. As a matter of fact it took me weeks to make the above.
 
Thanks in advance for your help


0
Comment
Question by:vico1
  • 8
  • 3
12 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 12299728
For emailing you can use this free component:
http://www.freevbcode.com/ShowCode.Asp?ID=109
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12318084
A simple way to remove the prompt is to ensure there is no file before you save, by deleting the file:

     ...
     FileName = "D:\Report.xls"
     If FileSystem.FileExists(FileName) Then FileSystem.DeleteFile(FileName)
     Book.SaveAs(FileName)
     ...


If you can access an Outlook application class, like you do with Excel, you might be able to send your mail via Outlook:

    Dim myOL, myMail, ShowMailBeforeSend
   
    ShowMailBeforeSend = True    

    Set myOL = CreateObject("Outlook.Application")
    Set myMail = myOL.CreateItem(olMailItem)
   
    myMail.SenderName = "Automated emailer"
    myMail.To = "somebody@somedomain.com"
    myMail.Subject = "Report"
    myMail.Attachments.Add (Filename)
   
    If ShowMailBeforeSend = True Then
        myMail.display
    Else
        myMail.Send
    End If

HTH

J.
0
 
LVL 15

Author Comment

by:vico1
ID: 12321160
Thanks Jimbobmcgee I will try and let you know.
0
 
LVL 15

Author Comment

by:vico1
ID: 12337795
Thanks Jimbobmcgee

     It didn't work It gave me an error on line 5, Char 8
it also said that the file is read only, which I check but is not.
 
0
 
LVL 15

Author Comment

by:vico1
ID: 12388917
Jimbobmcgee

The prompt suggestion works. However I am still not able to send the email.

Mail be you forgot to add something or I am doing something wrong. please help.
0
 
LVL 15

Author Comment

by:vico1
ID: 12416583
I have found a script that does what i needed. However I cannot add an attachment to it:

This is the script:

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "monitor@domain.com"
objEmail.To = "user@domain.com"
objEmail.Subject = "Email test"
objEmail.Textbody = "How do I attach a file to this email."
objEmail.Send


How do I attach a file like "report.txt" to this script

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 500 total points
ID: 12417052
If you are using CDO, try:

          objEmail.AddAttachment("report.txt")

As for why the other mail did not send, I have revised my Outlook-based code to the below.  This works fine for me:

    Sub sendmail()
       
        Dim myOL, myMail, ShowMailBeforeSend
       
        ShowMailBeforeSend = True
   
        Set myOL = CreateObject("Outlook.Application")
        Set myMail = myOL.CreateItem(olMailItem)
       
        'myMail.SenderName = "Automated emailer"   <-- COMMENT OR REMOVE THIS LINE...
                                                                                 ...YOU CANNOT SET THIS PROPERTY
                                                                                 ...MY ERROR!!
        myMail.To = "somebody@somedomain.com"
        myMail.Subject = "Report"
        myMail.Attachments.Add (FileName)
       
        If ShowMailBeforeSend = True Then
            myMail.Display
        Else
            myMail.Send
        End If
   
    End Sub

Make sure you set the Filename in the Attachments.Add line...

J.
0
 
LVL 15

Author Comment

by:vico1
ID: 12417336
I would like to use CDO. However by adding : objEmail.AddAttachment("report.txt")

I get the error:

Line:    6
Char:   1
error:   The specified protocol is unknown
code:    800C00d
source:  CDO.Message.1

Any Suggestion?
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12417837
Try using the full path instead of just "report.txt", e.g. "c:\report.txt".

See http://systemwebmail.com/faq/4.3.7.aspx for details...

J.
0
 
LVL 15

Author Comment

by:vico1
ID: 12417884
It worked Thank you.
0
 
LVL 15

Author Comment

by:vico1
ID: 12417900
If I wanted to write those scripts to an existing excel file how would I do That?

Note: either way you deserve the 500
0
 
LVL 15

Author Comment

by:vico1
ID: 12422252
Never mind I got it
0

Featured Post

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

Join & Write a Comment

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

706 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

15 Experts available now in Live!

Get 1:1 Help Now