Open Excel from .NET

J

Jennyfer Barco

Hello, I have a question, how can I open Microsoft Excel from .NET. I only
need to open a new file in Excel and paste some information and set the
Microsoft Excel as the enabled aplication, so the user can continue working
in Excel and he'll save the information I pasted. I tried this but doesn't
open Excel at all, but it does save the file c:\test.xls with the value
"This is column B row 2" in colum B and row 2:

Dim xlApp As Interop.Excel.Application

Dim xlBook As Interop.Excel.Workbook

Dim xlSheet As Interop.Excel.Worksheet

xlApp = CType(CreateObject("Excel.Application"), Interop.Excel.Application)

xlBook = CType(xlApp.Workbooks.Add, Interop.Excel.Workbook)

xlSheet = CType(xlBook.Worksheets(1), Interop.Excel.Worksheet)

' Place some text in the second row of the sheet.

xlSheet.Cells(2, 2) = "This is column B row 2"

' Show the sheet.

xlSheet.Application.Visible = True

' Save the sheet to C:\Test.xls directory.

xlSheet.SaveAs("C:\Test.xls")



Thanks in advance
Jennyfer
 
J

Jan

Hi Jennyfer,

Are you sure you want to use automation for such a simple task? If you use
automation your application will be linked to a specific version of MS Excel
Object Library.

Instead, you can generate Excel-compatible file and use
System.Diagnostics.Process.Start method to run Excel or other associated
application:

Dim fileName As String = "Test.csv"

Dim sw As StreamWriter = New StreamWriter(fileName)

sw.WriteLine(",")

sw.WriteLine("," & Chr(34) & "This is column B row 2" & Chr(34))

sw.close()

System.Diagnostics.Process.Start(fileName)

If you don't want to use CSV files or want more advanced formatting you can
use our ExcelLite Free component to generate native XLS files. You can use
it in a commercial applications; it is only limited to max 150 rows per
sheet (http://www.gemboxsoftware.com/ExcelLiteFree.htm).

Regards,
Jan
 
D

Dragon

Hello Jennyfer,

Set the xlApp.Visible property to True.

BTW, why do you use the
xlApp = CType(CreateObject("Excel.Application"),
Interop.Excel.Application)

Why not

~
xlApp = New Interop.Excel.Application
~

?

Roman
 
C

Carlos J. Quintero [VB MVP]

Hi Jennyfer,

When you open programatically an Office application, assuming that your code
releases all COM references to the Application and the objects that you have
created, the application will be destroyed after the last reference is
released. In .NET is quite easy to get into the opposite problem, see

Office application does not quit after automation from Visual Studio .NET
client
http://support.microsoft.com/default.aspx?scid=kb;en-us;317109)

but since your intention is to keep the app open, you should use
Application.UserControl = True to increase artificially the number of COM
references. I am assuming that your code Application.Visible = True works
fine but the app is closed so fast that you can´t even see it. You can
verify it inserting a MsgBox just after making it visible.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top