Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Auto email an attachment

Posted on 2004-10-13
12
Medium Priority
?
340 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Accepted Solution

by:
jimbobmcgee earned 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

636 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