Close file that was inserted in excel as object as an icon

emmieh

New Member
Joined
Jun 25, 2009
Messages
11
I need some help on a VBA code. I want to insert other files in an excel file which is displayed as an icon and give the icon a specific name. I used this code:

Dim fn
fn = Application.GetOpenFilename
If fn = False Then
Exit Sub
End If

name = InputBox("give the file a name")

ActiveSheet.OLEObjects.Add(Filename:=fn, Link:=False, DisplayAsIcon:=True, IconFileName:= _
"C:\Program Files\Internet Explorer\iexplore.exe", _
IconIndex:=4, IconLabel:=name).Select
Selection.Left = 1000
End Sub

The file that is inserted opens in the background when it is inserted. It is not visible but it remains open. I would like the macro to automatically close the file without saving changes after it was inserted in the excel file .

Does anybody know how to add this to the code?

thank you for the help!!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Welcome to the Board.

Is it an Excel workbook that is being inserted? This closes it, but there also seems to be a copy open.

Code:
Sub Test()
    Dim fn
    fn = Application.GetOpenFilename
    If fn = False Then
        Exit Sub
    End If
    Name = InputBox("give the file a name")
    ActiveSheet.OLEObjects.Add(Filename:=fn, Link:=False, DisplayAsIcon:=True, IconFileName:= _
        "C:\Program Files\Internet Explorer\iexplore.exe", _
        IconIndex:=4, IconLabel:=Name).Select
    Selection.Left = 1000
    fn = Right(fn, Len(fn) - InStrRev(fn, "\"))
    Workbooks(fn).Close False
End Sub
 
Upvote 0
Hi Andrew,
it can actually be any kind of file that is inserted, i think that makes it so hard!
 
Upvote 0
e.g. I want to insert a Word file in the excel file. You can do this in excel through Insert--> Object --> Create from file --> Display as icon and browse to the document you want to insert. The code I posted does this too. If you insert a word file, this is opened on the background when it is inserted. You do not see Word as an active application but it is running at the background. It would like the Word application to close after the file was inserted. But this should also happen with acrobat reader when a pdf file is inserted. Is that clear enough?
 
Upvote 0
How do you know Word is running in the background? I can't see it as an Application or a Process in Task Manager. The same applies to Acrobat Reader.
 
Upvote 0
If i run the macro I can see a process running in the Task Manager, I see e.g. Winword.exe. I cannot explain why this does not happen when you run the Macro.
 
Upvote 0

Forum statistics

Threads
1,215,518
Messages
6,125,293
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top