Jump to content

Error using Autoit through VBA


Davgop
 Share

Recommended Posts

@TheXman - Apologize for the misunderstanding. I have run the script file mentioned in the below path and both works fine.

image.png.0f53298eebbad9fc6e6b0dc0e14f197b.png

Just to let you know I followed the instructions provided in this link and I able to attain my results with below code. I am not sure if i could access this executable script file using VBA.

image.png.453039fc96c7a36b8e1d04952bae05d4.png

 

Link to comment
Share on other sites

I used the following syntax in Excel, and it ran fine.  If you are using a 32bit version of excel, then you need to add the 32bit autoitx3.dll in Tools|References.  Likewise, if you have 64bit version of excel, you need to add the 64bit version.  As you can see, there are slight differences between the way you implement AutoItX3 in pure VBScript and VBA (VBScript for Applications).

image.thumb.png.f7919479bea40c02db357383affd0ceb.png

Edited by TheXman
Link to comment
Share on other sites

@TheXman - Thanks a lot for your assistance and patience, with the code you supplied I am able to open notepad and maximize it.
So I tried to type something in the notepad and click CTRL+P, however it all happens in the VBA editor window.
So I think we activate the notepad window, could you please help me.

 

Sub Test()
Dim oAutoit As AutoItX3
Set oAutoit = New AutoItX3
oAutoit.Run "Notepad.exe", "", oAutoit.SW_MAXIMIZE
oAutoit.Send ("Hi")
oAutoit.Send ("^p")
End Sub

image.png.d880b7259597dd59f73ad75912a1def5.png

Link to comment
Share on other sites

You're welcome! :thumbsup:

Why don't you lookup the WinActivate function in the AutoItX help file and try to implement it yourself first.  If you fail, then post your code and I will show you why it failed.  Pay close attention to the examples in the help file, especially the one for WinActivate().  It specifically references Notepad.

Edited by TheXman
Link to comment
Share on other sites

@TheXman - With the below code I am able to open notepad, type "HI" and Print it.
However I did the same in my actual code but still it does not work. I am not getting any error the code runs fine but the Print window is not popping up.
 

Sub Test()
Dim oAutoit As AutoItX3
Set oAutoit = New AutoItX3
oAutoit.Run "Notepad.exe", "", oAutoit.SW_MAXIMIZE
oAutoit.WinActivate "Untitled - Notepad"
oAutoit.Send ("Hi")
oAutoit.Send ("^p")
oAutoit.WinActivate "Print", "Print"
oAutoit.Send ("!p")
End Sub


Actual code:

Sub Tessst()
Dim bot As New WebDriver, keys As New Selenium.keys
bot.Start "chrome"
bot.Get "https://google.com"
Dim oAutoit As AutoItX3
Set oAutoit = New AutoItX3
oAutoit.WinActivate "Google - Google Chrome", "Chrome Legacy Window"
oAutoit.Send ("^p")
End Sub

 

Link to comment
Share on other sites

My first guess is that you are not using the correct title parameter for your WinActivate.  For the record, I would leave off the optional "Text" parameter unless it is necessary.  It also could be a timing issue if the chrome window isn't fully initialized and ready before you try to activate it.  You could look into using a WinWaitActive and/or Sleep to make sure that your windows are actually active and ready for input. 

I'm leary of continuing to assist you when I see the terms like "bot" in your script.  Can you explain what your ultimate goal is with this script of yours?  I have no desire to help "Gamers" or "Script Kiddies" with their projects.  If you are one of those, I'll leave it to someone else to help you, if they choose to.

Edited by TheXman
Link to comment
Share on other sites

What I think we're looking for in regards to your ultimate goal is less of an exact plan of what you want, like login to _x_ site with username and password, but more of an overall scope... like download an invoice or record your timesheet. It's not that we don't want to help, we just don't want to help you break the rules of our forums :)

It looks like your code is attempting to activate the window, but you start sending keys before it's active. Try using WinWaitActive with the same parameters as your WinActivate calls right after to delay your sending of keys correctly. For a save window, I do this...

WinActivate("Save As...", "")
If WinWaitActive("Save As...", "", 5) Then
    ; Change the file's name
    ControlSetText("Save As...", "", 1, "C:\temp.txt")
Else
    Exit MsgBox(0, "Error", "Unable to get Save As window!")
EndIf
Edited by seadoggie01
TheXman is smarter than me. I forget about the timeout option

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

I would strongly suggest that you use the "timeout" parameter with WinWaitActive(). Without it, you could easily get into a situation where your script hangs indefinitely because the specified title doesn't exist.  For example, if you made a typo in the title name or the application changed what you expected the title to be, then your script will hang until it is killed.

A simple example:

Sub test()
    Dim oAutoIt As AutoItX3
    
    Set oAutoIt = New AutoItX3
    
    'Run Notepad
    oAutoIt.Run "Notepad.exe", "", oAutoIt.SW_SHOW
    
    'Make sure that it is active and send keys to it
    oAutoIt.WinActivate "Untitled - Notepad"
    If oAutoIt.WinWaitActive("Untitled - Notepad", "", 3) Then
        oAutoIt.Send "Hello World{!}"
        oAutoIt.Send "^p"
    End If
End Sub

 

Edited by TheXman
Link to comment
Share on other sites

@TheXman - I am really not one of those guys. Honestly I do not have any experience in programming or automation, the code I used is from this website I think I can change the string bot into any desired name as it is a declaration (see below code as I have changed the same).
Purely my intention is to automate a manual task, My role is a Team manager and this automation is my added skill.
The reason why I am using "google.com" is because I cannot share the URL as I stated earlier. Please do let me know what should I do to prove that I am not a unethical person

  So I tried to .wait option of VBA, but still no luck.

Sub Testt()
Dim mate As New WebDriver, keys As New Selenium.keys
mate.Start "chrome"
mate.Get "https://google.com"
Application.Wait (Now() + TimeValue("00:00:03"))
Dim oAutoit As AutoItX3
Set oAutoit = New AutoItX3
oAutoit.WinActivate "Google - Google Chrome"
Application.Wait (Now() + TimeValue("00:00:03"))
oAutoit.Send ("^p")
End Sub


Also with the help of the below code based on this thread I am able to open browser click CTRL+P and the print window pop up. However unable to click enter.
 

Sub Teset()
Dim mate As New WebDriver, keys As New Selenium.keys
mate.Start "chrome"
mate.Get "https://google.com"
Application.Wait (Now() + TimeValue("00:00:03"))
Call Autoit
End Sub


Sub Autoit()
Dim AutoItPath
Dim FileName As String
Dim FileName1 As String
FileName = "C:\AP\Praise.au3"
AutoItPath = "C:\Program Files (x86)\AutoIt3\AutoIt3.exe "
FileName1 = """" & AutoItPath & """" & " " & """" & FileName & """"
runScript = Shell(FileName1)
End Sub

image.png.0fd1e5aaf4eed5e70be5c77370356e8b.png

Link to comment
Share on other sites

@seadoggie01 @TheXman - Thanks for your suggestion, based on you guidance. I have tried the below code but still no luck.  
 

Sub Tessst()
Dim mate As New WebDriver, keys As New Selenium.keys
mate.Start "chrome"
mate.Get "https://google.com"
Application.Wait (Now() + TimeValue("00:00:03"))
Dim oAutoit As AutoItX3
Set oAutoit = New AutoItX3
oAutoit.WinActivate "Google - Google Chrome"
Application.Wait (Now() + TimeValue("00:00:03"))
If oAutoit.WinWaitActive("Google - Google Chrome", "", 3) Then
oAutoit.Send ("^p")
Else
MsgBox "Window not found"
End If
End Sub

 

Link to comment
Share on other sites

1 hour ago, Davgop said:

Please do let me know what should I do to prove that I am not a unethical person

Please do not take this the wrong way.  I'm not trying to be nasty, just straight forward and factual.  You don't have to prove anything to me unless you want my continued assistance.  If so, then you can start by answering my previous question as to what you are trying to accomplish.  Just saying that you are "trying to automate a manual task" is not good enough and seems evasive. 

This piecemeal approach to getting to your ultimate solution is a bit frustrating.  If I had an idea of what it is your trying to achieve, then I could probably be much more helpful and give my opinion as to whether you are going about it in an effective way.  AutoItX3 is only a subset of the functionality that you would have available to you in an actual AutoIt script.  Up to now, I'm still trying to figure out why you are trying to embed AutoIt logic in an Excel macro when neither your Excel macro nor the description of your goal has anything to do with Excel or Excel data.  There may be really good reasons for doing it that way.  I just haven't read it yet.

Lastly, I haven't figured out whether you are truly trying to learn the language or whether you are more interested in just getting the solution.  If it's the former, then I am always willing to help.  If it's the latter, then I will leave it to others to feed you solutions.  There are many other people on this forum that seem to be willing and able to do that.

Link to comment
Share on other sites

@TheXman - Yes I would love to learn the language. I was a bit frustrated about the questions and that was the reason.

Let me tell you the whole history we do work for claims industry our job is to consolidate evidences from different portal to prove that the claim is genuine. The reason I'm using Excel is as we have different portals and this has to be done repeatedly for all customers, we have data for each customer in the excel file to fetch the exact data from the portals. So i have 500 customers data and created FOR LOOP so once it downloads first customer then it moves to the next.

Link to comment
Share on other sites

So the process of pulling all of this information from the different portals and placing it Excel already exists?  And all you are trying to add is the printing of each Excel workbook?  Or are you trying to create this whole process?  If you are trying to create the whole process, and you are interested in doing it with AutoIt, then I would suggest doing it in an AutoIt script as opposed to a macro in a spreadsheet.  There's a very useful  Excel UDF that can help with any of the Excel-related functionality.  Plus, AutoIt can easily scrape or pull data from web resources like websites, web services, FTP servers, etc.

Edited by TheXman
Link to comment
Share on other sites

@TheXman - No in excel we only have data of customer. From the portal we will download evidences in a pdf version. So once all the evidences downloaded for a customer then it will be moved from downloads folder to local folder with customer id as the folder name. Once this done it will move to the next customer.

So to visit, login, navigate, and click print in each portal I use selenium web driver.

Link to comment
Share on other sites

Okay, now we are getting somewhere.  :)  Since it appears Selenium has a COM interface, I guess the next question is whether you want to stick with Selenium for your browser automation or whether you want to do all of the automation (browser & application) using just AutoIt.  AutoIt has the ability to communicate with COM interfaces that utilize the iDispatch interface.  That means that you may be able to use Selenium in AutoIt instead of AutoIt in Excel.  Since this is an AutoIt forum, I think you can guess which way might get you the most help here on this forum.  :)

I must admit that this is a rather ambitious project for someone who is, admittedly, "very new to programming".  However, if you are truly interested in learning AutoIt, it could be a very good one to start with since it will expose you to many different subjects (browser automation, app automation, file handling, general coding principles like looping, string handling, boolean logic, etc).

Edited by TheXman
Link to comment
Share on other sites

@TheXman - Apologizes for the delay in reply, I would really love to learn Autoit, however I need excel for this project because this is a repetitive task we need to store the details of the customers whom we have worked and not. so as the Loop runs for each customer I have added status of the fetch in the adjacent column (like- Document saved, Document not found, Document not relevant) and after the Loop is finished the same data will be exported to an Email and a copy of the sheet will be saved for future references.

Link to comment
Share on other sites

I understood that you needed Excel because that is where the customer information is kept.  My question was not whether you wanted to get rid of Excel.  My question was whether you wanted to do all of the processing in AutoIt or whether you wanted to continue to use Excel macros (VBScript) with AutoItX to try to do the processing?  AutoIt can work with Excel workbooks.  AutoIt can do most, if not all, of the things you are using Selenium to do.  AutoIt can also do the file processing and the rest of the tasks you have said that you would like to do.  So my question was, or should I say suggestion, is to do it all with AutoIt because this is an AutoIt forum and not an Excel/VBScript forum.  The choice is yours.  In any case, when you start your coding and run into AutoIt/AutoItX issues, post your code, describe the issue, describe the expected outcome, and I'm sure we can help you get past the issue so you can continue on with your project.

 

Link to comment
Share on other sites

i will try to help if you share code, i just need to re-figure out how to get into the editor in 365, lol haven't touched this stuff in literally ages

this probably would be easier to do than in vbscript using just AutoIt and the excellent Excel UDFs that exist already. tons of code to get you started too in the threads. I personally think AutoIt is easier to program in.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

@TheXman - Thanks for the detailed explanation, let me explain the end to end project process.
So you let me know if Autoit can help me completely.

1. Open, Login and navigate to a credentials portal (in-house built) and get all User credentials of the portals to checked
2. Segregate the customer for whom we need to check the supporting docs
3  Initiate the Loop to Open, Login, Find the customer, Check for relevant Document
4. If document not present then make a note of it against the customer
5. If document present then do CTRL+P and save the document
6. Then move the document to the a specified folder in a local drive and restart the loop
7. Post completion of Loop save the data (Customer checked and the document fetched status as Excel file)
8. Send an a Email with a default template.

Thing need to be noted:-
What will happen if the code encounters an error in the middle, will the data which was run until then can it go to step 6,7,8?
How to initiate the process?
When the process is running can the system be used (whether the user can use other application, like excel, word, browser, outlook and etc..)?
 

Link to comment
Share on other sites

@Earthshine - Appreciate your time and effort, below is the code.
 

Sub Testt()
Dim mate As New WebDriver, keys As New Selenium.keys
mate.Start "chrome"
mate.Get "https://google.com"
Application.Wait (Now() + TimeValue("00:00:03"))
Dim oAutoit As AutoItX3
Set oAutoit = New AutoItX3
oAutoit.WinActivate "Google - Google Chrome"
Application.Wait (Now() + TimeValue("00:00:03"))
oAutoit.Send ("^p")
End Sub
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...