VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 306,807 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,674 people online right now. Registration is fast and FREE... Join Now!




[Help] Excel Printing/Saving/Opening

2 Pages V  1 2 >  

[Help] Excel Printing/Saving/Opening

MuSSi

5 Jun, 2009 - 06:24 PM
Post #1

D.I.C Head
Group Icon

Joined: 5 Jun, 2009
Posts: 84



Thanked: 1 times
Dream Kudos: 50
My Contributions
Hey Guys,
My first topic/post and I'm new to these forums and I need help with spreadsheet,
I need a tutorial or someone to help me with printing/saving/opening a spreadsheet.
So far I've got no leads or anything and I've been searching for 2 days without any help.
Is there a way to print a spreadsheet and save the spreadsheet and also open it?
Thanks a lot much appreciated

Edit: Visual Basic 2008

This post has been edited by MuSSi: 5 Jun, 2009 - 06:37 PM

User is offlineProfile CardPM
+Quote Post


June7

RE: [Help] Excel Printing/Saving/Opening

5 Jun, 2009 - 08:41 PM
Post #2

D.I.C Regular
Group Icon

Joined: 9 Dec, 2008
Posts: 485



Thanked: 38 times
My Contributions
Web Search: VB Open Excel Workbook
http://xldennis.wordpress.com/2007/11/16/o...bnet-solutions/
http://forums.devx.com/showthread.php?t=98175
User is offlineProfile CardPM
+Quote Post

MuSSi

RE: [Help] Excel Printing/Saving/Opening

6 Jun, 2009 - 02:00 AM
Post #3

D.I.C Head
Group Icon

Joined: 5 Jun, 2009
Posts: 84



Thanked: 1 times
Dream Kudos: 50
My Contributions
When I entered
CODE
Imports Excel = Microsoft.Office.Interop.Excel
Imports Office = Microsoft.Office.Core

Public Class Form4
    Private xlApp As New Excel.Application
    Private xlWBook As Excel.Workbook
End Class
provided in the first link i get the following error:
CODE

Warning    1    Namespace or type specified in the Imports 'Microsoft.Office.Interop.Excel' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.    C:\Documents and Settings\MuSSi\My Documents\Visual Studio 2008\Projects\Excel\Excel\Form4.vb    1    9    Excel
CODE
Warning    2    Namespace or type specified in the Imports 'Microsoft.Office.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.    C:\Documents and Settings\MuSSi\My Documents\Visual Studio 2008\Projects\Excel\Excel\Form4.vb    2    9    Excel
CODE
Type 'Excel.Application' is not defined.
CODE
Type 'Excel.Workbook' is not defined.

User is offlineProfile CardPM
+Quote Post

June7

RE: [Help] Excel Printing/Saving/Opening

6 Jun, 2009 - 11:35 PM
Post #4

D.I.C Regular
Group Icon

Joined: 9 Dec, 2008
Posts: 485



Thanked: 38 times
My Contributions
I got this to work for me (in Access VBA and need to select the Microsoft Excel xx.0 Object Library in the VBA References):
CODE
Private Sub OpenExcel()

Dim applExcel As New Excel.Application
Dim xlWorkbook As Excel.Workbook

Set applExcel = CreateObject("Excel.Application")

applExcel.Visible = True

Set xlWorkbook = applExcel.Workbooks.Open("C:\Users\June\Documents\LL\Schedules.xlsx")
Set xlSheet = xlWorkbook.Sheets("Games")

xlSheet.PrintOut
xlworkbook.Save
xlworkbook.Close
applExcel.Quit
applExcel = Nothing

End Sub
Here's another site:
http://www.tech-archive.net/Archive/Excel/...4/msg03216.html

This post has been edited by June7: 7 Jun, 2009 - 12:23 AM
User is offlineProfile CardPM
+Quote Post

Bort

RE: [Help] Excel Printing/Saving/Opening

8 Jun, 2009 - 04:34 AM
Post #5

VBort.NET
Group Icon

Joined: 18 Sep, 2006
Posts: 1,391



Thanked: 27 times
Dream Kudos: 350
My Contributions
I have a tutorial which might help you.

Here
User is offlineProfile CardPM
+Quote Post

MuSSi

RE: [Help] Excel Printing/Saving/Opening

9 Jun, 2009 - 04:15 AM
Post #6

D.I.C Head
Group Icon

Joined: 5 Jun, 2009
Posts: 84



Thanked: 1 times
Dream Kudos: 50
My Contributions
QUOTE(Bort @ 8 Jun, 2009 - 04:34 AM) *

I have a tutorial which might help you.

Here
Its not exactly what I needed but the template thing was. Is there a way to open the .xls file that i imported to the project ( e.g Form1.Show() ) I know its not as simple as that but is there something like that? Please Help
Thanks

User is offlineProfile CardPM
+Quote Post

Bort

RE: [Help] Excel Printing/Saving/Opening

9 Jun, 2009 - 05:31 AM
Post #7

VBort.NET
Group Icon

Joined: 18 Sep, 2006
Posts: 1,391



Thanked: 27 times
Dream Kudos: 350
My Contributions
Use this line:

CODE

        oBook = oExcel.Workbooks.Open(Application.StartupPath & "\" & TextBox5.Text & ".xlt")


But instead of having App.StartupPath...etc... you'll want
CODE
Application.StartupPath & "\Filename.xls"


The Application.StartupPath is the bit that references your project's install directory.

For testing, copy the .xls file into the <Project>\bin\debug folder.

This post has been edited by Bort: 9 Jun, 2009 - 05:31 AM
User is offlineProfile CardPM
+Quote Post

MuSSi

RE: [Help] Excel Printing/Saving/Opening

9 Jun, 2009 - 05:18 PM
Post #8

D.I.C Head
Group Icon

Joined: 5 Jun, 2009
Posts: 84



Thanked: 1 times
Dream Kudos: 50
My Contributions
QUOTE(Bort @ 9 Jun, 2009 - 05:31 AM) *

Use this line:

CODE

        oBook = oExcel.Workbooks.Open(Application.StartupPath & "\" & TextBox5.Text & ".xlt")


But instead of having App.StartupPath...etc... you'll want
CODE
Application.StartupPath & "\Filename.xls"


The Application.StartupPath is the bit that references your project's install directory.

For testing, copy the .xls file into the <Project>\bin\debug folder.

oBook and oExcel aren't declared.
Is there a command such as Shell that will open a .xls file?
Thats exactly what I'm trying to do but Shell only works with .exe
User is offlineProfile CardPM
+Quote Post

MuSSi

RE: [Help] Excel Printing/Saving/Opening

9 Jun, 2009 - 08:23 PM
Post #9

D.I.C Head
Group Icon

Joined: 5 Jun, 2009
Posts: 84



Thanked: 1 times
Dream Kudos: 50
My Contributions
QUOTE

Use this line:

CODE

        oBook = oExcel.Workbooks.Open(Application.StartupPath & "\" & TextBox5.Text & ".xlt")


But instead of having App.StartupPath...etc... you'll want
CODE
Application.StartupPath & "\Filename.xls"


The Application.StartupPath is the bit that references your project's install directory.

For testing, copy the .xls file into the <Project>\bin\debug folder.

oBook and oExcel aren't declared.
Is there a command such as Shell that will open a .xls file?
Thats exactly what I'm trying to do but Shell only works with .exe

Edit: I tried OpenFileDialog1.Show() on a button click event but when i double clicked the .xls file it didnt do anything, is there a way to skip the dialog and just open the xls file at a certain directory not giving the user an option

This post has been edited by MuSSi: 9 Jun, 2009 - 08:25 PM
User is offlineProfile CardPM
+Quote Post

June7

RE: [Help] Excel Printing/Saving/Opening

9 Jun, 2009 - 11:36 PM
Post #10

D.I.C Regular
Group Icon

Joined: 9 Dec, 2008
Posts: 485



Thanked: 38 times
My Contributions
Were you not able to adapt to VB.Net the VBA example I presented in earlier post? Between that and Bort's terrific tutorial, you should have enough to accomplish this task. Perhaps if you posted your actual code for analysis we could be more helpful.

This post has been edited by June7: 9 Jun, 2009 - 11:47 PM
User is offlineProfile CardPM
+Quote Post

fixo

RE: [Help] Excel Printing/Saving/Opening

10 Jun, 2009 - 12:14 AM
Post #11

D.I.C Head
**

Joined: 10 May, 2009
Posts: 78



Thanked: 5 times
My Contributions
QUOTE(MuSSi @ 9 Jun, 2009 - 08:23 PM) *


Here is the simpliestway to print
CODE

Imports System.IO
Imports System.Globalization  
..........................................
    Dim strFileName As String = Application.StartupPath & "\Data.xls"
        Dim proc As New Process
        proc.StartInfo.FileName = strFileName
        proc.StartInfo.Verb = "Print"
        proc.StartInfo.CreateNoWindow = True
        proc.Start()


Change the full path of file to suit

~'J'~

This post has been edited by fixo: 10 Jun, 2009 - 01:16 AM
User is offlineProfile CardPM
+Quote Post

MuSSi

RE: [Help] Excel Printing/Saving/Opening

10 Jun, 2009 - 01:07 AM
Post #12

D.I.C Head
Group Icon

Joined: 5 Jun, 2009
Posts: 84



Thanked: 1 times
Dream Kudos: 50
My Contributions
June7 Thats my entire code.
CODE

Public Class Form2

    Private Sub OpenExcel()

        Dim applExcel As New Excel.Application
        Dim xlWorkbook As Excel.Workbook

        applExcel = CreateObject("Excel.Application")

        applExcel.Visible = True

        xlWorkbook = applExcel.Workbooks.Open("C:\Users\June\Documents\LL\Schedules.xlsx")
        xlSheet = xlWorkbook.Sheets("Games")

        xlSheet.PrintOut()
        xlworkbook.Save()
        xlworkbook.Close()
        applExcel.Quit()
        applExcel = Nothing

    End Sub
End Class


and these are my errors:
CODE

Error    1    Type 'Excel.Application' is not defined.    C:\Documents and Settings\MuSSi\My Documents\Visual Studio 2008\Projects\GTM\Excel\Form2.vb    5    30    Excel
Error    2    Type 'Excel.Workbook' is not defined.    C:\Documents and Settings\MuSSi\My Documents\Visual Studio 2008\Projects\GTM\Excel\Form2.vb    6    27    Excel
Error    3    Name 'xlSheet' is not declared.    C:\Documents and Settings\MuSSi\My Documents\Visual Studio 2008\Projects\GTM\Excel\Form2.vb    13    9    Excel
Error    4    Name 'xlSheet' is not declared.    C:\Documents and Settings\MuSSi\My Documents\Visual Studio 2008\Projects\GTM\Excel\Form2.vb    15    9    Excel


Sorry I'm new to all this,
Just to be sure this is visual basic 2008 express edition right?

This post has been edited by MuSSi: 10 Jun, 2009 - 02:00 AM
User is offlineProfile CardPM
+Quote Post

Bort

RE: [Help] Excel Printing/Saving/Opening

10 Jun, 2009 - 02:31 AM
Post #13

VBort.NET
Group Icon

Joined: 18 Sep, 2006
Posts: 1,391



Thanked: 27 times
Dream Kudos: 350
My Contributions
This code is correct Mussi, and sorry, but my bad for not telling you this earlier...you missed the first part of my tutorial...

QUOTE

References

Before you can start automating any of the Office applications, you will need to add a reference to the application into your project. To do this, go to the Project menu, select Add Reference, and click on the COM tab. In the list, find the following entry:

Microsoft <App> Object Library <x>.0

Where <App> is the application you wish to automate, and <x> is the version number. For example, if I wanted to automate Word, it would be Microsoft Word Object Library 9.0. The version number depends on what version of Office you are using.

Office 2000 = version 9.0
Office XP = version 10.0
Office 2003 = version 11.0
Office 2007 = version 12.0.


This should solve your problems smile.gif
User is offlineProfile CardPM
+Quote Post

MuSSi

RE: [Help] Excel Printing/Saving/Opening

10 Jun, 2009 - 04:12 AM
Post #14

D.I.C Head
Group Icon

Joined: 5 Jun, 2009
Posts: 84



Thanked: 1 times
Dream Kudos: 50
My Contributions
Just to test why I'm getting all these errors I followed your tutorial added the reference Microsoft Word Object Library 11.0 and then did your code and came up with 2 errors,
Type 'Word.Application' is not defined.
Type 'Word.Document' is not defined.
Please help?

(It would be alot more helpful if someone could add me on MSN at Mussy69a@live.com.au)

This post has been edited by MuSSi: 10 Jun, 2009 - 05:01 AM
User is offlineProfile CardPM
+Quote Post

Bort

RE: [Help] Excel Printing/Saving/Opening

10 Jun, 2009 - 04:52 AM
Post #15

VBort.NET
Group Icon

Joined: 18 Sep, 2006
Posts: 1,391



Thanked: 27 times
Dream Kudos: 350
My Contributions
When I wrote that tutorial, I was using old tech (VB2005 and Office 2000) so I think it may now be slightly out of date. At the very top of your code, before the Public Class Form2 line, put this:

CODE

Imports Excel = Microsoft.Office.Interop.Excel


and see if it helps smile.gif

This post has been edited by Bort: 10 Jun, 2009 - 04:57 AM
User is offlineProfile CardPM
+Quote Post

MuSSi

RE: [Help] Excel Printing/Saving/Opening

10 Jun, 2009 - 05:15 AM
Post #16

D.I.C Head
Group Icon

Joined: 5 Jun, 2009
Posts: 84



Thanked: 1 times
Dream Kudos: 50
My Contributions
Thanks that worked,
I went back to see if I did anything wrong to June7s 1st post and saw that in a part of the script it uses WebBrowser1.Navigate to open the .xls file
thats all I needed but thank you both.
Bort could you please add me on MSN i'd like to learn a few things and I need help with 1 more thing
User is offlineProfile CardPM
+Quote Post

Bort

RE: [Help] Excel Printing/Saving/Opening

10 Jun, 2009 - 05:21 AM
Post #17

VBort.NET
Group Icon

Joined: 18 Sep, 2006
Posts: 1,391



Thanked: 27 times
Dream Kudos: 350
My Contributions
I don't use MSN, but I'm glad I could help.

Your best bet will be to create a new topic for the other thing you need help with. That way not just me can help you smile.gif
User is offlineProfile CardPM
+Quote Post

MuSSi

RE: [Help] Excel Printing/Saving/Opening

10 Jun, 2009 - 05:27 AM
Post #18

D.I.C Head
Group Icon

Joined: 5 Jun, 2009
Posts: 84



Thanked: 1 times
Dream Kudos: 50
My Contributions
Alright well just 1 more thing, how come it won't show the icons such as open, save & print ( it did before )
User is offlineProfile CardPM
+Quote Post

Bort

RE: [Help] Excel Printing/Saving/Opening

10 Jun, 2009 - 05:42 AM
Post #19

VBort.NET
Group Icon

Joined: 18 Sep, 2006
Posts: 1,391



Thanked: 27 times
Dream Kudos: 350
My Contributions
How come what won't show the icons? Excel, or VB?

Either way it is probably a toolbar thing. Right click in an empty gap on the toolbars and you should be able to select them from a drop down menu.
User is offlineProfile CardPM
+Quote Post

MuSSi

RE: [Help] Excel Printing/Saving/Opening

10 Jun, 2009 - 04:06 PM
Post #20

D.I.C Head
Group Icon

Joined: 5 Jun, 2009
Posts: 84



Thanked: 1 times
Dream Kudos: 50
My Contributions
The WebBrowser wont show the Excel icons but it did before when I first tried it


(Icons being the print/save/open ones)

Edit: Well this is obviously not what I wanted,
What would I do to open up the actual excel program with a selected spreadsheet?
Thanks!

This post has been edited by MuSSi: 10 Jun, 2009 - 06:27 PM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 09:48PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month