【发布时间】:2016-02-24 10:29:26
【问题描述】:
这就是我遇到的问题。我正在将旧的 Excel 宏转换为 Excel 插件,以便我可以更轻松地与我的同事分享。我是 VB.net 的新手,但我正在尽我所能,所以请放轻松。
我有一个允许用户输入数据的 Windows 表单,当他们点击输入数据按钮时,数据应该从表单转到特定的工作表。代码如下:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form_CutListEntry
Dim xApp As New Excel.Application
Dim wss As Microsoft.Office.Tools.Excel.Worksheet
Private Sub Btn_InsertJobInfo_Click(sender As Object, e As EventArgs) Handles Btn_InsertJobInfo.Click
wss = xApp.Worksheets("Job Info")
'Check that all data is entered
If Trim(TxtBx_CustomerName.Text) = "" Then
TxtBx_CustomerName.Focus()
MsgBox("Please enter a Customer Name")
Exit Sub
End If
If Trim(TxtBx_OrderNum.Text) = "" Then
TxtBx_OrderNum.Focus()
MsgBox("Please enter an Order Number")
Exit Sub
End If
If Trim(TxtBx_CutlistAuthor.Text) = "" Then
TxtBx_CutlistAuthor.Focus()
MsgBox("Please enter your initials")
Exit Sub
End If
'Write data to excel worksheet.
wss.Cells(3, 1) = "Customer Name: " + TxtBx_CustomerName.Text
wss.Cells(4, 1) = "Order Number: " + TxtBx_OrderNum.Text
wss.Cells(5, 1) = "Todays Date: " + TxtBx_TodaysDate.Text
wss.Cells(6, 1) = "Cutting List Prepared By: " + TxtBx_CutlistAuthor.Text
Exit Sub
End Sub
(注意我取出了 cmets 和一些不相关的额外部分,因此下面的详细错误消息有错误的行号)
我可以从 excel 中很好地打开 windows 窗体,但是当我输入一些数据并单击输入数据时会发生这种情况:
An exception of type 'System.Runtime.InteropServices.COMException' occurred in Toms CutList Maker.dll but was not handled in user code
Additional information: Exception from HRESULT: 0x800A03EC
在这一行:
wss = xApp.Worksheets("Job Info")
任何人都可以指出我的写作方向吗?
如果有人感兴趣,这里是完整的错误详细信息:
System.Runtime.InteropServices.COMException was unhandled by user code
ErrorCode=-2146827284
HResult=-2146827284
Message=Exception from HRESULT: 0x800A03EC
Source=Microsoft.Office.Interop.Excel
StackTrace:
at Microsoft.Office.Interop.Excel.ApplicationClass.get_Worksheets()
at Toms_CutList_Maker.Form_CutListEntry.Btn_InsertJobInfo_Click(Object sender, EventArgs e) in d:\tom\documents\visual studio 2013\Projects\Toms CutList Maker\Toms CutList Maker\CutList Entry.vb:line 15
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 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.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
【问题讨论】:
-
Worksheet Object不是Excel Application Object的成员,但它是Workbook Object的成员,后者是Excel Application Object的成员。所以首先,设置Workbook,然后设置Worksheet。