Results 1 to 12 of 12

Converting vba into vb

Threaded View

  1. #1
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Converting vba into vb

    Hello,

    I need some help converting the code below into a standalone visual basic exe file.

    Simply, the code loops until it finds a window that is generated by a program called "EndNote". Once it find this window, it clicks on two buttons in sequence, which will close the windows (of course others things will happen in EndNote but that's outside the scope of this vba code). In the attached file, there is also a "Cancel" button in a userform that allows the user to exit the program, and close the workbook.

    I want to share this with others, but don't want it to be hinged on having Excel installed, macros enabled, etc.

    Thanks in advance.

    abousetta

    ThisWorkbook:
    Option Explicit
    
    Private Sub Workbook_Open()
        CancelBtn.Show vbModeless
        Application.Visible = False
        Application.Run "Upate_EndNote_References"
    End Sub
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Application.Visible = True
        ThisWorkbook.Close
    End Sub
    Standard module:
    Option Explicit
    
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    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
    Declare Function GetDesktopWindow Lib "user32" () As Long
    Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdSHow As Long) As Long
    Const WM_LBUTTONDOWN = &H201
    Const WM_LBUTTONUP = &H202
    Public CancelMe As Boolean
    
    Private Sub Upate_EndNote_References()
      Dim parent_handle&, child_handle&, ClassName$, SearchName$
      ' Don't stop till the user clicks on the stop button
        CancelBtn.Show vbModeless
        CancelMe = False
      ' Window information
        ClassName = "#32770"
        SearchName = "Review Available Updates - "
      ' Click on the update all fields buttons and save the changes
        DoEvents
        Do While CancelMe = False
      ' Find the correct window
        parent_handle = FindWindowLike(GetDesktopWindow(), SearchName, ClassName)
            DoEvents: If CancelMe = True Then Exit Do
            If parent_handle <> 0 Then
            Debug.Print WindowText(parent_handle)
              ' Click the Update All Fields button
                child_handle = FindWindowEx(parent_handle, 0, "Button", "Update All Fields ->")
                Click_Button (child_handle)
              ' Click the Save Updates button
                child_handle = FindWindowEx(parent_handle, 0, "Button", "Save Updates")
                Click_Button (child_handle)
            End If
            'Application.Wait Now() + TimeValue("00:00:02")
        Loop
        Unload CancelBtn
        MsgBox "Task completed...", vbInformation
        Application.Quit
    End Sub
    
    Private Sub Click_Button(child_handle As Long)
        DoEvents: If CancelMe = True Then Exit Sub
        Call PostMessage(child_handle, WM_LBUTTONDOWN, 0, 0)
        Call PostMessage(child_handle, WM_LBUTTONUP, 0, 0)
    End Sub
    
    Function FindWindowLike(hWndParent As Long, Caption As String, ClassName As String) As Long
      Dim hWnd&
      Const GW_HWNDNEXT = 2, GW_CHILD = 5
        DoEvents: If CancelMe = True Then Exit Function
      ' Find window using a like function
        hWnd = GetWindow(hWndParent, GW_CHILD)
        Do Until hWnd = 0
            If WindowText(hWnd) Like "*" & Caption & "*" Then
                FindWindowLike = hWnd
                Exit Do
            End If
            hWnd = GetWindow(hWnd, GW_HWNDNEXT)
        Loop
    End Function
    
    Function WindowText(hWnd As Long) As String
      Dim lng&, str$
      Const WM_GETTEXT = &HD, WM_GETTEXTLENGTH = &HE
        DoEvents: If CancelMe = True Then Exit Function
      ' Convert ID to text
        If hWnd <> 0 Then
            lng = SendMessage(hWnd, WM_GETTEXTLENGTH, 0&, 0&) + 1
            If lng > 0 Then
                str = String$(lng, vbNullChar)
                lng = SendMessage(hWnd, WM_GETTEXT, lng, ByVal str)
                If lng > 0 Then WindowText = Left$(str, lng)
            End If
        End If
    End Function
    
    Function apicShowWindow(strClassName As String, strWindowName As String, lngState As Long)
      Dim lngWnd&, intRet%
        DoEvents: If CancelMe = True Then Exit Function
      ' Use Findwindow and ShowWindow together to manipulate window
        lngWnd = FindWindow(strClassName, strWindowName)
        apicShowWindow = ShowWindow(lngWnd, lngState)
    End Function
    Attached Files Attached Files
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1