Closing Embedded Excel Document

tomexcel1

New Member
Joined
Feb 22, 2018
Messages
47
Hi All,

I currently use the below code to allow users to attach documents (Normally Excel) to the current sheet.

When the Excel documents are embedded, they automatically open in the background. Is there a way of closing them? Currently, we embed the document, then close the master file and reopen it again. Could I add anything to the code to close the file as soon as it's embedded whilst keeping the master file open?

Thanks
In Advance

VBA Code:
Private Sub CommandButton1_Click() 
Sheet1.Unprotect ("passwordhere")
    Set Rng = Range("F8")
    Rng.RowHeight = 7.8
    On Error Resume Next
    fpath = Application.GetOpenFilename("All Files,*.*", Title:="Select file", MultiSelect:=True)
    For i = 1 To UBound(fpath)
        Rng.Select
        Rng.ColumnWidth = 8.11
        
        ActiveSheet.OLEObjects.Add _
        Filename:=fpath(i), _
        Link:=False, _
        DisplayAsIcon:=True, _
        IconFileName:="excel.exe", _
        IconIndex:=0, _
        IconLabel:=extractFileName(fpath(i))
        ActiveSheet.OLEObjects.Locked = False
        Set Rng = Rng.Offset(0, 1)
    Next i
    Sheet1.Range("H5").Value = "1" 
    Sheet1.Protect ("passwordhere")
End Sub


Public Function extractFileName(filePath) 'Used in code for attaching raw file
    For i = Len(filePath) To 1 Step -1
        If Mid(filePath, i, 1) = "\" Then
        extractFileName = Mid(filePath, i + 1, Len(filePath) - i + 1)
        Exit Function
        End If
    Next
End Function
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Tested your code & embedded file does not open for me. You could (at the end of the sub)
VBA Code:
  Set wb = FPath(i)
  If Not wb Is Nothing Then
     wb.Close savechanges:=False
  Else
     Set wb = Nothing
  End If
or change False to True to save changes.
 
Upvote 0
Thanks for your reply. I added your code, and I still get the same outcome.

The embedded file doesn't physically open on screen, but after I embed it, I can see it open in the background when looking on VBA editor

1680523361170.png
 
Upvote 0
AFAIK, that is normal because the embedded file becomes part of the vba project scope. No experience with that in Excel but I'd liken it to creating a reference in Access to another db. The referenced db isn't open, but its code project is in scope (available to be used by the 'parent' db). At first I thought you were saying you could see it in Task Manager. That would mean that it's either open or hasn't been cleared from memory.
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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