Another VBA exercise, file this under "bored":
I wrote this code to return the Application name for any given object (well, most object). Just pass in an object and it will tell you what application it belongs to.
Function GetAppName(obj As Object) As String Dim objParent As Object Dim myObject As Object Set myObject = obj If TypeName(myObject) = "Application" Then GetApp = myObject.Name Exit Function End If Do Until TypeName(objParent) = "Application" Set objParent = myObject.Parent Set myObject = objParent Loop GetAppName = objParent.Name End Function
Usage:
Sub test_me()
Dim rng As Excel.Range
Dim str As String
Set rng = Range("A1")
str = GetAppName(rng)
End Sub
Or:
Sub test_me2() Dim olApp As Object Dim olMsg As Object Dim str As String Set olApp = GetObject(, "Outlook.Application") Set olMsg = olApp.CreateItem(0) ' email message str = GetAppName(olMsg) End Sub
Can anyone think of any use for this?





Can anyone think of any use for this? I think I have.
We have an application built in Excel that allows customers to populate information and then send the completed file back to us for import into our Access database. We recently had a customer open the file in Works (yeah, I know) and what we got back was just a mess.
Although I haven't been able to test it, what I'd like to see is if I can figure out what application opened the Excel file, and then give the user a message that "some of the form controls and other functionality of the file will not work" unless it is opened in Excel. I have already tested to be sure that Access does not cause an issue when it opens the file and reads the data on the various worksheets during the import process. So far so good. Now I just need to find a computer with some other program that can open Excel files so I can see what happens.
Thanks for the code!
PS — the last line of the GetAppName function as well as the call to the function in the test_me sub procedure say "GetApp" rather than "GetAppName".
Unfortunately, there's no cure for PEBKAC.
The code does have limited uses. In your case, any code you write would presuppose that the file was opened in Excel, otherwise how would the opening program know to run the VBA code? For example, if I were able to open a Word file in Excel, Excel would still have no knowledge of the Word object model, only maybe the text of the document itself.
Your best bet is to simply ask your customers to fill out your form ONLY in Excel and resend.
Thanks for the correction, I updated the article!