Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2008 > Visual Basic 2008 Essentials
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Visual Basic 2008 Essentials If you are new to Visual Basic programming with version 2008, this is the place to start your questions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2008 Essentials section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 23rd, 2008, 03:36 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Working with Excel in VB.net

I am a newbe and working on a program that will open excel and enter a value in the first open cell in column "A". I can make this code work in VBA, but VB.net is giving me the same success. Can anyone help?

Imports Microsoft.Office.Interop
Imports System.IO

Public Class Form1

Dim oEx As Excel.Workbook
Dim iEx As Excel.Application

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

iEx = New Excel.Application
iEx.Visible = True
oEx = iEx.Workbooks.Open("C:\Program Files\Nexulator\Schedule.xls")

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
iEx.Range("A1").End(Excel.XlDirection.xlDown).Sele ct()
iEx.ActiveCell.Offset(1, 0).Select()
iEx.ActiveCell.Value = TextBox1.Text

End Sub

End Class



Reply With Quote
  #2 (permalink)  
Old May 23rd, 2008, 04:57 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

What happens when you run this?
Will it compile?
Does it generate any warnings when you compile it?
Reply With Quote
  #3 (permalink)  
Old May 26th, 2008, 08:01 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

0x800A03EC

is the error I get.

Reply With Quote
  #4 (permalink)  
Old May 27th, 2008, 12:59 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

come on... What is the text of the message? Who memorizes error numbers...
Reply With Quote
  #5 (permalink)  
Old May 29th, 2008, 07:02 AM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Below is the complete text of the error from Studio.net 2008

System.Runtime.InteropServices.COMException was unhandled
  ErrorCode=-2146827284
  Message="Exception from HRESULT: 0x800A03EC"
  Source=""
  StackTrace:
       at System.RuntimeType.ForwardCallToInvokeMember(Strin g memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) at Microsoft.Office.Interop.Excel.Range.get_Offset(Ob ject RowOffset, Object ColumnOffset) at WindowsApplication1.Form1.Button2_Click(Object sender, EventArgs e) in C:\Documents and Settings\skalman\My Documents\Visual Studio 2008\Projects\excel5-23\excel5-23\Form1.vb:line 24 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventAr gs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(ApplicationCo ntext context) at Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.OnRun() at Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.DoApplicationModel() at Microsoft.VisualBasic.ApplicationServices.WindowsF ormsApplicationBase.Run(String[] commandLine) at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context( Object state) at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
  InnerException:


Reply With Quote
  #6 (permalink)  
Old May 29th, 2008, 12:15 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

OK. That shows me that it is button 2 that is raising the error.
I am curious what "HRESULT" is, and where it is... But that is beside the point.

But in looking at button 2ís routine, Iím curious: Which statement raises the error?

If you examine iEx in the watch window do you find that it has actually opened the file?
Reply With Quote
  #7 (permalink)  
Old May 29th, 2008, 01:51 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Button1 actually opens the file, and button2 accesses the file. The code gets stuck at the following line:

iEx.Range("A1").End(Microsoft.Office.Interop.Excel .XlDirection.xlDown).Offset(1, 0).Select()

Then in the "immediate window" the following error is shown.

A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

Reply With Quote
  #8 (permalink)  
Old May 29th, 2008, 03:13 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Try highlighting iEx, right-clicking the highlit text, and selecting to add it to the Watch Window.
Then go there and see if the variable actually references anything.

If it does, do the same thing, but after highlighting iEx.Range("A1")
Reply With Quote
  #9 (permalink)  
Old May 30th, 2008, 12:48 PM
Registered User
 
Join Date: May 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

There is no option in the context menu after right clicking for a watch window. I am working in VB.net 2008.

Reply With Quote
  #10 (permalink)  
Old May 30th, 2008, 02:52 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

This works while the program is running. So in the IDE, set a breakpoint early-on in the program flow of the button click. Then, highlight the element you want to watch, and add it to the watch window. (You're gonna like this feature!)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Working With Word Template From ASP.NET and VB.NE Haribabu74 Classic ASP Components 2 July 22nd, 2014 05:11 PM
Excel reports in vb.net juster21 .NET Framework 2.0 1 March 3rd, 2014 02:53 AM
vb.net and excel - HELP! vbnetgeek Pro VB.NET 2002/2003 3 May 10th, 2005 09:38 PM



All times are GMT -4. The time now is 09:23 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.