-
Oct 23rd, 2013, 02:55 PM
#1
Thread Starter
Frenzied Member
Create a program to run on pc with or without excel?
Hello,
I am writing a program in VB.NET that uses excel in it but also want the program to run on a computer that does not have excel. Wouldn't it crash on the computer without excel? Below is how I reference Excel and import the data. It is just a small example for importing a few rows. This computer I wrote it on has Office installed. Is there any way to have this code in a program that runs on a computer without Excel? In case you ask, the alternate way will use another input file format such as Access or a text file.
Thanks!
Code:
Imports Microsoft.Office.Interop
Private Sub ImportExcel(ByVal strFileName As String)
Dim objXLApp As Excel.Application = Nothing
Dim objXLWb As Excel.Workbook
Dim objXLWs As Excel.Worksheet
'open the workbook, get a reference to the first worksheet
objXLApp = New Excel.Application
objXLApp.Workbooks.Open(strFileName)
objXLWb = objXLApp.Workbooks(1)
objXLWs = objXLWb.Worksheets(1)
For X = 2 To 10
Dim var1 As String = IIf(objXLWs.Range("A" & X).Value Is Nothing, "", objXLWs.Range("A" & X).Value)
Dim var2 As String = IIf(objXLWs.Range("B" & X).Value Is Nothing, "", objXLWs.Range("B" & X).Value)
Dim var3 As String = IIf(objXLWs.Range("C" & X).Value Is Nothing, "", objXLWs.Range("C" & X).Value)
'More code here to process data
Next
End Sub
-
Oct 23rd, 2013, 03:02 PM
#2
Re: Create a program to run on pc with or without excel?
Can't work. Your code does not have a means to handle the worksheet. It needs excel to do it. Importing an Access database table wont either. But a text file is possible. Do you mean you can have the source file as any of those?
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Oct 23rd, 2013, 03:03 PM
#3
Thread Starter
Frenzied Member
Re: Create a program to run on pc with or without excel?
I know my current code will not. So that is how I am asking how to use excel when needed and the app still not crash when it is not needed. I read about this somewhere before on how it is referenced.
-
Oct 23rd, 2013, 03:10 PM
#4
Frenzied Member
Re: Create a program to run on pc with or without excel?
The code you have is going to require excel on the users machine
What are you trying to accomplish with your program?
I believe if you are trying to take advantage of excel functionality in your program the user will need to have excel installed. With that said if you are just trying to create an excel type file you can do that if I am not mistaken
Here are a few links that might help you in meantime
http://stackoverflow.com/questions/5...y-having-excel
http://stackoverflow.com/questions/1...office-interop
-
Oct 23rd, 2013, 03:13 PM
#5
Thread Starter
Frenzied Member
Re: Create a program to run on pc with or without excel?
I just found what I think I read about before. I think I want to use late binding. What I want to do with my program is integrate data from different types such as excel, text, etc. If they want to load in a text file on a computer that does not have excel, I want the program to still run even with excel not installed. I'm not sure but I think I can determine if excel is available or not somehow and use late binding if it is and that's only if they select to use excel.
-
Oct 23rd, 2013, 03:19 PM
#6
Frenzied Member
Re: Create a program to run on pc with or without excel?
I think you are headed in the right direction
One of the links I provided I believe talks about late binding if not just Google there is plenty of info on how to do that
-
Oct 23rd, 2013, 03:20 PM
#7
Thread Starter
Frenzied Member
Re: Create a program to run on pc with or without excel?
I just found this example that uses late binding. So I bet if I run a program with this code it in on a machine without excel it will not crash. Well, it will if I run this code but I wouldn't if I do not have excel. And I will use error trapping and notify the user of no excel if they did. This code does not have any references to excel or office so it won't crash ahead of time which is what I wanted.
I have to run it on my laptop tonight to make sure though.
Thanks!!
Code:
Imports System.Reflection
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim objApp_Late As Object
Dim objBook_Late As Object
Dim objBooks_Late As Object
Dim objSheets_Late As Object
Dim objSheet_Late As Object
Dim objRange_Late As Object
Dim Parameters As Object()
Try
' Get the class type and instantiate Excel.
Dim objClassType As Type
objClassType = Type.GetTypeFromProgID("Excel.Application")
objApp_Late = Activator.CreateInstance(objClassType)
'Get the workbooks collection.
objBooks_Late = objApp_Late.[GetType]().InvokeMember("Workbooks", BindingFlags.GetProperty, Nothing, objApp_Late, Nothing)
'Add a new workbook.
objBook_Late = objBooks_Late.[GetType]().InvokeMember("Add", BindingFlags.InvokeMethod, Nothing, objBooks_Late, Nothing)
'Get the worksheets collection.
objSheets_Late = objBook_Late.[GetType]().InvokeMember("Worksheets", BindingFlags.GetProperty, Nothing, objBook_Late, Nothing)
'Get the first worksheet.
Parameters = New [Object](0) {}
Parameters(0) = 1
objSheet_Late = objSheets_Late.[GetType]().InvokeMember("Item", BindingFlags.GetProperty, Nothing, objSheets_Late, Parameters)
'Get a range object that contains cell A1.
Parameters = New [Object](1) {}
Parameters(0) = "A1"
Parameters(1) = Missing.Value
objRange_Late = objSheet_Late.[GetType]().InvokeMember("Range", BindingFlags.GetProperty, Nothing, objSheet_Late, Parameters)
'Write "Hello, World!" in cell A1.
Parameters = New [Object](0) {}
Parameters(0) = "Hello, World!"
objRange_Late.[GetType]().InvokeMember("Value", BindingFlags.SetProperty, Nothing, objRange_Late, Parameters)
'Return control of Excel to the user.
Parameters = New [Object](0) {}
Parameters(0) = True
objApp_Late.[GetType]().InvokeMember("Visible", BindingFlags.SetProperty, Nothing, objApp_Late, Parameters)
objApp_Late.[GetType]().InvokeMember("UserControl", BindingFlags.SetProperty, Nothing, objApp_Late, Parameters)
Catch theException As Exception
Dim errorMessage As [String]
errorMessage = "Error: "
errorMessage = [String].Concat(errorMessage, theException.Message)
errorMessage = [String].Concat(errorMessage, " Line: ")
errorMessage = [String].Concat(errorMessage, theException.Source)
MessageBox.Show(errorMessage, "Error")
End Try
End Sub
End Class
-
Oct 23rd, 2013, 03:29 PM
#8
Re: Create a program to run on pc with or without excel?
Please someone correct me if I am wrong. What you will gain from late binding is that the editor will not give you errors trying to include the properties of objects that you may have, losing with it the advantage of intellisense. But as for the way you are reading the file, it must be an excel file so if the target computer does not have excel, it will still fail. Your error trapping is a must, but will work on both codes.
More important than the will to succeed, is the will to prepare for success.
Please rate the posts, your comments are the fuel to keep helping people
-
Oct 23rd, 2013, 03:38 PM
#9
Thread Starter
Frenzied Member
Re: Create a program to run on pc with or without excel?
I'm sure you are right. But this will only be one part of the program to import an excel file. The example I have creates one. The user should not select to use excel and go into this part if they don't have excel. They will select another file format. If they are stupid enough to select excel anyways, my error trapping will catch it and show a message. Does that sound okay?
-
Oct 23rd, 2013, 05:48 PM
#10
Re: Create a program to run on pc with or without excel?
The other advantage to late binding is that it is not version dependent.
Sounds like your approach should work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|