From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Change focus to excel password pop-up

This should be easy but I haven't figured it out yet. I have an application that opens a password protected excel file for reading. Before the file opens the program front panel is minimized. Excel opens but it is not in focus. I must click on the password entry box before I can enter the password. Is there a way to programmatically set the focus to excel? I am running LabVIEW 2019 sp1.

0 Kudos
Message 1 of 8
(1,679 Views)

You'd have to use FindWindowA to get a hWindow. Then use that in a call to SetWindowPos, with hWindInsertAfter set to HWND_TOPMOST and then again with HWND_NOTOPMOST.

 

EDIT: You probably have to bring the window to front, and then find the text entry in that window and give it focus. That might involve sending the main window or the text window message...

 

See for instance how-to-bring-other-app-window-to-front-without-activating-it on SO.

 

IIRC, these very (very) old libraries might have those functions. For 64 bit, you'd need to make some minor adjustments.

0 Kudos
Message 2 of 8
(1,640 Views)

So there's nothing native in LabVIEW to do this? I have to use calls to c++ code? I'm just an old retired guy helping out a non-profit. I wrote LabVIEW code for years and really enjoy it on occasion. Although I'm sure it's easy once learned, I'm not really interested in going down the path of embedding c calls in my LabVIEW code. I would rather go camping. (Being retired is great!)

 

Any other suggestions? It's easy enough to just click on the excel window and enter the password. I'll probably just continue with that solution.

0 Kudos
Message 3 of 8
(1,623 Views)

@ColoradoBob wrote:

So there's nothing native in LabVIEW to do this? I have to use calls to c++ code? I'm just an old retired guy helping out a non-profit. I wrote LabVIEW code for years and really enjoy it on occasion. Although I'm sure it's easy once learned, I'm not really interested in going down the path of embedding c calls in my LabVIEW code. I would rather go camping. (Being retired is great!)


API calls are native to LabVIEW.

 

There's nothing C\C++ about it, although the API might be written in C\C++ of course.

 

In C\C++ that would be the same API call as in LabVIEW.

 

It's not dependent on the programming language, it's an interface to the OS and the other programs running in the OS.  The C\C++ (and LV) way to do this will only work on Windows.

 


@ColoradoBob wrote:

Any other suggestions? It's easy enough to just click on the excel window and enter the password. I'll probably just continue with that solution.


It depend on the details... We probably can help.

 

Do you want to make the window front most? That would be fairly easy.

 

Do you really want to give the text entry key focus? That's probably a bit harder.

 

How do you start Excel in the first place? Command line, ActiveX, .NET? Perhaps there is a way to pass the password, and skip the dialog.

0 Kudos
Message 4 of 8
(1,619 Views)

When I was working my LV code was strictly instrument control with a bit of 'write to excel' to save data. I never make API calls or any of that other fancy stuff. 🙃

 

The goal is to type in the password without clicking on anything (e.g. give the pw entry box the focus). I thing making the excel window front most will do this. I open excel from within my LV application. I have tried entering the password there but it is a catch-22. Once excel is called, it's too late to pass the password. Here's how I open the file.

ColoradoBob_0-1601309324795.png

 

Thanks for your help.

 

0 Kudos
Message 5 of 8
(1,611 Views)

In stead of setting the password property, you could try the Activate method. It might just work...

Activate Workbook.PNG

 

Otherwise, I can help you with this:

        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);   
 
        [DllImport("user32.dll", SetLastError = true)]
        static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName);     

 

But I'd have to know a few things. First, if you're on a 64 or 32 bit system. Next, the name and class of the window.

0 Kudos
Message 6 of 8
(1,583 Views)

I'll take a stab at the activate method approach. Otherwise I will just stick with what I have working now. (My license expires Oct 8 and we don't have the funds to purchase even a basic license for a full year. Like I said, I'm working with a non-profit.) Thanks for your help.

0 Kudos
Message 7 of 8
(1,549 Views)

@ColoradoBob wrote:

I'll take a stab at the activate method approach. Otherwise I will just stick with what I have working now. (My license expires Oct 8 and we don't have the funds to purchase even a basic license for a full year. Like I said, I'm working with a non-profit.) Thanks for your help.


I think for non-profit, the LabVIEW Community Edition is legal to use?

0 Kudos
Message 8 of 8
(1,546 Views)