在VB.NET中编写输出Word和Excel模块时候出错

tianshilei1992 2012-10-13 09:27:41
输出Word报表模块代码如下:

Private Sub 输出Word格式WToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 输出Word格式WToolStripMenuItem.Click
Dim wordApp As New Word.Application
Dim myDoc As Word.Document
Dim oTable As Word.Table
Dim rowIndex, colIndex As Integer
wordApp.Documents.Add()
myDoc = wordApp.ActiveDocument
rowIndex = 1
colIndex = 0
Dim Table As New DataTable
Table = objDataSetOutput.Tables(0)

oTable = myDoc.Tables.Add(Range:=myDoc.Range(Start:=0, [End]:=0), NumColumns:=Table.Rows.Count + 1, NumRows:=Table.Columns.Count)

'将所得到的表的列明赋值给单元格
Dim Col As DataColumn
Dim Row As DataRow
For Each Col In Table.Columns
colIndex = colIndex + 1
oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName)
Next

'将所得到的表所有行赋值给单元格
For Each Row In Table.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each Col In Table.Columns
colIndex = colIndex + 1
oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row(Col.ColumnName))
Next
Next

oTable.Borders.InsideLineStyle = 1
oTable.Borders.OutsideLineStyle = 1
wordApp.Visible = True

objDataSetOutput = Nothing
End Sub

当点击按钮时候,程序运行出错,错误提示:
未处理的COMException:oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName)集合要求的成员不存在
调试窗口输出内容:


在 System.Runtime.InteropServices.COMException 中第一次偶然出现的“PhoneStore.exe”类型的异常
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>未处理的异常</Description><AppDomain>PhoneStore.vshost.exe</AppDomain><Exception><ExceptionType>System.Runtime.InteropServices.COMException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>集合所要求的成员不存在。</Message><StackTrace> 在 Microsoft.Office.Interop.Word.Table.Cell(Int32 Row, Int32 Column)
在 PhoneStore.MainWnd.输出Word格式WToolStripMenuItem_Click(Object sender, EventArgs e) 位置 E:\PhoneStore\GUI\VB.NET\PhoneStore\MainWnd.vb:行号 544
在 System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
在 System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
在 System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
在 System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
在 System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
在 System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
在 System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
在 System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
在 System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message&amp; m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
在 System.Windows.Forms.ToolStrip.WndProc(Message&amp; m)
在 System.Windows.Forms.ToolStripDropDown.WndProc(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
在 PhoneStore.My.MyApplication.Main(String[] Args) 位置 17d14f5c-a337-4978-8281-53493378c1071.vb:行号 81
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()</StackTrace><ExceptionString>System.Runtime.InteropServices.COMException (0x800A1735): 集合所要求的成员不存在。
在 Microsoft.Office.Interop.Word.Table.Cell(Int32 Row, Int32 Column)
在 PhoneStore.MainWnd.输出Word格式WToolStripMenuItem_Click(Object sender, EventArgs e) 位置 E:\PhoneStore\GUI\VB.NET\PhoneStore\MainWnd.vb:行号 544
在 System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
在 System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
在 System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
在 System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
在 System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
在 System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
在 System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
在 System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
在 System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message&amp; m)
在 System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)
在 System.Windows.Forms.ToolStrip.WndProc(Message&amp; m)
在 System.Windows.Forms.ToolStripDropDown.WndProc(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
在 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
在 PhoneStore.My.MyApplication.Main(String[] Args) 位置 17d14f5c-a337-4978-8281-53493378c1071.vb:行号 81
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()</ExceptionString></Exception></TraceRecord>

整个模块的代码是在书上抄的,不知道怎么到我的机器上就有问题……

上面代码中,objDataSetOutput是一个Public类型的DataSet对象,是我点击查询按钮以后将数据Fill到这个Set中去的,该不会是这个是NULL引起的吧……
...全文
284 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tom000443 2013-12-02
  • 打赏
  • 举报
回复
我也出现这个问题!! 无解~~资料太少了
tianshilei1992 2012-10-13
  • 打赏
  • 举报
回复
人工置顶啊……

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧