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')
Related
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 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.
Bizarre problem.
I am using Excel VBA to control IE and download data from web sites. Pretty vanilla.
I experience an error that only happens when I am running Windows 8.1 and IE 10. On a second PC, which is running Windows 7 and IE 8, the code works fine.
Here is the behavior.
I call the following code to open a web page:
Set appIE = CreateObject("InternetExplorer.Application")
With appIE
.navigate sURL 'Where sURL is a valid web address
End With
This works fine, and opens a browser.
Shortly afterwards, I call the following:
With appIE
.navigate sURL'a different valid web address
End With
An error message pops up at the .navigate line. The message says:
Run-time Error '-2147024726(800700aa)'
Method 'Navigate' of object 'IEWebBrowser2' Failed
I have looked up this error message and the only thing that I can find is that it may relate to spyware. But I completely reinstalled the OS and all apps on the system and the same thing happened.
Another very strange phenomenon is that if I put a trace point in-between the above two sections and pause the code, then manually start the code again, everything works fine. But if, instead of me putting in a trace point I put in a wait loop, I get the same failure. That is, if I manually pause and then resume the code, the code works, but if I automate a pause and resume, the code does not work.
As you consider this situation, please remember that everything works fine under Windows 7 and IE 8, and everything works fine if I manually pause and restart.
Does any of this make sense?
1- Please Check sURL (if you check, please check again compiled folder)
2- Delete WebBrowser Control and Add New
Me forget copy original file App.Path + "\error.html" and see this problem.
I've got a long-running macro, in which I write periodic messages to Output just so that the user know somethings happening. The problem is that the output doesn't get shown until the macro has finished (unless you have the Continue Waiting? dialog box open, strangely enough).
Is there anyway to 'flush' the event queue?
If you reference Windows Forms you can access Application.DoEvents as normal.
I have the main IDE responding to windows updates and closes in real time while the macro is still running with only
DTE.SuppressUI = False
Threading.Thread.Sleep(5000)
As such I suggest using a standard DoEvents & Sleep(55) loop for a 250 milliseconds or so when you want to ensure the UI is updated should do the trick, along with the SuppressUI=False.
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.