marciovieira

Urgent need to convert the VBA code to Autoit code

18 posts in this topic

Hello friends!

Urgent need to convert the code in VBA that I posted to AutoIt, is the only solution I found, I'm "catching" a lot to understand how to use user32.dll functions as yet had no need to use them, but now I have to learn anyway, the continuation of my script depends solely on it.
 
Thank you so much
 

Márcio.

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                                    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
                                      (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
                                       ByVal lpsz2 As String) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
                                     (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public Declare Function GetWindow Lib "user32" ( _
                                  ByVal hwnd As Long, _
                                  ByVal wCmd As Long _
                                  ) As Long

Public Declare Function GetWindowPlacement Lib "user32" _
                                           (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

Public Declare Function SetWindowPlacement Lib "user32" _
                                           (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

Public Declare Function SetForegroundWindow Lib "user32" _
                                            (ByVal hwnd As Long) As Long

Public Declare Function BringWindowToTop Lib "user32" _
                                         (ByVal hwnd As Long) As Long

Public Declare Function GetForegroundWindow Lib "user32" _
                                            () As Long

Const WM_SETTEXT As Long = &HC
Const BM_CLICK = &HF5
Const GW_CHILD = 5
Const GW_HWNDNEXT = 2

Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type

Dim Ret As Long, OpenRet As Long, FlDwndHwnd As Long
Dim ChildRet As Long
Dim pos As RECT

Const SW_SHOWNORMAL = 1
Const SW_SHOWMINIMIZED = 2

Public Type POINTAPI
    X As Long
    Y As Long
End Type

Public Type WINDOWPLACEMENT
    Length As Long
    flags As Long
    showCmd As Long
    ptMinPosition As POINTAPI
    ptMaxPosition As POINTAPI
    rcNormalPosition As RECT
End Type

'==> Use this if you want to specify your own name in the 'Save As'-Dialog
Const FileSaveAsName = "C:\tmp\MyFile6.xls"

Private Function ActivateWindow(xhWnd&) As Boolean
    Dim Result&, WndPlcmt As WINDOWPLACEMENT

    With WndPlcmt
        .Length = Len(WndPlcmt)
        Result = GetWindowPlacement(xhWnd, WndPlcmt)
        If Result Then
            If .showCmd = SW_SHOWMINIMIZED Then
                .flags = 0
                .showCmd = SW_SHOWNORMAL
                Result = SetWindowPlacement(xhWnd, WndPlcmt)
            Else
                Call SetForegroundWindow(xhWnd)
                Result = BringWindowToTop(xhWnd)
            End If
            If Result Then ActivateWindow = True
        End If
    End With
End Function

Private Function DeActivateWindow(xhWnd&) As Boolean
    Dim Result&, WndPlcmt As WINDOWPLACEMENT

    With WndPlcmt
        .Length = Len(WndPlcmt)
        Result = GetWindowPlacement(xhWnd, WndPlcmt)
        If Result Then
            .flags = 0
            .showCmd = SW_SHOWMINIMIZED
            Result = SetWindowPlacement(xhWnd, WndPlcmt)
            If Result Then DeActivateWindow = True
        End If
    End With
End Function

Sub SendMess(Message As String, hwnd As Long)
    Call SendMessage(hwnd, WM_SETTEXT, False, ByVal Message)
End Sub


Private Sub Auto_SaveAs_SAP()

    On Error GoTo err_handler

    '******************************************************************************************************************
    '*                                                                                                                *
    '* Automatic 'Save as' dialog from SAP => fillin SaveAsFileName and press 'Save'                                  *
    '*                                                                                                                *
    '******************************************************************************************************************

    Ret = FindWindow("#32770", "Salvar como")

    If Ret = 0 Then
        MsgBox "Save As Window Not Found"
        Exit Sub
    End If

    '==> Get the handle of  ComboBoxEx32
    ChildRet = FindWindowEx(Ret, ByVal 0&, "ComboBoxEx32", "")
    If ChildRet = 0 Then
        MsgBox "ComboBoxEx32 Not Found"
        Exit Sub
    End If

    '==> Get the handle of the Main ComboBox
    ChildRet = FindWindowEx(ChildRet, ByVal 0&, "ComboBox", "")

    If ChildRet = 0 Then
        MsgBox "ComboBox Window Not Found"
        Exit Sub
    End If

    '==> Get the handle of the Edit
    ChildRet = FindWindowEx(ChildRet, ByVal 0&, "Edit", "")

    If ChildRet = 0 Then
        MsgBox "Edit Window Not Found"
        Exit Sub
    End If

    ActivateWindow (Ret)

    '==> fillin FileName in 'Save As' Edit
    DoEvents
    SendMess FileSaveAsName, ChildRet

    '==> Get the handle of the Save Button in the Save As Dialog Box
    ChildRet = FindWindowEx(Ret, ByVal 0&, ByVal "Button", ByVal "Ab&rir como somente leitura")
    ChildRet = GetWindow(ChildRet, GW_HWNDNEXT)    ' This will be handle of '&Save'-Button

    '==> Check if we found it or not
    If ChildRet = 0 Then
        MsgBox "Save Button in Save As Window Not Found"
        Exit Sub
    End If

    '==> press Save-button
    SendMessage ChildRet, BM_CLICK, 0, ByVal 0&

    Exit Sub
err_handler:
    MsgBox Err.Description
End Sub

 

Share this post


Link to post
Share on other sites



Can't tell if you're looking for the WinApi UDF calls or these functions in AutoIt function calls.

Here are the WinAPI UDF

_WinApi_FindWindow
_SendMessage
_WinAPI_GetWindow
_WinAPI_GetWindowPlacement
_WinAPI_SetWindowPlacement
_WinAPI_SetForegroundWindow
_WinAPI_BringWindowToTop
_WinAPI_GetForegroundWindow

AutoIt

WinGetHandle
WinGetPos
WinMove
WinActivate
WinGetHandle("[Active]")

 

Share this post


Link to post
Share on other sites
2 minutes ago, InunoTaishou said:

Can't tell if you're looking for the WinApi UDF calls or these functions in AutoIt function calls.

Here are the WinAPI UDF

_WinApi_FindWindow
_SendMessage
_WinAPI_GetWindow
_WinAPI_GetWindowPlacement
_WinAPI_SetWindowPlacement
_WinAPI_SetForegroundWindow
_WinAPI_BringWindowToTop
_WinAPI_GetForegroundWindow

AutoIt

WinGetHandle
WinGetPos
WinMove
WinActivate
WinGetHandle("[Active]")

 

Hello Inuno Taishou


I've tried to use the basic functions of AutoIt, but do not work, have to be the _WinAPI UDFs functions, but does not have the AutoIt UDF function that loads the User32.dll ("FindWindowExA").
 

Thank you for helping.


Márcio.

Share this post


Link to post
Share on other sites

Hi marciovieira, welcome to the forum.

It's not always the best approach, to convert VBA to AutoIt, especially as VBA is Office concentric. Neither is it always best for just VB.

The better approach generally, is to describe what it is you are wishing to achieve with the code ... the end result.

If you do that and provide some of your AutoIt code, then you are likely to get far more help, as it won't just rely on those well versed with VB/VBA.

In many cases anyway, AutoIt has its own way (often better) to achieve what you are wanting to do.

We also have UDF's here to help with Word and Excel, etc.

 


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites
3 hours ago, TheSaint said:

We also have UDF's here to help with Word and Excel, etc.

To automate SAP there is a UDF available as well.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Func _WinAPI_FindWindowEx($hwndParent, $hwndChildAfter, $sClassName, $sWindowName)
    Local $aResult = DllCall("user32.dll", "hwnd", "FindWindowExW", "hwnd", $hwndParent, "hwnd", $hwndChildAfter, "wstr", $sClassName, "wstr", $sWindowName)
    If @error Then Return SetError(@error, @extended, 0)

    Return $aResult[0]
EndFunc   ;==>_WinAPI_FindWindowWx

 


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
21 hours ago, InunoTaishou said:
16 hours ago, JohnOne said:
Func _WinAPI_FindWindowEx($hwndParent, $hwndChildAfter, $sClassName, $sWindowName)
    Local $aResult = DllCall("user32.dll", "hwnd", "FindWindowExW", "hwnd", $hwndParent, "hwnd", $hwndChildAfter, "wstr", $sClassName, "wstr", $sWindowName)
    If @error Then Return SetError(@error, @extended, 0)

    Return $aResult[0]
EndFunc   ;==>_WinAPI_FindWindowWx

 

Hello John One!


You could you please post an example showing the parameters that I use to call the function _WinAPI_FindWindowEx that you posted?


Thank you so much!


Márcio.

 

Share this post


Link to post
Share on other sites
18 hours ago, water said:

To automate SAP there is a UDF available as well.

Hello water!


I already use some functions of SAP UDF that I found here, my problem is when I try to save a worksheet and the SAP opens the Save As window, I can not access it through the functions AutoIt ControlSetText () and ControlClick (), but when I use the code I posted in VBA works perfectly.


Thank you.


Márcio.

 

Share this post


Link to post
Share on other sites
21 hours ago, TheSaint said:

Hi marciovieira, welcome to the forum.

It's not always the best approach, to convert VBA to AutoIt, especially as VBA is Office concentric. Neither is it always best for just VB.

The better approach generally, is to describe what it is you are wishing to achieve with the code ... the end result.

If you do that and provide some of your AutoIt code, then you are likely to get far more help, as it won't just rely on those well versed with VB/VBA.

In many cases anyway, AutoIt has its own way (often better) to achieve what you are wanting to do.

We also have UDF's here to help with Word and Excel, etc.

 

Hello TheSaint!

When I posted the code in VBA thought it was easy, because the code is exactly what I need to access a single window through the User32.dll that is one of the most popular windows "Save As".
 
Thank you so much!

Márcio.

Share this post


Link to post
Share on other sites

You haven't made things much clearer, other than it seems you are wanting to access the "Save As" dialog.

VBA = Visual Basic for Applications.

Which Application are you using? Excel, Word, etc.

Regardless, you must have a reason you are wanting to access that dialog with AutoIt, rather than just with VBA code?

To my mind, you should ignore your VBA code altogether and just use what AutoIt provides ... or use one of the Office UDF's available here, to run your existing VBA code.

But really we need a better explanation than you have given thus far ... an overall picture of things from start to finish.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

28 minutes ago, TheSaint said:

You haven't made things much clearer, other than it seems you are wanting to access the "Save As" dialog.

VBA = Visual Basic for Applications.

Which Application are you using? Excel, Word, etc.

Regardless, you must have a reason you are wanting to access that dialog with AutoIt, rather than just with VBA code?

To my mind, you should ignore your VBA code altogether and just use what AutoIt provides ... or use one of the Office UDF's available here, to run your existing VBA code.

But really we need a better explanation than you have given thus far ... an overall picture of things from start to finish.

Okay friend!

The application in question is the "SAP GUI Scripting" that allows the use of AutoItScript or VBScript, however when you click a command button to save a workshet the window "Save As" opens and I can not access it through AutoIt functions ControlSetText () and ControlClick (), and search the specific forum for the "SAP GUI Scripting" found a post mentioning that only through User32.dll would be possible to access the window "Save As".

Thank you.

Márcio.


Follow the solution links found in the specific forum for the "SAP GUI Scripting":
http://scn.sap.com/message/8219341#8219341

https://scn.sap.com/thread/3361288

Edited by marciovieira

Share this post


Link to post
Share on other sites

_Excel_BookSaveAs is the answer.

Check the examples for the named function in the Au3 help file .

Share this post


Link to post
Share on other sites

I guess I am out of my league, as I have never heard of "SAP GUI Scripting" and neither of the links you posted will work for me ... perhaps they are zone or membership related.

I had a quick look at - http://scn.sap.com/community/gui/blog/2012/10/08/introduction-to-sap-gui-scripting

water is the Office Guru here, and he appears to be familiar with SAP, so he is probably your best bet.

So I suggest you wait until he is available to respond to this topic again ... unless someone else with the required knowledge jumps in first.

P.S. I use VBA regularly, but AutoIt much more. I'm not sure if you meant VBS instead of VBA, which I am only passingly familiar with.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

If getting a handle to your apps save as dialog is the goal then FindWindowEx is probably not what you need.

Something like the following might be more suited.

#include <WinAPI.au3>

Run("Notepad")

$hwndTarget = WinWaitActive("Untitled - Notepad")

Send("^s")

WinWaitActive("Save As")

$aList = WinList("Save As")

; look for all Save As windows
; and if the parent of it is our target app then close it
For $i = 1 To $aList[0][0]
    If _WinAPI_GetParent($aList[$i][1]) = $hwndTarget Then
        MsgBox(0, "Found", "Closing")
        WinClose($aList[$i][1])
        Exit
    EndIf
Next

MsgBox(0, "Not Found", "Nothing to close")

 

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
9 hours ago, JohnOne said:

If getting a handle to your apps save as dialog is the goal then FindWindowEx is probably not what you need.

Something like the following might be more suited.

#include <WinAPI.au3>

Run("Notepad")

$hwndTarget = WinWaitActive("Untitled - Notepad")

Send("^s")

WinWaitActive("Save As")

$aList = WinList("Save As")

; look for all Save As windows
; and if the parent of it is our target app then close it
For $i = 1 To $aList[0][0]
    If _WinAPI_GetParent($aList[$i][1]) = $hwndTarget Then
        MsgBox(0, "Found", "Closing")
        WinClose($aList[$i][1])
        Exit
    EndIf
Next

MsgBox(0, "Not Found", "Nothing to close")

 

Hello John One!


I could see that the "Save As" window is independent of the application open, seems to be a native instance of Windows, I can not access it through the functions AutoIt WinWaitActive, even through the Handle obtained by WinList function, follows the image window (My Windows language is Portuguese Brazilian).


Thank you so much.


Márcio.

SaveAs002.jpg

Share this post


Link to post
Share on other sites
13 hours ago, TheSaint said:

I guess I am out of my league, as I have never heard of "SAP GUI Scripting" and neither of the links you posted will work for me ... perhaps they are zone or membership related.

I had a quick look at - http://scn.sap.com/community/gui/blog/2012/10/08/introduction-to-sap-gui-scripting

water is the Office Guru here, and he appears to be familiar with SAP, so he is probably your best bet.

So I suggest you wait until he is available to respond to this topic again ... unless someone else with the required knowledge jumps in first.

P.S. I use VBA regularly, but AutoIt much more. I'm not sure if you meant VBS instead of VBA, which I am only passingly familiar with.

Hello, The Saint!


Now that you know my problem and revealed that frequently uses VBA and AutoIt, why do not you convert code I posted in VBA to AutoIt, you do not need to understand anything "SAP GUI Scripting" to do so, and I'll be very grateful for your help.
 

Thank you.


Márcio.

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Hello Márcio.

Most of what I said earlier still applies.

I have no intention of converting VBA to AutoIt ... or even VB to AutoIt, which would be more feasible. I use AutoIt rather than VB, because I don't really like VB, and avoid it as much as I can.

It is my view, that you ignore the VBA and just use AutoIt. Others above have steered you in what seems to be the right directions.

I gather you are working with an XLS file, so I would investigate the Excel UDF, that is in the AutoIt Help file.

You still haven't really explained why you are doing things the way you are. We need to see the bigger picture, so we can advise you appropriately ... especially if what JohnOne etc has provided you isn't enough.

We really need to see what AutoIt code you have come up with so far. We can't assess your skill level or what mistakes you may be making or even gain an understanding of you aim, without that.

We are not a conversion service here. We help others to help themselves.

P.S. We don't provide you with free fish, we teach you how to fish, so that you can provide for yourself.

 

Edited by TheSaint

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites
Hello community!

I would like to thank the attention and patience of all, I obtained the necessary help in the specific forum on "SAP GUI Scripting", below is the link of the perfect, fast and efficient solution ...

... Sure with this percar stick I will catch many fish!

http://scn.sap.com/community/scripting-languages/blog/2016/02/29/control-saveas-dialog-with-autoit#comment-654360

Thank you all.

Márcio.

Share this post


Link to post
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