Error handling with VBA compiling?

China☆狼群 提交于 2021-02-11 13:41:22

问题


How easy would it be for someone with only decent programming experience to add error handling for compiling? I have an Excel-VBA program that on some computers fails to locate the references, so I made a common folder and placed the .dll files there. Is there a way I can automate this error handling process? It is a compile error for "cannot find library" so I imagine I cannot do this with VBA. Is this easily doable? Thoughts?

I did not attach code because there is nothing relevant to show. I get a compile error on some computers because there is a missing reference that I would like to have a program locate on error, rather than a human.


回答1:


Error handling refers to run-time errors. You can't handle compile-time errors with error handling, since executing the error handling requires compiling and running the code in the first place.

What you can do, is change how you're compiling your code. For example, this code requires a compile-time reference to MyLibrary:

Dim foo As MyLibrary.Something

This code doesn't:

Dim foo As Object

The difference is that member calls against foo are no longer resolved at compile-time - which means you can't get intellisense when coding against it; VBA will happily compile a typo and run-time will blow up with error 438 saying it can't find the specified member.

foo.DoSomething 'works if foo has a DoSomething member.
foo.IDontExist  'compiles but throws error 438 at run-time.

When your project has a reference to MyLibrary, you're early-binding - meaning code will refuse to compile if, say, the DLL can't be found.

When your project doesn't have a reference to MyLibrary, and your code doesn't include any identifier that's defined in that library (Option Explicit becomes utterly important then), you're late-binding - meaning the compiler will not care to validate the member calls, and the linking happens at run-time.

Converting your early-bound code to late-bound code will require extensive overhaul of every single declaration, replacing every single class with Object, then removing the references to the DLL's.

Then you can't do this anymore:

Set foo = New MyLibrary.Something

Instead, you use the class' registered ProgId (a GUID works too) to locate the DLL at run-time by hitting the Windows Registry entry for it, using the CreateObject function:

Set foo = CreateObject("MyLibrary.Something")

If the MyLibrary.Something ProgID isn't registered on the machine the code is running on, a run-time error will occur, that you can handle at run-time with classic On Error statements. The correct ProgID values to use depend on how the DLLs were registered; they may or may not match the "Library.TypeName" scheme.




回答2:


You can trap some compile errors. In this example, I was able to trap whether the Microsoft Word 16.0 Object Library was missing (which was required later in the code), and instruct the user what to do.

In the Workbook_BeforeSave event, record the version of Office that last saved the Macro workbook; then, when it's opened, test whether the current version is older than the version it was saved under. Similar code could be used to test whether a specific .dll exists in the filesystem, and then instruct the user how to correct the problem. It's not a perfect solution, but better than nothing

This code goes in ThisWorkbook.:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
   Me.Sheets("Macros").Activate
   Sheets("Macros").Shapes(1).TextFrame.Characters.Text = "Show Tools" & Chr(10) & "Application Version: " & Application.Version
End Sub

Sub Workbook_Open()
   Dim sAppVersion As String
   Application.EnableEvents = True
   Me.Windows(1).ActivateNext
   sAppVersion = Split(Split(Sheets("Macros").Shapes(1).TextFrame.Characters.Text, "Application Version: ")(1), vbCr)(0)
   If sAppVersion > Application.Version Then
       MsgBox ("You are running these Macros in an older version than it was created in.  If you get an error, end the macro (Run -> Reset) and update the References in the VBA." _
           & vbCr _
           & "To do this, press Alt+F11 and click Tools -> References....  Then uncheck the Missing Reference, scroll down and check " _
           & "Microsoft Word " & Application.Version & " Object Library")
   End If

End Sub


来源:https://stackoverflow.com/questions/51249572/error-handling-with-vba-compiling

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!