When I step thru this code using F8 it works ok to save the embedded objects.
When I run the code, it gets confused and thinks the ActiveWorkbook is the WBN file for some reason, instead of the extracted WBE file. Does anyone have a thought on why the code would yield different results step thru verus run?
Thank you in advance for any input!!
Mark
When I run the code, it gets confused and thinks the ActiveWorkbook is the WBN file for some reason, instead of the extracted WBE file. Does anyone have a thought on why the code would yield different results step thru verus run?
Thank you in advance for any input!!
Mark
Code:
Sub Extract_Embedded_Files()
Dim WBO As Workbook ' Manage Files workbook
Dim WBN As Workbook ' Individual Workorder workbooks
Dim WBE As Workbook ' Extracted object workbooks
Dim WSL As Worksheet ' List of files worksheet
Dim WSN As Worksheet ' Sheet where embedded object is being activated
Dim i As Long
Dim NextRow As Long
Dim FinalRow As Long
Dim LastRow As Long
Dim RowCount As Long
Dim ThisFile As String
Dim oEmbFile As Object
' Define object variables
Set WBO = ThisWorkbook
Set WSL = WBO.Worksheets("List")
Application.DisplayAlerts = False
' Loop through all the files on WSL
FinalRow = WSL.Cells(Rows.Count, 1).End(xlUp).Row
For i = 1 To FinalRow
Application.StatusBar = i & " files processed out of " & FinalRow
ThisFile = WSL.Cells(i, 1)
' Open a file
Set WBN = Workbooks.Open(Filename:=ThisFile)
Set WSN = WBN.Worksheets("Submission")
' Reference the OLEObject by index or by name (if Object is named)
' Sheets(xxx) refers to the sheet where object is embedded
Set oEmbFile = WBN.Sheets("Submission").OLEObjects(1)
oEmbFile.Verb Verb:=xlPrimary
Set oEmbFile = ActiveWorkbook
Application.EnableEvents = False 'allows .xlsm files to close
oEmbFile.SaveAs Filename:=ThisWorkbook.Path & "\" & oEmbFile.Name, FileFormat:=xlOpenXMLWorkbook
oEmbFile.Close
WBN.Close SaveChanges:=False
Next i
Application.EnableEvents = True
Application.StatusBar = False
Application.DisplayAlerts = True
MsgBox Title:="Extract_Embedded_Files has Finished", Prompt:="Workbooks have been extracted and saved to " & ThisWorkbook.Path & "." _
& vbCr & vbCr & "Please review extracted files for accuracy.", Buttons:=vbInformation
End Sub