I have VB6 code that executes various Excel macros. I set my breakpoints in VB6 and in the the Excel macro, then begin debugging in the VB6 application. When it reaches (a breakpoint) the statement to execute the macro, it skips over the command and continue traversing through my VB6 code. I was expecting it to jump to Excel and began debugging the Excel macro. Is there some configuration or trick, to debug an Excel macro that is being called from a VB6 application?
When VB6 executes the its own code, it allows the programmer using the IDE to set breakpoints and give control to user when the execution has reached those said breakpoints. However, setting breakpoints in Excel will only benefit you if you (the programmer) have executed the macro from Excel interface. Which is not the case when VB6 is in charge of firing up Excel and telling it to execute the macros within the workbook.
Here is what you can do:
migrate all of the macro code to VB6. It is fairly easy, you just need to add few words here and there like this
'In Excel macro:
Activecell.Offset(0,1).Value = "my value"
so if we want to migrate this line of macro to VB6:
'In VB6 you must declare an instance of EXCEL application:
Dim EX as Excel.Application
EX.Workbooks.Open('c:\my_excel_file.xls')
EX.Sheets(0).Activate 'Go to the first sheet
'Then you are back to business:
EX.ActiveCell.Offset(0,1).Value = "my vlaue"
'But don't forget to close and dispose of EX instance afterwards.
EX.Activeworkbook.Close 'Check save options
EX.Quit
Set EX = Nothing
After you have migrated all of the macros functionality to VB6, you can set breakpoints in VB6 and you will have all the control over the execution and will be able to debug the code as you intended to do.
Related
I am trying to run a VBScript that opens powerpoint and runs a macro named UpdateandBreaklinks. The Script that I am running is as follows:
Dim oApp
Dim oPres
Dim oSlide
Set oApp = CreateObject("Powerpoint.Application")
oApp.visible = true
Set oPres = oApp.Presentations.Open("C:\Users\PPwithLinks.pptm")
Set oSlide = oPres.Slides(1).Duplicate
oApp.Run "PPwithLinks.pptm!UpdateandBreaklinks"
This Gets the script to Run and open Powerpoint, however the Macro is not Ran. I get an error on this line:
oApp.Run "PPwithLinks.pptm!UpdateandBreaklinks"
I have tried to adjust it to this:
oApp.Run "UpdateandBreaklinks"
But I still get the same error. The error reads as:
Application.Run: Invalid request. Sub or funtion not defined.
Code: 80048240
I have researched the problem and all I can find is that people have remedied this same issue with the code above. I am not sure what I am doing incorrectly.
I have further tested the problem and I am now at this point:
So, I created a new pptm, I created a new macro for MsgBox "Hello" within the pptm. When I call the pptm to be opened with VBS, the pptm opens and I still get the error.
BUT, testing further, if I have opened the pptm first and then call the VBS the macro does run.(It opens another instance of the program as a [Read-Only] and runs the macro.)
Also, if I just open the pptm from the VBS(delete the line that runs the macro) and then try to run the Macro manually within PowerPoint then I get the following error: "Because of your security settings, macros have been disabled."
I go into the Macro Security settings and they have not been changed, so it appears to me that when VBS Opens the pptm something is disabling the Macros and therefore erroring out the VBS code to not call and run the macro.
I have this simple vbscript, but windows gives me an error when I'm trying to run it. I've never worked with vbscript before and i tried to google, but could figure out why. I'm getting expected end of line error at line 1, character 16. Does anyone see anything?
Also, I'm coding this in notepad and running it by double clicking the file. Is there any other way i should do it?
Dim WithEvents Button As CommandButton
Private Sub Application_StartUp(Cancel As Boolean)
Set Button = CommandButtons.Add("Click me")
End SubPrivate Sub Button_Click()
Msgbox "Clicked", vbInformation
End Sub
Since it was mentioned that notepad is being used for this and looking at the code this certainly appears to be code to add a button to a Form Based application using VBScript in Visual Studio.
VBScript via notepad is not really good at being used for creating GUI dialog boxes and such. A sample code of what would work using Notepad is as such:
toRepeat = InputBox("What should I repeat?", "Echo", "Hello World")
MsgBox toRepeat
Whatever is typed in to the input box will be repeated in this example.
If Visual Studio is not available and there is an interest in building a form based application then it may be possible to create User Forms in VBA via Word or Excel. The following site gives a good tutorial for creating a User Form application in Excel: Excel VBA Userform - Easy Excel Macros
In case there is interest, I recently wrote in to my blog an explanation of the different flavors of VB that are available: VBA vs VBSCRIPT vs Visual Basic
Thanks,
Sean W.
I am using ruby a WIN32OLE to automate execution of Excel macros with windows scheduler.
I have all of this working properly. My problem comes with having to run a macro embedded in an excel file of an external party (ie I don't have control of the macro). I can call the macro with no problem, however the code contains user prompts (in the form of Msgbox statements)
In short how do I tell the msgbox "yes" or "ok" via WIN32OLE?
What I have tried
Event handling: The Msgbox does not throw a worksheet event. I would have to think that windows throws an event somewhere for the Msgbox (but I am not skilled enough to capture it with WIN32OLE)
-Threading and Sendkeys: I was also unable to get the msgbox to respond to sendkeys. More problematic was that I was unable to get the threading to work with the WIN32OLE object (it worked fine when I was using puts and sleeps to simulate the macro running.)
It appears I am stuck with copying the code into another workbook and removing the msgbox statments, I don't really want to do that because then it will add maintenance work everytime the business logic of the 3rd party code changes.
Here's one way,
install Autoit
and then do:
au3 = WIN32OLE.new("AutoItX3.Control")
au3.ControlClick("MessageBox Title",'', 'OK')
I guess prety much everyone who does a lot of debugging have a handy macro in Visual Studio (with shortcut to it on a toolbar) which when called automatically attaches to a particular process (identified by name).
it saves a lot of time rather than clicking "Debug" -> "Attach to the process ...", but it only works if one is running a single instance of the process one wants to attach to. If theres is more than one instance of particular process in memory - the first one (with a smaller PID?) is being choose by debugger.
Does anyone have a macro which shows a dialog (if more that 1 process with a specified name running) and lets developer to select to one he/she really wants to attach to.
I guess the selection could be made based on a windwow caption text (which would be suffice in most of cases) and when the particular instance is selected macro passes the PID of the process to the Debugger object?
If someone has that macro or knows how to write it - please share.
Thanks.
You can always attach to all instances...
Here is a macro I used when debugging asp.net applications - these typically have both a UI and a Webservice and I need to attach to both.
Sub AttachToAspNET()
Try
Dim process As EnvDTE.Process
Dim listProcess As New List(Of String)
listProcess.Add("aspnet_wp.exe")
listProcess.Add("w3wp.exe")
listProcess.Add("webdev.webserver.exe")
For Each process In DTE.Debugger.LocalProcesses
For Each procname As String In listProcess
If process.Name.ToLower.IndexOf(procname) <> -1 Then
process.Attach()
End If
Next
Next
ListDebuggedProcesses()
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub
I'm having an issue referencing public procedures of User Controls that I've created within a VB6 project.
A simple example (exe), I have a form with a button and a user control:
Option Explicit
Private Sub Command1_Click()
UserControl1.updateMessage ("TIME NOW: " & DateTime.Time)
End Sub
The User Control code is as follows:
Option Explicit
Public Sub updateMessage(ByVal newMessage As String)
Label1.Caption = newMessage
End Sub
This exe compiles and works fine, and when I'm typing updateMessage in the Form, it appears in the intellisense list with the appropriate requirements. The issue I have is when I'm wanting to "go to the definition" of updateMessage, instead of going to the appropriate section of the code within the User Control, the message always returns with:
"Cannot jump to 'updateMessage' because it is in the library 'Unknown1' which is not currently referenced."
where the numbered suffix of "Unkown1" changes from time to time.
It seems that if there were no reference to this procedure, then it would not appear in the intellisense and the project shouldn't compile. When running this with MZTools (though the error appears regardless of this plug-in being installed), I can go into the updateMessage procedure, and use it to find all procedures calling this function, so the link between the two should exist (although I'm not sure if MZTools just finds using a text-matching pattern).
If anyone out there could shed some light on this matter, it would be very much appreciated, and save this poor VB6 developer a lot of hassle!
I have SP6 installed (build 9782) of VB6 and am running XP SP3 on an HP dx2400.
Yes, this is extremely annoying and I'm convinced it's a bug in VB6. I say this because, if you locate the updateMessage method in the object browser and double-click on it, you are taken to the definition. So, the compiler actually does know where the definition is, it just refuses to take you there with Shift+F2.