+ Reply to Thread
Results 1 to 12 of 12

Converting vba into vb

  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:
    Please Login or Register  to view this content.
    Standard module:
    Please Login or Register  to view this content.
    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.

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

    Re: Converting vba into vb

    Bump......

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

    Re: Converting vba into vb

    Bump...... Day 2

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Converting vba into vb

    I'll have a look at this in the morning for you if noones helping you. It will be C# rather than vb though since I don't have it installed. I'll try to keep it as close to your code as possible though so you can follow it if that's ok?

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

    Re: Converting vba into vb

    Hi Kyle,

    Thanks for looking at this for me. I'm just going to go with an easy workaround from the MVP Orlando's site that converts it to an Exe. It isn't perfect but at least it should allow me to share it without much hassle. I will PM you because I have a question about doing this large-scale and whether or not it's worth it.

    Thanks again.

    abousetta

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Converting vba into vb

    No problems

    FWIW, the C# would look something like (whilst staying as close as possible to the original code):
    PHP Code: 
    namespace clicker
    {
        public 
    partial class Form1 Form
        
    {
            [
    DllImport("user32.dll")]
            public static 
    extern IntPtr FindWindow(String sClassNameString sAppName);
            [
    DllImport("user32.dll")]
            static 
    extern IntPtr FindWindowEx(IntPtr hwndParentint hwndChildAfterstring lpszClassstring lpszWindow);
            [
    DllImport("user32.dll")]
            static 
    extern IntPtr GetDesktopWindow();
            [
    DllImport("user32.dll")]
            static 
    extern IntPtr GetWindow(IntPtr hwndInt32 hwndtype);
            [
    DllImport("user32.dll")]
            public static 
    extern IntPtr SendMessage(IntPtr hWndUInt32 Msgint wParam, [OutStringBuilder lParam);
            [
    DllImport("user32.dll")]
            public static 
    extern IntPtr PostMessage(IntPtr hWndUInt32 Msgint wParamint lParam);
            [
    DllImport("user32.dll")]
            static 
    extern IntPtr ShowWindow(IntPtr hWndint wFlag);
            [
    DllImport("user32.dll"SetLastError trueCharSet CharSet.Auto)]
            public static 
    extern int GetWindowTextLength(IntPtr hWnd);

            private const 
    int WM_LBUTTONDOWN 0x201;
            private const 
    int WM_LBUTTONUP 0x202;
            private const 
    int GW_HWNDNEXT 2;
            private const 
    int GW_CHILD 5;

            private 
    Boolean cancelMe false;

            public 
    Form1()
            {
                
    InitializeComponent();
            }

            private 
    void UpdateEndNoteReferences()
            {
                
    //string ClassName = "#21770"; Not actually used
                
    string SearchName "Review Available Updates - ";
                
    cancelMe false;
                do
                {
                    
    IntPtr parentHandle FindWindowLike(GetDesktopWindow(), SearchName);

                    if (
    parentHandle.ToInt32() > 0)
                    {
                        
    Debug.WriteLine(WindowText(parentHandle));
                        
    IntPtr childHandle FindWindowEx(parentHandle0"Button","Update All Fields ->");
                        
    clickButton(childHandle);
                        
    childHandle FindWindowEx(parentHandle0"Button""Save Updates");
                        
    clickButton(childHandle);
                    }
                    
    Application.DoEvents();
                }
                while(
    cancelMe==false);
            }

            private 
    void clickButton(IntPtr hwnd)
            {
                
    PostMessage(hwndWM_LBUTTONDOWN00);
                
    PostMessage(hwndWM_LBUTTONUP00);
            }

            private 
    void button1_Click(object senderEventArgs e)
            {
                
    UpdateEndNoteReferences();
            }

            private 
    string WindowText(IntPtr hwnd)
            {
                const 
    int WM_GETTEXT 0x000D;
                
    int ln GetWindowTextLength(hwnd);
                if (
    ln 0)
                {
                    
    StringBuilder sb2 = new StringBuilder(ln 1);
                    
    SendMessage(hwndWM_GETTEXTsb2.Capacitysb2);
                    return 
    sb2.ToString();
                }
                return 
    "";
            }

            private 
    IntPtr FindWindowLike(IntPtr hwndstring Caption)
            {
                
    Regex regex = new Regex(".*" Caption ".*");
                
    IntPtr lhwnd GetWindow(hwndGW_CHILD);
                do
                {
                    if (
    regex.IsMatch(WindowText(lhwnd)))
                    {
                        return 
    lhwnd;
                    }
                    else
                    {
                        
    lhwnd GetWindow(lhwndGW_HWNDNEXT);
                    }
                }
                while (
    lhwnd.ToInt32() > 0);
                return 
    System.IntPtr.Zero;
            }

            private 
    void button2_Click(object senderEventArgs e)
            {
                
    cancelMe true;
            }
        }


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

    Re: Converting vba into vb

    Thanks Kyle. My ignorance of C sharp comes to light ... I put the code in txt file and changed the file extension to .exe but it didn't work (I tried a few more for fun but still no cigar). Am I even remotely on the right track?

    abousetta

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Converting vba into vb

    No, not even remotely

    You'd need a copy of Visual c# developer installed (the express version is free) it then needs compiling and turning into an application.

    I'll do it when I get a few mins if you like, the only issue that I obviously can't check the code

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

    Re: Converting vba into vb

    Thanks Kyle. I do have Visual C# express edition installed. I'll try to play with it over the weekend and see if I can get anywhere. It will be a learning experience.

    Thanks again.

    abousetta

    P.S. Now I owe you two reps

  10. #10
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Converting vba into vb

    haha no probs
    Attached Files Attached Files

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

    Re: Converting vba into vb

    Thanks. This works much faster than its original vba counterpart. Makes me wonder why? I guess C# is more optimized/ in sync with the windows API. Anyways, that's a thought to investigate on another day.

    abousetta

  12. #12
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Converting vba into vb

    It actually works? I figured it might want a bit of tweaking which is why I posted the code.

    c#/vb.net speeds are incomparable to VBA, they're much faster. They're compiled so they are much faster, VBA just runs in an Excel instance so it's not suprising really.

+ Reply to Thread

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