K
ker_01
Using XL2003, although preferably all code would also work on 2007 for when I
hand this off to another user who uses 2007.
I helped a colleague create some workbooks to collect some data (from
employees with both XL2003 and XL2007). To ensure that the users enabled
macros, we set it up so that the only sheet visible when opened is an
instruction sheet that tells how to enable macros; if macros are enabled then
the Workbook_Open event runs the following code:
Sheet1.Visible = xlSheetVisible 'unhide the data entry sheet
Sheet4.Visible = xlSheetHidden 'hide the macro warning/instructions sheet
Sheet1.Activate 'make sure the data entry sheet is active
CreateIndEmplList 'run code to pull employee names based on the userID of
the person opening the file
That worked just fine (for both XL2003 and XL2007 users); then I was asked
to help aggregate the data as well. I had to disable macros in the data
workbooks so the CreateIndEmplList code wouldn't run when opening the
workbooks to collect the data (partly for speed, and partly because the
person who runs the code may not have direct reports which makes that code
error out and would stop the overall data retrieval), so I ended up with:
Dim secAutomation As MsoAutomationSecurity
secAutomation = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Debug.print fil
Application.Workbooks.Open (fil), False, True
Debug.print "opened successfully"
Application.AutomationSecurity = secAutomation
So now I've got it set up to loop through the workbooks and scoop out the
data... it opens and closes the first data workbook just fine, but dies on
the second one. I don't even get the "opened successfully" debug output. The
workbook does actually open and I can interact with it manually, but the code
is dead.
Both the first file (which works) and the second file (where it dies) are
saved as xlsm format.
I'm stumped- any idea what might be happening? More code details below.
Thank you,
Keith
Retrieval code snippet:
Sub AggregateSPData()
'Searches the selected folders and sub folders for files with the
specified (xls*) extensions.
Dim i As Integer, z As Long, Rw As Long, ii As Long
Dim Dest1 As Integer, Dest2 As Integer ',Dim i As Integer
Dim WS As Worksheet, ws2 As Worksheet
Dim y As Variant
Dim fldr As String, fil As String, FPath As String
Dim LocName As String
Dim FString As String
Dim SummaryWB As Workbook
Dim SummaryWS As Worksheet
Dim Raw_WS As Worksheet
Dim LastRow As Long, FirstRow As Long, RowsOfData As Long
Dim UseData As Boolean
Dim FirstBlankRow As Long
Dim SourceRange As Variant
'grab current location for later reference, for where to paste final data
Set SummaryWB = Application.ActiveWorkbook
Set SummaryWS = Application.ActiveWorkbook.ActiveSheet
<snip>
fldr = PickAFolder 'brings up the folder picker dialogue
FirstBlankRow = 2
'asd is a 1-D array of files returned
asd = ListFiles(fldr, False)
Set WS = Excel.ThisWorkbook.Worksheets(1) 'destination for data
Set ws2 = Excel.ThisWorkbook.Worksheets(2) 'destination for data
Set ws3 = Excel.ThisWorkbook.Worksheets("AllReceivedIDs")
LastIDRow = ws3.Range("A65536").End(xlUp).Row
On Error GoTo 0
For ii = LBound(asd) To UBound(asd)
'Submitter initials, if files have been renamed
ShortID = Left(asd(ii), 3)
'file name
fil = asd(ii)
'open the file and grab the data
Dim secAutomation As MsoAutomationSecurity
secAutomation = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Debug.Print fil
Application.Workbooks.Open (fil), False, True
Debug.Print "opened successfully"
Application.AutomationSecurity = secAutomation
DoEvents
'Identify the submitter through tracking log
Excel.ActiveWorkbook.Worksheets("Lists").Visible = True
Excel.ActiveWorkbook.Worksheets("Lists").Activate
'<snip>
ThisUser = Excel.ActiveWorkbook.ActiveSheet.Range("K" &
EachUser).Value
Next
LastIDRow = LastIDRow + 1
ws3.Range("A" & LastIDRow).Value = FoundUser
Excel.ActiveWorkbook.Worksheets("Succession Planning").Visible =
True
Excel.ActiveWorkbook.Worksheets("Succession Planning").Activate
'Dynamically pull the submitted data
<snip>
'I've tried just the close line by itself, then added the
security lines in case the Workbook_BeforeClose event was being triggered as
the first workbook was closed (in case that might have caused any problems)
secAutomation = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Workbooks(Dir(fil)).Close SaveChanges:=False
Application.AutomationSecurity = secAutomation
DoEvents
Next ii
MsgBox "Data processing on the selected folder has been completed. Please
move these files to a backup directory so that their data is not accidently
pulled a second time", , "Program Complete"
End Sub
hand this off to another user who uses 2007.
I helped a colleague create some workbooks to collect some data (from
employees with both XL2003 and XL2007). To ensure that the users enabled
macros, we set it up so that the only sheet visible when opened is an
instruction sheet that tells how to enable macros; if macros are enabled then
the Workbook_Open event runs the following code:
Sheet1.Visible = xlSheetVisible 'unhide the data entry sheet
Sheet4.Visible = xlSheetHidden 'hide the macro warning/instructions sheet
Sheet1.Activate 'make sure the data entry sheet is active
CreateIndEmplList 'run code to pull employee names based on the userID of
the person opening the file
That worked just fine (for both XL2003 and XL2007 users); then I was asked
to help aggregate the data as well. I had to disable macros in the data
workbooks so the CreateIndEmplList code wouldn't run when opening the
workbooks to collect the data (partly for speed, and partly because the
person who runs the code may not have direct reports which makes that code
error out and would stop the overall data retrieval), so I ended up with:
Dim secAutomation As MsoAutomationSecurity
secAutomation = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Debug.print fil
Application.Workbooks.Open (fil), False, True
Debug.print "opened successfully"
Application.AutomationSecurity = secAutomation
So now I've got it set up to loop through the workbooks and scoop out the
data... it opens and closes the first data workbook just fine, but dies on
the second one. I don't even get the "opened successfully" debug output. The
workbook does actually open and I can interact with it manually, but the code
is dead.
Both the first file (which works) and the second file (where it dies) are
saved as xlsm format.
I'm stumped- any idea what might be happening? More code details below.
Thank you,
Keith
Retrieval code snippet:
Sub AggregateSPData()
'Searches the selected folders and sub folders for files with the
specified (xls*) extensions.
Dim i As Integer, z As Long, Rw As Long, ii As Long
Dim Dest1 As Integer, Dest2 As Integer ',Dim i As Integer
Dim WS As Worksheet, ws2 As Worksheet
Dim y As Variant
Dim fldr As String, fil As String, FPath As String
Dim LocName As String
Dim FString As String
Dim SummaryWB As Workbook
Dim SummaryWS As Worksheet
Dim Raw_WS As Worksheet
Dim LastRow As Long, FirstRow As Long, RowsOfData As Long
Dim UseData As Boolean
Dim FirstBlankRow As Long
Dim SourceRange As Variant
'grab current location for later reference, for where to paste final data
Set SummaryWB = Application.ActiveWorkbook
Set SummaryWS = Application.ActiveWorkbook.ActiveSheet
<snip>
fldr = PickAFolder 'brings up the folder picker dialogue
FirstBlankRow = 2
'asd is a 1-D array of files returned
asd = ListFiles(fldr, False)
Set WS = Excel.ThisWorkbook.Worksheets(1) 'destination for data
Set ws2 = Excel.ThisWorkbook.Worksheets(2) 'destination for data
Set ws3 = Excel.ThisWorkbook.Worksheets("AllReceivedIDs")
LastIDRow = ws3.Range("A65536").End(xlUp).Row
On Error GoTo 0
For ii = LBound(asd) To UBound(asd)
'Submitter initials, if files have been renamed
ShortID = Left(asd(ii), 3)
'file name
fil = asd(ii)
'open the file and grab the data
Dim secAutomation As MsoAutomationSecurity
secAutomation = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Debug.Print fil
Application.Workbooks.Open (fil), False, True
Debug.Print "opened successfully"
Application.AutomationSecurity = secAutomation
DoEvents
'Identify the submitter through tracking log
Excel.ActiveWorkbook.Worksheets("Lists").Visible = True
Excel.ActiveWorkbook.Worksheets("Lists").Activate
'<snip>
ThisUser = Excel.ActiveWorkbook.ActiveSheet.Range("K" &
EachUser).Value
Next
LastIDRow = LastIDRow + 1
ws3.Range("A" & LastIDRow).Value = FoundUser
Excel.ActiveWorkbook.Worksheets("Succession Planning").Visible =
True
Excel.ActiveWorkbook.Worksheets("Succession Planning").Activate
'Dynamically pull the submitted data
<snip>
'I've tried just the close line by itself, then added the
security lines in case the Workbook_BeforeClose event was being triggered as
the first workbook was closed (in case that might have caused any problems)
secAutomation = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityForceDisable
Workbooks(Dir(fil)).Close SaveChanges:=False
Application.AutomationSecurity = secAutomation
DoEvents
Next ii
MsgBox "Data processing on the selected folder has been completed. Please
move these files to a backup directory so that their data is not accidently
pulled a second time", , "Program Complete"
End Sub