School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,144 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 3,821 people online right now. Registration is fast and FREE... Join Now!



[Help] Excel Printing/Saving/Opening

  • (2 Pages)
  • +
  • 1
  • 2

[Help] Excel Printing/Saving/Opening Rate Topic: -----

#1 MuSSi  Icon User is offline

  • D.I.C Head
  • Icon
  • Group: Contributors
  • Posts: 84
  • Joined: 05-June 09


Dream Kudos: 50

Posted 05 June 2009 - 06:24 PM

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: 05 June 2009 - 06:37 PM

Was This Post Helpful? 0
  • +
  • -


#2 June7  Icon User is offline

  • D.I.C Addict
  • Icon
  • Group: Members w/DIC++
  • Posts: 591
  • Joined: 09-December 08


Dream Kudos: 0

Posted 05 June 2009 - 08:41 PM

Web Search: VB Open Excel Workbook
http://xldennis.word...bnet-solutions/
http://forums.devx.c...ead.php?t=98175
Was This Post Helpful? 1
  • +
  • -

#3 MuSSi  Icon User is offline

  • D.I.C Head
  • Icon
  • Group: Contributors
  • Posts: 84
  • Joined: 05-June 09


Dream Kudos: 50

Posted 06 June 2009 - 02:00 AM

When I entered
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:
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

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

Type 'Excel.Application' is not defined.
Type 'Excel.Workbook' is not defined.

Was This Post Helpful? 0
  • +
  • -

#4 June7  Icon User is offline

  • D.I.C Addict
  • Icon
  • Group: Members w/DIC++
  • Posts: 591
  • Joined: 09-December 08


Dream Kudos: 0

Posted 06 June 2009 - 11:35 PM

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):
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-arch...4/msg03216.html

This post has been edited by June7: 07 June 2009 - 12:23 AM

Was This Post Helpful? 0
  • +
  • -

#5 Bort  Icon User is offline

  • Echo-3 to Carlos Spiceyweiner.
  • Icon
  • Group: Authors
  • Posts: 1,549
  • Joined: 18-September 06


Dream Kudos: 350

Posted 08 June 2009 - 04:34 AM

I have a tutorial which might help you.

Here
Was This Post Helpful? 1
  • +
  • -

#6 MuSSi  Icon User is offline

  • D.I.C Head
  • Icon
  • Group: Contributors
  • Posts: 84
  • Joined: 05-June 09


Dream Kudos: 50

Posted 09 June 2009 - 04:15 AM

View PostBort, on 8 Jun, 2009 - 04:34 AM, said:

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
Was This Post Helpful? 0
  • +
  • -

#7 Bort  Icon User is offline

  • Echo-3 to Carlos Spiceyweiner.
  • Icon
  • Group: Authors
  • Posts: 1,549
  • Joined: 18-September 06


Dream Kudos: 350

Posted 09 June 2009 - 05:31 AM

Use this line:

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



But instead of having App.StartupPath...etc... you'll want
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: 09 June 2009 - 05:31 AM

Was This Post Helpful? 0
  • +
  • -

#8 MuSSi  Icon User is offline

  • D.I.C Head
  • Icon
  • Group: Contributors
  • Posts: 84
  • Joined: 05-June 09


Dream Kudos: 50

Posted 09 June 2009 - 05:18 PM

View PostBort, on 9 Jun, 2009 - 05:31 AM, said:

Use this line:

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



But instead of having App.StartupPath...etc... you'll want
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
Was This Post Helpful? 0
  • +
  • -

#9 MuSSi  Icon User is offline

  • D.I.C Head
  • Icon
  • Group: Contributors
  • Posts: 84
  • Joined: 05-June 09


Dream Kudos: 50

Posted 09 June 2009 - 08:23 PM

Quote

Use this line:

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



But instead of having App.StartupPath...etc... you'll want
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: 09 June 2009 - 08:25 PM

Was This Post Helpful? 0
  • +
  • -

#10 June7  Icon User is offline

  • D.I.C Addict
  • Icon
  • Group: Members w/DIC++
  • Posts: 591
  • Joined: 09-December 08


Dream Kudos: 0

Posted 09 June 2009 - 11:36 PM

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: 09 June 2009 - 11:47 PM

Was This Post Helpful? 0
  • +
  • -

#11 fixo  Icon User is offline

  • D.I.C Head
  • PipPip
  • Group: Members
  • Posts: 109
  • Joined: 10-May 09


Dream Kudos: 0

Posted 10 June 2009 - 12:14 AM

View PostMuSSi, on 9 Jun, 2009 - 08:23 PM, said:


Here is the simpliestway to print
 
 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 June 2009 - 01:16 AM

Was This Post Helpful? 0
  • +
  • -

#12 MuSSi  Icon User is offline

  • D.I.C Head
  • Icon
  • Group: Contributors
  • Posts: 84
  • Joined: 05-June 09


Dream Kudos: 50

Posted 10 June 2009 - 01:07 AM

June7 Thats my entire 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:
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 June 2009 - 02:00 AM

Was This Post Helpful? 0
  • +
  • -

#13 Bort  Icon User is offline

  • Echo-3 to Carlos Spiceyweiner.
  • Icon
  • Group: Authors
  • Posts: 1,549
  • Joined: 18-September 06


Dream Kudos: 350

Posted 10 June 2009 - 02:31 AM

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 :)
Was This Post Helpful? 0
  • +
  • -

#14 MuSSi  Icon User is offline

  • D.I.C Head
  • Icon
  • Group: Contributors
  • Posts: 84
  • Joined: 05-June 09


Dream Kudos: 50

Posted 10 June 2009 - 04:12 AM

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 June 2009 - 05:01 AM

Was This Post Helpful? 0
  • +
  • -

#15 Bort  Icon User is offline

  • Echo-3 to Carlos Spiceyweiner.
  • Icon
  • Group: Authors
  • Posts: 1,549
  • Joined: 18-September 06


Dream Kudos: 350

Posted 10 June 2009 - 04:52 AM

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:

Imports Excel = Microsoft.Office.Interop.Excel



and see if it helps :)

This post has been edited by Bort: 10 June 2009 - 04:57 AM

Was This Post Helpful? 1

#16 MuSSi  Icon User is offline

  • D.I.C Head
  • Icon
  • Group: Contributors
  • Posts: 84
  • Joined: 05-June 09


Dream Kudos: 50

Posted 10 June 2009 - 05:15 AM

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
Was This Post Helpful? 0
  • +
  • -

#17 Bort  Icon User is offline

  • Echo-3 to Carlos Spiceyweiner.
  • Icon
  • Group: Authors
  • Posts: 1,549
  • Joined: 18-September 06


Dream Kudos: 350

Posted 10 June 2009 - 05:21 AM

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 :)
Was This Post Helpful? 0
  • +
  • -

#18 MuSSi  Icon User is offline

  • D.I.C Head
  • Icon
  • Group: Contributors
  • Posts: 84
  • Joined: 05-June 09


Dream Kudos: 50

Posted 10 June 2009 - 05:27 AM

Alright well just 1 more thing, how come it won't show the icons such as open, save & print ( it did before )
Was This Post Helpful? 0
  • +
  • -

#19 Bort  Icon User is offline

  • Echo-3 to Carlos Spiceyweiner.
  • Icon
  • Group: Authors
  • Posts: 1,549
  • Joined: 18-September 06


Dream Kudos: 350

Posted 10 June 2009 - 05:42 AM

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.
Was This Post Helpful? 0
  • +
  • -

#20 MuSSi  Icon User is offline

  • D.I.C Head
  • Icon
  • Group: Contributors
  • Posts: 84
  • Joined: 05-June 09


Dream Kudos: 50

Posted 10 June 2009 - 04:06 PM

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 June 2009 - 06:27 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

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

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month