Originally posted by: Semidevil
Lets say I have excel along with another program open. I want to be able to write a macro that will allow me to retrieve certain parts/data of the program and paste it into excel automatically.
is this possible? how would I go about dong that?
What kind of program would you be retrieving data from? Is this a text box you want to copy? Is this a program you made? If so, you can have that program interface with Excel.
Example VB code (for a VB app or VB script you're making, not for the macro):
---
Dim xlObj As Object
Set xlObj = GetObject(, "Excel.Application") ' obtain reference to existing Excel app
xlObj.Workbooks(1).Sheets(1).Range("A1").Value = InputBox("This data will be entered into Excel:")
---
This uses the COM object model to access Excel and interface with its methods. I'm sure it's possible with C, C++, Delphi, VB, VB.NET, C#, or any other language that supports COM/OLE.
If you need to grab the contents of a textbox from a "foreign" app, then you have to use the Windows API.
Visual Studio comes with a tool called Spy++ (there are other free window handle spies out there also) that allows you to find the Class and Window Name of the app in question. Using this information, you can look through all the HWNDS (window handles) and see which match this app. Once you have the HWND of the app, you can browse through all its controls and find the handle of the textbox. With the textbox's HWND, you can use SendMessage to retrieve the contents of the textbox and store it at a pointer, which you may access to get the text. This is all possible from within VB (and probably an Excel VBA macro) if you do it correctly.
I'm sure the app has some way to export its output (it should) so that's a far more elegant way.