FAQ Home :
How do I delete an Excel Range Name. Delete Excel Name. Name define. ASP sample code for Excel Automation.
How do I delete an Excel Range Name. Delete Excel Name. Name define. ASP sample code for Excel Automation.

AnswerBelow is some sample code in .asp that allows you to send a server based excel spreadsheet that has an external lookup to a database. The master Exel workbook if pened would refresh the data based on a QueryTable that queries MSSQl server.
Often in corporate companies you may want to send a snapshot report once a month to a group of employees or mamangers etc. this code saves the auto master file and deletes any references to Names (Named Ranges) that the Query Table (MSquery) or Data filter have created. This way the user receives a non updating snapshot in time of the report.
Excel is excellent for ding this type of adhoc, template driven reporting and is easy to configure in IIS/ASP environments. Lotus Domino can also use this type of concept except in the example you could only use the Excel automation code in LotusScript as LotusScript does not know the .asp code. It is possible on Win32 OS that LotusScript can reference the COM object from .asp.
<%
dim sFileName
'sFileName = request.QueryString("filename")
sFileName = "thefile"
Dim objExcel
Dim objWorkBook

'Instaniate EXCEL.APPPLICATION OBJECT
Set objExcel = server.CreateObject("EXCEL.APPLICATION")
dim rootPath
rootPath = Request.ServerVariables( "Appl_Physical_Path" )
sPath = "excel_reports\"
sSavePath = "excel_reports\sent\"
sExtension = ".xls"

sTemplatePath = rootPath & sPath & sFileName & sExtension
sFullSavePath = rootPath & sSavePath & sFilename & "_" & getshortdate(now()) & sExtension

'response.write "<BR>sTemplatePath = " & sTemplatePath
'response.write "<BR>sFullSavePath=" & sFullSavePath
'Get a Workbook object to the returned Woorkbook object from the application.open method
Set objWorkBook = objExcel.Workbooks.Open(sTemplatePath)

'By deletng the names from the worksheet, we disconnect the querytable that was in the master template
dim objNames
set objnames = objworkBook.names

'Basic VBA code to do some printing and how how to reference objects
if objnames is nothing then
response.write "objnames is nothing"
Else
response.write "<BR>objnames.count = " & objnames.count
For r = 1 To objnames.Count
response.write "<BR>objnames(objnames.count).name = " & objnames(r).name
response.write "<BR>objnames(objnames.count).refersto = " & objnames(r).refersto
response.write "<BR>objnames(objnames.count).namelocal = " & objnames(r).namelocal
response.write "<BR>objnames(objnames.count).value = " & objnames(r).value
response.write "<BR>objnames(objnames.count).parent = " & objnames(r).parent
response.write "<BR>objnames(objnames.count).index = " & objnames(r).index
response.write "<BR>NameExists = " & NameExists(objnames(r).name)
next
end if

'Delete function - Deletes Defined Names from the Names Collection.
Function DeleteName(TheName)
response.write "<BR>Deleted: " & TheName
On Error Resume Next
objworkBook.Names(TheName).Delete
End function

'We cannot put this routine in the loop of names as when we delete the index is less by one and thus we get errors due to names not found.
for i = 1 to objnames.Count
DeleteName(objnames(1).name)
next

'Sample function using the Names Collection ie using an index see if the actual defined name exists, could be used to decide on a delete or not. Remeber
'When using VBA (OLE Automation from ASP oir EVel Lotus Notes you will get many erros from referencing objects and methods wrongly. They need to be caught.
'TIP:
'I often record my actions that I wish to perform, then manually convert the recroded code to Automation code.
'When using ASP and LotusScript you cannot use the native recorded VBA code. VB can however. There needs to be explicit declarations of all object references.

Function NameExists(TheName)
On Error Resume Next
NameExists = Len(objnames(TheName).Name) <> 0
End Function

objWorkBook.SaveCopyAs(sFullSavePath) ' We use save copy as otherwise we have to pass extra constants - real pain
objWorkBook.Close False
Set objWorkBook = Nothing
'Use this command to stop Excel being left resident in the server tasks list.
objExcel.Quit
Set objExcel = Nothing

'Send the email (does not work in LotusScript)
Dim emailbody
set eml_objMail = server.createobject("CDONTS.NewMail")
eml_objMail.From = "reports@domain.com"
eml_objMail.To = "group@domain.com" 'Send to eval@arc.com
eml_objMail.cc = "individual@domain.com"
eml_objMail.Subject = "Your Monthly Report"
emailbody = "Attached is your monthly report" & vbCRLf
emailbody = emailbody & "If you have any problems with this report please contact reports@domain.com or consult our Technical Support Site." & vbcrlf & vbcrlf
emailbody = emailbody & "Domain Auto Report Manager" & vbcrlf
emailbody = emailbody & "http://www.domain.com" & vbcrlf
eml_objMail.Body = emailbody
eml_objMail.AttachFile sFullSavePath
eml_objMail.Send
set eml_objMail = nothing

'Function
'Gets a formatted date (does not work on LotusScript)
Function getshortdate(aDate)
if adate <> "" then
getshortdate = Day(aDate) & "-" & MonthName(Month(aDate), 2) & "-" & Year(aDate)
Else
getshortdate = ""
End if
End Function
%>
Attachments sendexcelreport.asp (4295 bytes)
Applies to versions4.x; 5.x; 6.x
FAQ Provided BySteve C Robinson
Credit
 Forum Latest Entries
29/07/2010 Lotus Notes Forum - AppendDocLink to Doc in Db-A to from a Doc in Db-B    (Rus)
28/07/2010 Lotus Notes Forum - Get Document from another db and appenddoc link to a field in source    (Rus)
24/06/2010 Lotus Notes Forum - Lotus Notes 6.5 Programming Certification Test    (Charles)
21/06/2010 Lotus Notes Forum - Consult    (Rogers )
09/06/2010 Lotus Notes Forum - #error : Must specify type of OS ("DOS", "OS2", etc) on C command line    (Abhi)
31/05/2010 Lotus Notes Forum - Note mail error    (Anonymous)
28/05/2010 Lotus Notes Forum - lotus notes 8 calendars & email setup...    (Dermick Vaughn)
19/05/2010 Lotus Notes Forum - Stop sending delivery and read receipts    (Simon)
19/05/2010 Lotus Notes Forum - Folder Restore Tool v2.5 is released, try for ten databases for free    (Kim van den Berg)
19/05/2010 Lotus Notes Forum - Try our Archive Solution for free for ten databases (mail/applications/quickr places)    (Kim van den Berg)
14/05/2010 Lotus Notes Forum - Adobe fillable form submit Email button not working with lotus    (Anthony )
12/05/2010 Lotus Notes Forum - Recipients on archived mails differ from regular mail    (Borja)
03/05/2010 Lotus Notes Forum - Name change only partially works    (Anonymous)
02/05/2010 Lotus Notes Forum - How to edit value in MASTER from, and backupdate MAIN form??    (Julee)
22/04/2010 Lotus Notes Forum - Note 8 Alarms Not Working    (Tony D)
07/04/2010 Lotus Notes Forum - Lotus Notes Agent to send an e-mail every month    (Lorant)
31/03/2010 Lotus Notes Forum - Lotus Notes C++, LNText Item, Unicode    (Matt)
19/03/2010 Lotus Notes Forum - Searching for a date in a document collection    (Kris Mitchell)
16/03/2010 Lotus Notes Forum - Lotus CAPI --> CSharp    (HW)
12/03/2010 Lotus Notes Forum - Notes IMAP connection    (Charl)
 Latest Tips & Tricks
10/07/2009Terminal Services Tips & Tricks - How to kill terminal server sessions. Utilities for terminal services: qwinsta and rwinsta(Steven Charles Robinson)
10/07/2009.NET Tips & Tricks - The type or namespace name could not be found (are you missing a using directive or an assembly reference?)(SCRobinson)
10/07/2009Terminate Lotus Notes Processes, Error Tips & Tricks - Lotus Notes: An Error Was Encountered While Opening A Window(Steven Charles Robinson)
26/04/2007LotusScript Mail, SMTP, Spam, Junkmail Tips & Tricks - How to drag and drop spam into junk mail filter using LotusScript(Steven Charles Robinson)
31/10/2006Terminate Lotus Notes Processes, Error Tips & Tricks - Lotus Notes: An Error Was Encountered While Opening A Window(Steven Charles Robinson)
23/10/2006Errors, SMTP, Internet Sites, Configuration, Administration, Domino 7 Tips & Tricks - Domino authentication is not enabled in the smtp internet site document(Steven Charles Robinson)
20/10/2006Articles Tips & Tricks - Have you see our recent arcticle in "The View": Proven techniques for abstracting UI from data using XML, XSL and Domino.(Steven Charles Robinson)
26/09/2006.NET Tips & Tricks - If your looking for .NET tips visit http://www.codedotnet.net(S C Robinson)
25/09/2006Using Lotus Notes Tips & Tricks - Save the Window state of a Lotus Notes client on shutdown.(Steven Charles Robinson)
22/09/2006NET, Culture, Globalization Tips & Tricks - How to find out the current culture in a .NET application.(Steve C Robinson)
© 2003 notes411.com. All rights reserved. Disclaimer. site designed & developed by appsworks.com
Lotus Notes is a registered trademark of IBM. This site is not affiliated with IBM or Lotus.
 News

 Google Box
 Sponsor
ClearCase, Subversion, WebSphere Consulting
 Best in the Industry
Lotus Notes & Domino FAQ




File Attachment Icon
sendexcelreport.asp