dkilby
asked on
VB.NET app works on development not on other machines
I have created a small app, that creates a report in Excel then emails the report out. It works perfectly fine on the machine I developed it on (Win XP Pro). I have used a setup program in the project to build the app and ran the setup to which it worked fine. When i tried to install it on another machine(win 2000 pro) The app runs fine and creates the report but then and errror comes up near the end. It seems to either be when it trys to save the excel spreadsheet or when it tries to email it. Here is my code and the error, can someone please see where I am going wrong. Thanks
System.NullReferenceExcept ion: Object reference not set to an instance of an object.
at Excel.WorkbookClass.SaveAs (Object Filename, Object FileFormat, Object Password, Object WriteResPassword, Object ReadOnlyRecommended, Object CreateBackup, XlSaveAsAccessMode AccessMode, Object ConflictResolution, Object AddToMru, Object TextCodepage, Object TextVisualLayout, Object Local)
at Daily_Attendance_Report.Fo rm1.Form1_ Load(Objec t sender, EventArgs e)
at System.Windows.Forms.Form. OnLoad(Eve ntArgs e)
at System.Windows.Forms.Form. OnCreateCo ntrol()
at System.Windows.Forms.Contr ol.CreateC ontrol(Boo lean fIgnoreVisible)
at System.Windows.Forms.Contr ol.CreateC ontrol()
at System.Windows.Forms.Contr ol.WmShowW indow(Mess age& m)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Scrol lableContr ol.WndProc (Message& m)
at System.Windows.Forms.Conta inerContro l.WndProc( Message& m)
at System.Windows.Forms.Form. WmShowWind ow(Message & m)
at System.Windows.Forms.Form. WndProc(Me ssage& m)
at System.Windows.Forms.Contr olNativeWi ndow.OnMes sage(Messa ge& m)
at System.Windows.Forms.Contr olNativeWi ndow.WndPr oc(Message & m)
at System.Windows.Forms.Nativ eWindow.Ca llback(Int Ptr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
xlApp.Application.Workbook s(1).SaveA s(filename )
xlApp.Application.Quit()
Call SendFile(filename, searchDate, reportTime)
End Sub
Public Sub SendFile(ByVal sFile As String, ByVal reportDate As Date, ByVal reportTime As DateTime)
Dim sFileLocation, sSubject, sBody, sRecipient, sCCrecipient As String
Dim appOutlook, mitItem, olMailItem As Object
sFileLocation = sFile
sSubject = "Daily Attendance Report " & reportDate & " At " & reportTime
sBody = "Please find requested document attached"
appOutlook = CreateObject("Outlook.Appl ication")
mitItem = appOutlook.CreateItem(olMa ilItem)
sRecipient = "test@test.com"
sCCrecipient = "test@test.com"
With mitItem
.to = sRecipient
.cc = sCCrecipient
.Subject = sSubject
.body = sBody
.Attachments.Add(sFileLoca tion)
.send()
End With
mitItem = Nothing
appOutlook = Nothing
Dim st As String
Dim x, cols As Integer
End Sub
System.NullReferenceExcept
at Excel.WorkbookClass.SaveAs
at Daily_Attendance_Report.Fo
at System.Windows.Forms.Form.
at System.Windows.Forms.Form.
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Scrol
at System.Windows.Forms.Conta
at System.Windows.Forms.Form.
at System.Windows.Forms.Form.
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
xlApp.Application.Workbook
xlApp.Application.Quit()
Call SendFile(filename, searchDate, reportTime)
End Sub
Public Sub SendFile(ByVal sFile As String, ByVal reportDate As Date, ByVal reportTime As DateTime)
Dim sFileLocation, sSubject, sBody, sRecipient, sCCrecipient As String
Dim appOutlook, mitItem, olMailItem As Object
sFileLocation = sFile
sSubject = "Daily Attendance Report " & reportDate & " At " & reportTime
sBody = "Please find requested document attached"
appOutlook = CreateObject("Outlook.Appl
mitItem = appOutlook.CreateItem(olMa
sRecipient = "test@test.com"
sCCrecipient = "test@test.com"
With mitItem
.to = sRecipient
.cc = sCCrecipient
.Subject = sSubject
.body = sBody
.Attachments.Add(sFileLoca
.send()
End With
mitItem = Nothing
appOutlook = Nothing
Dim st As String
Dim x, cols As Integer
End Sub
ASKER
Excel is installed on the other machine, I can see the report being generated no problem at all. the problem seems to happen when it is either trying to save the report or trying to email it, I am not sure which one.
if it's in the send action, it could be the security patch that's installed in outlook and when you try to automate a mail message it doesn't allow it resulting in an error
but that's just a guess, since you have it working on your development machine without problems
can you insert a try finally block to catch the error?
but that's just a guess, since you have it working on your development machine without problems
can you insert a try finally block to catch the error?
ASKER
I don't believe it is outlook, i have several other programs written with VB 6.0 that send emails and they all work no problem with sending the emails.
"can you insert a try finally block to catch the error?" What do you mean by this, I am new to VB .NET, and how do I do this?
Thanks
"can you insert a try finally block to catch the error?" What do you mean by this, I am new to VB .NET, and how do I do this?
Thanks
if you can install vs.net in that machine, you can see at which line vb break execution, if you cannot then try to add try-catch expression to your app and put a messagebox within the catch expression that show the line it stop, something like this:
Try
xlApp.Application.Workbook s(1).SaveA s(filename )
Catch err As Exception
MessageBox.Show("Save File")
End Try
xlApp.Application.Quit()
Try
Call SendFile(filename, searchDate, reportTime)
Catch err As Exception
MessageBox.Show("Send File")
End Try
....
so you can understand where the bug is...
the edge
Try
xlApp.Application.Workbook
Catch err As Exception
MessageBox.Show("Save File")
End Try
xlApp.Application.Quit()
Try
Call SendFile(filename, searchDate, reportTime)
Catch err As Exception
MessageBox.Show("Send File")
End Try
....
so you can understand where the bug is...
the edge
ASKER
Message Box with Save File popped up. So it seems to be the saving of the file.
so the problem maybe is the filename string or the permission rigths to create file.
try to add thi second messagebox:
Try
xlApp.Application.Workbook s(1).SaveA s(filename )
Catch err As Exception
MessageBox.Show(filename)
End Try
and write here the string that the messagebox show.
maybe the path is wrong...
the edge
try to add thi second messagebox:
Try
xlApp.Application.Workbook
Catch err As Exception
MessageBox.Show(filename)
End Try
and write here the string that the messagebox show.
maybe the path is wrong...
the edge
Is the version of Excel the same on the development machine and the other machine?
ASKER
I am not sure, it may not be. I think development is 2002 and other machine 2000.
ASKER
name of fileName = c:/attendReport/attendance _2003_10_2 7_2pm.xls
i've tried to reproduce the error in my machine and i've discovered that the execution stop because the folder "attendReport" doesn't exist in my HD. i've created that folder, then tried again and it worked fine, the file was created.
check the existence of that folder in your win200 machine.
the edge
check the existence of that folder in your win200 machine.
the edge
This could be the source of your problem. On the development machine, you add a reference to Excel. The version of the reference is 10. However, on the other machine, the version is 9. Your app is looking for 10 but can only find 9.
When referencing Excel, I always compile on another machine which has a previous version of Excel installed (2000). That way my app will work with 2000 and newer. That is my 'work around'. I'm sure there is an easier way, but I have never had the time to investigate.
When referencing Excel, I always compile on another machine which has a previous version of Excel installed (2000). That way my app will work with 2000 and newer. That is my 'work around'. I'm sure there is an easier way, but I have never had the time to investigate.
dkilbi note that when you declare variables in sendfile sub:
Dim sFileLocation, sSubject, sBody, sRecipient, sCCrecipient As String
Dim appOutlook, mitItem, olMailItem As Object
only sCCrecipient is declared as string and olMailItem as object, all others variable as declared with not specified type.
the rigth variables declaration is:
Dim sFileLocation As String, sSubject As String, sBody As String, sRecipient As String, sCCrecipient As String
Dim appOutlook As Object, mitItem As Object, olMailItem As Object
the edge
Dim sFileLocation, sSubject, sBody, sRecipient, sCCrecipient As String
Dim appOutlook, mitItem, olMailItem As Object
only sCCrecipient is declared as string and olMailItem as object, all others variable as declared with not specified type.
the rigth variables declaration is:
Dim sFileLocation As String, sSubject As String, sBody As String, sRecipient As String, sCCrecipient As String
Dim appOutlook As Object, mitItem As Object, olMailItem As Object
the edge
ASKER
okay, I ran the app again but removed the line:
xlApp.Application.Workbook s(1).SaveA s(filename )
I had also tired this :
xlSheet.SaveAs(filename)
The app failed both times at those lines, I than removed those lines and saved and put an excel spreadsheet in the attendReport folder with the name of the file that was usually being saved. When I ran the app, it generated the report just fine, and then sent the one I had put in the folder just fine. The problem with the code comes down to the one line, I have a folder with the correct name, the report generates completely fine so surely if it was a problem with the versions of excel it wouldn't complete it. I can not compile the app on machine with Excel 2000, is there anything else I can do to save the excel spreadsheet.
Thanks
xlApp.Application.Workbook
I had also tired this :
xlSheet.SaveAs(filename)
The app failed both times at those lines, I than removed those lines and saved and put an excel spreadsheet in the attendReport folder with the name of the file that was usually being saved. When I ran the app, it generated the report just fine, and then sent the one I had put in the folder just fine. The problem with the code comes down to the one line, I have a folder with the correct name, the report generates completely fine so surely if it was a problem with the versions of excel it wouldn't complete it. I can not compile the app on machine with Excel 2000, is there anything else I can do to save the excel spreadsheet.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I copied over the excel9.olb file and try referencing to it, but it keeps using the 10.0, if i register the ms09.dll will that mess things up.
I tried the app on a different win 2000 machine with Excel 2002 and it worked so this seems to be the issue, now I just need to figure out how to fix it.
thanks
I tried the app on a different win 2000 machine with Excel 2002 and it worked so this seems to be the issue, now I just need to figure out how to fix it.
thanks
did you remove the reference to Microsoft Excel 10 Object library before add the reference to excel9.olb?
ASKER
Yes, it would not allow me to reference excel9.olb until I removed the reference to 10.0
It did not show up in the window where the 10.0 was I had to browse and select the file excel9.olb.
It did not show up in the window where the 10.0 was I had to browse and select the file excel9.olb.
when you install the app in win2000 the installer copy also the excel10.old and the mso10.dll files (i suppose they have this name in office 2002)?
if the answer is 'no' try to copy these files manually in the app path and, if it needs, register mso10.dll.
if the answer is 'no' try to copy these files manually in the app path and, if it needs, register mso10.dll.
also take a look at:
http://www-level3.experts-exchange.com/questions/20705091/Changing-references-by-code.html
maybe there is the answer
http://www-level3.experts-exchange.com/questions/20705091/Changing-references-by-code.html
maybe there is the answer
ASKER
I think i figured out a way. I changed the save to :
xlApp.Application.Workbook s(1).SaveC opyAs(file name)
and it worked. Thanks for all your help.
xlApp.Application.Workbook
and it worked. Thanks for all your help.
dkilby thanks for the excellent solution. I found that
oExcelWorkbook.Close(true, excelFilen ame,Type.M issing);
also works.
oExcelWorkbook.Close(true,
also works.
the edge