VB6试图让老程序与Excel 2013工作

我有一个程序,这是创build一个程序,是为了获取数据,并将其导出到excel它是在VB6,我不知道很多关于VB6我开始编码在VB.NET有人可以告诉我为什么这不是与Excel 2013的工作,但它打开,但然后马上closures,我不确定为什么。

Sub GetExcel() Dim MyExcel As Object ' Variable to hold reference ' to Microsoft Word. Dim ExcelWasNotRunning As Boolean ' Flag for final release. ' Test to see if there is a copy of Microsoft Excel already running. 10 On Error Resume Next ' Defer error trapping. ' Getobject function called without the first argument returns a ' reference to an instance of the application. If the application isn't ' running, an error occurs. 20 Set MyExcel = GetObject(, "XLMAIN") 30 If Err.Number <> 0 Then ExcelWasNotRunning = True 40 Err.Clear ' Clear Err object in case error occurred. ' Check for Microsoft Excel. If Microsoft Excel is running, ' enter it into the Running Object table. 50 DetectExcel ' Set the object variable to reference the file you want to see. 60 Set MyExcel = GetObject(App.Path & "\test.xls") ' Show Microsoft Word through its Application property. Then ' show the actual window containing the file using the Windows ' collection of the MyWord object reference. ' MyExcel.Application.Visible = True ' MyExcel.document(1).Visible = True 70 MyExcel.Show , f1 '////////////////////////////////////////////// ' Do manipulations of your file here. '////////////////////////////////////////////// ' ... ' If this copy of Microsoft Excel was not running when you ' started, close it using the Application property's Quit method. ' Note that when you try to quit Microsoft Excel, the ' title bar blinks and a message is displayed asking if you ' want to save any loaded files. 80 If ExcelWasNotRunning = True Then 90 MyExcel.Application.Quit 100 End If 110 Set MyExcel = Nothing ' Release reference to the ' application and spreadsheet. End Sub Sub DetectExcel() ' Procedure dectects a running Word and registers it. Const WM_USER = 1024 Dim hwnd As Long ' If Excel is running this API call returns its handle. 10 hwnd = FindWindow("XLMAIN", 0) 20 If hwnd = 0 Then ' 0 means Word not running. 30 Exit Sub 40 Else ' Word is running so use the SendMessage API ' function to enter it in the Running Object Table. 50 SendMessage hwnd, WM_USER + 18, 0, 0 60 End If End Sub 

即使我可以得到一些方向如何重写这将不胜感激。

我已经使用了很长一段时间,所以你可能需要解决一些更好的点…但是:

首先,摆脱On Error Resume Next它掩盖了On Error Resume Next发生的任何事情。

这不是您想要忽略错误的情况。 捕获错误,向用户显示一些有用的信息(不包括debugging信息和可用于黑客攻击的细节),并恢复或返回(在清理已分配的对象variables以避免内存泄漏)。

然后通过代码来看看你得到了什么错误。

一些可能有所帮助的事情:

将您的GetObject的规范更改为"Excel.Application"

 Set MyExcel = GetObject(, "Excel.Application") 

使用实际的Excel对象及其方法和属性,例如注释MyExcel.Application.Visible = True ,而我质疑MyExcel.document(1).Visible = True而不是MyExcel.Show可能支持也可能不支持MyExcel.document(1).Visible = True

有关详细信息,请查阅Excel对象模型帮助。 并且永远不要硬编码索引 – 获取您想要的实际引用并使用它。

你仍然可以在线find关于Excel和VB6的文章。 select你的search引擎,祝你好运。

谨防那些说不可能的事 – 先看看它是否有效。 有人说,你可以在64位系统上安装VB6 – 这是不正确的,但有一些问题得到它的工作。 有人说,你不能从VB6与64位Office交互,但事实并非如此。 也许你不能做一些事情 – 我没有做太多的事情,只是知道做一些事情是可能的。

考虑使用VB6中的types对象进行开发和testing。 这可能是非常有帮助的,但为了使应用程序版本独立,您需要在最终testing和部署之前删除对象types。