简体   繁体   中英

Can't Workbook.Open an XLS File Type?

Stuck on this one. I'm creating a 2016 Excel macro that (will hopefully) loop through all .xls files in a folder and make some formatting changes, however my workbook.open doesn't open the .xls file. It seems like it just glances right over it? I've unchecked all the boxes in the Trust Centre, so why won't this open the .xls file in my VBA code? Anything else I can try? It works if the file is of .xlsx format, but not .xls for some reason. Relevant section of code is below. Thanks!

UPDATE

As requested, I'm going to try and get a bit more technical here. I've added a full script here (without any of the formatting stuff that it does) for someone to troubleshoot with me.

What's interesting to note here is:

Even though I have it set to only look for .xls file types in the loop, for some reason it carries out all of my formatting commands in the current macro .xlsm workbook, which it shouldn't. So as a workaround, I also had included the line

' Start a loop for all .xlsx files in the folder (defined in the last step)
Do While Filename <> "" And Filename <> "Bid_Report_Macro.xlsm" ' This workbook's name

to prevent this from happening in the current workbook. I would notice that all my formatting stuff would carry out once I clicked this button, within the same workbook, not the .xls file that it should be opening first. Don't ask me why that happens because I really don't know.

Also, the .xls file that I'm trying to open is a Microsoft Excel 97-2003 Worksheet (.xls) file. I have unchecked all of the boxes in the Trust Centre regarding blocking the opening of certain file types/file blocking. If I run this same code, but source all .xlsx files in a folder instead, it'll work. So I don't know why it's not working for the .xls files. When I click my macro button, nothing happens.

I tried to include CorruptLoad:=xlExtractData and CorruptLoad:=xlRepairFile in the open function, but that didn't do anything either.

I've tried the suggestions shown below and they didn't work. I'm assuming it has something to do with my local machine as it's a work computer, but I really don't know where to look and I would like to get this project done.

Any ideas? Thanks.

Sub Bid_Report_Function()

' Create (but don't define yet) some variables used throughout
Dim folderPath As String
Dim Filename As String
Dim wb As Workbook
Dim SrchRng As Range, cel As Range
Dim EmptyDays As Integer

' Run it in the background.
Application.ScreenUpdating = False

' Directory of this macro that has the other .xls files
folderPath = Application.ActiveWorkbook.Path

' A conditional for finding the directory with proper structure
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"

' Do this for each xls workbooks in the folder
Filename = Dir(folderPath & "*.xls")

' Start a loop for all .xlsx files in the folder (defined in the last step)
Do While Filename <> "" 'And Filename <> "Bid_Report_Macro.xlsm" ' This workbook's name

    ' Open a workbook in the folderPath
    Set wb = Workbooks.Open(folderPath & Filename, CorruptLoad:=xlRepairFile)

    ' Always start on the first worksheet of the opened workbook
    wb.Worksheets(1).Activate

    '......my formatting code is here. Skipping to the end...

    ' Rename the current tab
    wb.ActiveSheet.Name = "RENAMED TAB"


    ' Save as, a copy as a .xls file
    wb.SaveAs Filename:="TEST.xls"

    ' Close but don't save the current workbook to keep it the same
    wb.Close False


' This prevents the macro from doing all of the above endlessly.
Exit_Loop:
    Set wb = Nothing
    Filename = Dir

' Next workbook in the directory
Loop

' Can turn screen updating back on
Application.ScreenUpdating = True

' End program
End Sub

Perhaps your Do loop should look more like this:

' Start a loop for all .xlsx files in the folder (defined in the last step)
Do While Filename <> ""
    If Filename <> ThisWorkbook.Name Then 'This workbook's name
        ' Open a workbook in the folderPath
        Set wb = Workbooks.Open(folderPath & Filename, CorruptLoad:=xlRepairFile)
        ' Always start on the first worksheet of the opened workbook
        wb.Worksheets(1).Activate
    End If
    'get the next file in the folder which matches the Directory search
    Filename = Dir
Loop

I figured it out. Although it isn't a great solution, but I got it working by actually defining the full filename of the .xls file I was hoping to use. This isn't great, because it doesn't recognize all of the .xls files in the folder, rendering the loop useless, but for now it works. This is the working code. I will try and get a working loop for it someday. Thanks!

' Run it in the background.
Application.ScreenUpdating = False

' Directory of this macro that has the other .xls files
folderPath = Application.ActiveWorkbook.Path

' A conditional for finding the directory with proper structure
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"

' Do this for each xls workbooks in the folder
Filename = Dir(folderPath & "translate_quote_42666432_v6_all.xls")

' Start a loop for all .xlsx files in the folder (defined in the last step)
Do While Filename <> ""

    ' Open a workbook in the folderPath
    Set wb = Application.Workbooks.Open(folderPath & Filename)

    ' Always start on the first worksheet of the opened workbook
    wb.Worksheets(1).Activate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM