简体   繁体   中英

vb6 trying to get old program working with excel 2013

I have this code from a program that was created to take data and export it into excel it is in vb6 and I dont know much about vb6 I started coding in vb.net could someone tell me why this isnt working with excel 2013 it opens but then closes right away and i am unsure as to why.

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

even if i can get some direction on how to rewrite this it would be appreciated.

It's been a long time since I've used it, so you may need to address some of the finer points... But:

First and foremost - get rid of the On Error Resume Next it masks whatever happens next.

This is not a situation where you want to have errors ignored. Capture the error, show something useful (not debug information and not details that can be used for hacking) to the user, and resume or return (after cleanup of assigned object variables to avoid memory leaks).

Then step through the code to see what errors you get.

Some of the things that may help:

Change the specification in your GetObject to be "Excel.Application" ie

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

Instead of MyExcel.Show which may or may not be supported, use actual Excel objects and their methods and properties, for instance the commented MyExcel.Application.Visible = True works, while I question the MyExcel.document(1).Visible = True .

Look up the Excel object model help for details. And never hardcode the index - obtain the actual reference that you want and use it.

You can still find articles about Excel and VB6 on line. Use your search engine of choice and good luck.

Beware of those who say something is impossible - first see if it works. Some say you can's install VB6 on a 64-bit system - that is not true, but there are some issues getting it to work. Some say you can't interact with 64-bit Office from VB6, but that is not true. Maybe you can't do some things - I haven't done much with it, just enough to know it is possible to do some things.

Consider developing and testing with typed objects in VB6. It can be very helpful, but to make the application version independent, you will need to remove the object typing before final test and deployment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM