0

I would like to loop through all my excel files in the directory and resave them.

The following code:

 Sub DirectoryFileLoop()
 Dim fileDirectory As String
 Dim FileName As String
 Dim fileToOpen As Workbook
 Dim strPath As String

 strPath = Application.FileDialog(msoFileDialogOpen).Show


 Application.ScreenUpdating = False

 fileDirectory = Dir$(strPath & "*.xls", vbNormal)

 FileName = Dir(fileDirectory)

 Do While Len(FileName) > 0

 Set fileToOpen = Workbooks.Open(fileDirectory & FileName)

 With fileToOpen
 .Save
 .Close
 End With


 Debug.Print FileName

 FileName = Dir
 Loop


 Application.ScreenUpdating = True

 MsgBox ("All files have been validated")
 End Sub

works just for a few first examples. Afterward I am getting an error:

Run-time error '1004' Can't move focus to the control because it is invisible, not enabled or of a type that does not accept the focus.

enter image description here

Tried to find some solution, and everything I found is just here:

https://social.msdn.microsoft.com/Forums/office/en-US/3263b079-7e4f-452c-8dcc-92c682b8370b/excel-form-cant-move-focus-to-the-control-because-it-is-invisible-not-enabled-or-of-a-type-that?forum=exceldev

Error: Can't move focus because it is invisible

but these hints didn't help me.

Where might be the problem here?

Geographos
  • 827
  • 2
  • 23
  • 57

1 Answers1

1

Your code is wrong. It does not return a folder, as you need, Dir is also wrongly used. Please, try the next adapted code:

Sub DirectoryFileLoop()
 Dim fileDirectory As String, FileName As String
 Dim fileToOpen As Workbook

 fileDirectory = getFolderPath & "\"

 Application.ScreenUpdating = False
 
 If fileDirectory = "\" Then MsgBox "No any folder has been selected...": Exit Sub

 FileName = Dir$(fileDirectory & "*.xls*", vbNormal)
 
 If FileName = "" Then MsgBox "No Excel workbook could be found in " & fileDirectory: Exit Sub
 
 Do While FileName <> ""
     Set fileToOpen = Workbooks.Open(fileDirectory & FileName)
     'do whatever you need here (to validate)...
     fileToOpen.Close True
     Debug.Print FileName

     FileName = Dir
 Loop

 Application.ScreenUpdating = True

 MsgBox ("All files have been validated")
 End Sub

 Private Function getFolderPath(Optional strPath As String) As String
 Dim Fldr As FileDialog, sItem As String
 Set Fldr = Application.FileDialog(msoFileDialogFolderPicker)
 With Fldr
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    If strPath <> "" Then .InitialFileName = strPath 'the folder where the dialog to open
    If .Show <> -1 Then GoTo NextCode
    sItem = .SelectedItems(1)
 End With
NextCode:
 getFolderPath = sItem
 Set Fldr = Nothing
End Function

getFolderPath offers the possibility to set an initial folder where form to start browsing. I used it without such a parameter (it is Optional), but if necessary/helpful you can use it.

FaneDuru
  • 38,298
  • 4
  • 19
  • 27
  • The code seem to work, but the same error keeps coming out – Geographos Mar 16 '23 at 12:16
  • @MKR Do you want processing only `.xls` files, or all Excel ones (xlx, xlsx, xlsm, xlst etc.)? Then, when the respective message is raised, does the code stop highlighting a specific code line? If so, pressing `Debug` and moving the cursor over `FileName` variable, what does it display? An expected name, or something strange? – FaneDuru Mar 16 '23 at 12:21
  • I want to process Excel-macro files. The error appears on the following line: Set fileToOpen = Workbooks.Open(fileDirectory & FileName) - the scenario exactly the same - after 6th file, it appears. Honestly, the only thing, which comes to my mind is unblocking the excel files from displaying macros (what Windows 10 introduced in August). Maybe this is the reason? – Geographos Mar 16 '23 at 12:24
  • @MKRPlease, read carefully my above comment and also answer the second question (related to `FileName` displaied when moving the cursor over it). When stopped on error and looking in `Immediate Window`, which is the last `FileName`? Always the same, or not? When saying "Excel-macro files" do you mean `.xlsm` type? – FaneDuru Mar 16 '23 at 12:27
  • @MKR No any news? – FaneDuru Mar 16 '23 at 13:31
  • after your advice - Debug says: Run time error: Method 'Open' of object 'Workbooks' failed. – Geographos Mar 16 '23 at 13:55
  • @MKR What my "advice" are you talking about? I asked you to check what moving the cursor over the variable will show, and which is the last file appearing in `Immediate Window` when the code stops. Do you get me? I must confess I cannot get you... – FaneDuru Mar 16 '23 at 14:52
  • In Immediate window I can see the last file, where it stopped. I've updated my question, as I guess what causes this error despite the code correctness. – Geographos Mar 16 '23 at 15:01
  • @MKR Of course you can see the last file, but the question was different. Then, I do not see any edit on your question, so I cannot understand what you are talking about when say "I've updated my question". In such circumstances I started thinking that I am wasting my time trying to help... – FaneDuru Mar 17 '23 at 07:42
  • I will accept your answer and raise the new question, as the update didn't go through (I had a message, that the community or something must accept it?) In fact, your code as mine above is working, therefore I guess it's a problem for a separate question. – Geographos Mar 19 '23 at 15:28