Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default VBA code to modify Excel spreadsheet and then email it

Can someone please help me find the problem with this VBA code? It seemed to
run ok the first time through but then when I tested the second time I got an
error at the indicated place in the code. Thanks in advance.
Glenn

Dim xlsApp As Excel.Application
Dim olApp As Outlook.Application
Dim olMessage As Object
Dim olRecipient As Recipient

Set xlsApp = New Excel.Application
xlsApp.Workbooks.Open Filename:=[Full path to Excel file]
Set olApp = CreateObject("Outlook.Application")

Set olMessage = olApp.CreateItem(olMailItem)
olMessage.Subject = "Subject goes here"

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''
' This is where the error occurs after the second time through the code.
' Error #91 - Object variable or With block variable not set.
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''
xlsApp.Range("E4").Select
ActiveCell.FormulaR1C1 = "January"

xlsApp.Range("F4").Select
ActiveCell.FormulaR1C1 = DatePart("yyyy", Now)

strEmailRecipient = "
Set olRecipient = olMessage.Recipients.Add(strEmailRecipient)

' Not sure why I have a second reference to the subject line, either
' I think I borrowed the code from two different sources
strSubject = "Subject goes here"
xlsApp.ActiveWorkbook.SendMail olRecipient, strSubject

xlsApp.ActiveWorkbook.Close saveChanges:=xlDoNotSaveChanges
Set xlsApp = Nothing


--
Glenn
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default VBA code to modify Excel spreadsheet and then email it

add
DIM wb as Object
DIM ws as Object
then change this
xlsApp.Workbooks.Open Filename:=[Full path to Excel file]
to
SET wb = xlsApp.Workbooks.Open Filename:=[Full path to Excel file]
SET ws = wb.Activesheet

change this
xlsApp.Range("E4").Select
ActiveCell.FormulaR1C1 = "January"

to
ws.Range("E4") = "January"

change this
xlsApp.Range("F4").Select
ActiveCell.FormulaR1C1 = DatePart("yyyy", Now)
to

ws.Range("F4") = YEAR(now)


change these

xlsApp.ActiveWorkbook.SendMail olRecipient, strSubject
xlsApp.ActiveWorkbook.Close saveChanges:=xlDoNotSaveChanges

to

wb.SendMail olRecipient, strSubject
wb.Close FALSE
xlsApp.QUIT




"Glenn Suggs" wrote:

Can someone please help me find the problem with this VBA code? It seemed to
run ok the first time through but then when I tested the second time I got an
error at the indicated place in the code. Thanks in advance.
Glenn

Dim xlsApp As Excel.Application
Dim olApp As Outlook.Application
Dim olMessage As Object
Dim olRecipient As Recipient

Set xlsApp = New Excel.Application
xlsApp.Workbooks.Open Filename:=[Full path to Excel file]
Set olApp = CreateObject("Outlook.Application")

Set olMessage = olApp.CreateItem(olMailItem)
olMessage.Subject = "Subject goes here"

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''
' This is where the error occurs after the second time through the code.
' Error #91 - Object variable or With block variable not set.
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''
xlsApp.Range("E4").Select
ActiveCell.FormulaR1C1 = "January"

xlsApp.Range("F4").Select
ActiveCell.FormulaR1C1 = DatePart("yyyy", Now)

strEmailRecipient = "
Set olRecipient = olMessage.Recipients.Add(strEmailRecipient)

' Not sure why I have a second reference to the subject line, either
' I think I borrowed the code from two different sources
strSubject = "Subject goes here"
xlsApp.ActiveWorkbook.SendMail olRecipient, strSubject

xlsApp.ActiveWorkbook.Close saveChanges:=xlDoNotSaveChanges
Set xlsApp = Nothing


--
Glenn

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default VBA code to modify Excel spreadsheet and then email it

Thanks Patrick,
That worked great.
--
Glenn


"Patrick Molloy" wrote:

add
DIM wb as Object
DIM ws as Object
then change this
xlsApp.Workbooks.Open Filename:=[Full path to Excel file]
to
SET wb = xlsApp.Workbooks.Open Filename:=[Full path to Excel file]
SET ws = wb.Activesheet

change this
xlsApp.Range("E4").Select
ActiveCell.FormulaR1C1 = "January"

to
ws.Range("E4") = "January"

change this
xlsApp.Range("F4").Select
ActiveCell.FormulaR1C1 = DatePart("yyyy", Now)
to

ws.Range("F4") = YEAR(now)


change these

xlsApp.ActiveWorkbook.SendMail olRecipient, strSubject
xlsApp.ActiveWorkbook.Close saveChanges:=xlDoNotSaveChanges

to

wb.SendMail olRecipient, strSubject
wb.Close FALSE
xlsApp.QUIT




"Glenn Suggs" wrote:

Can someone please help me find the problem with this VBA code? It seemed to
run ok the first time through but then when I tested the second time I got an
error at the indicated place in the code. Thanks in advance.
Glenn

Dim xlsApp As Excel.Application
Dim olApp As Outlook.Application
Dim olMessage As Object
Dim olRecipient As Recipient

Set xlsApp = New Excel.Application
xlsApp.Workbooks.Open Filename:=[Full path to Excel file]
Set olApp = CreateObject("Outlook.Application")

Set olMessage = olApp.CreateItem(olMailItem)
olMessage.Subject = "Subject goes here"

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''
' This is where the error occurs after the second time through the code.
' Error #91 - Object variable or With block variable not set.
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''
xlsApp.Range("E4").Select
ActiveCell.FormulaR1C1 = "January"

xlsApp.Range("F4").Select
ActiveCell.FormulaR1C1 = DatePart("yyyy", Now)

strEmailRecipient = "
Set olRecipient = olMessage.Recipients.Add(strEmailRecipient)

' Not sure why I have a second reference to the subject line, either
' I think I borrowed the code from two different sources
strSubject = "Subject goes here"
xlsApp.ActiveWorkbook.SendMail olRecipient, strSubject

xlsApp.ActiveWorkbook.Close saveChanges:=xlDoNotSaveChanges
Set xlsApp = Nothing


--
Glenn

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Email excel spreadsheet - embedded in email Lisa Excel Discussion (Misc queries) 6 September 25th 08 04:17 PM
opened excel file from email & clicked save after modify . Where i bravorimes Excel Discussion (Misc queries) 2 July 27th 08 06:45 PM
VB.NET 2005: Open an Excel Spreadsheet, modify it and then save it MaxGruven Excel Programming 0 February 28th 08 08:46 PM
Modify Macro Code Depending on Excel Version John Taylor Excel Discussion (Misc queries) 11 February 26th 07 04:19 AM
Modify ADD ROWS Code - Excel 2003 [email protected] Excel Programming 4 August 17th 06 08:59 PM


All times are GMT +1. The time now is 04:54 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"