简体   繁体   English

无法工作簿。打开 XLS 文件类型?

[英]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.我正在创建一个 2016 Excel 宏,它(希望)遍历文件夹中的所有 .xls 文件并进行一些格式更改,但是我的 workbook.open 不会打开 .xls 文件。 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?我已取消选中信任中心中的所有框,那么为什么这不会在我的 VBA 代码中打开 .xls 文件? Anything else I can try?还有什么我可以尝试的吗? It works if the file is of .xlsx format, but not .xls for some reason.如果文件是 .xlsx 格式,但由于某种原因不是 .xls 格式,它会起作用。 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.尽管我将它设置为仅在循环中查找.xls文件类型,但出于某种原因,它会在当前宏.xlsm工作簿中执行我的所有格式化命令,而它不应该这样做。 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.我会注意到,一旦我在同一个工作簿中单击此按钮,我的所有格式化内容都会执行,而不是它应该首先打开的 .xls 文件。 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.此外,我尝试打开的.xls文件是Microsoft Excel 97-2003 Worksheet (.xls)文件。 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.如果我运行相同的代码,但将所有.xlsx文件放在一个文件夹中,它将起作用。 So I don't know why it's not working for the .xls files.所以我不知道为什么它不适用于.xls文件。 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.我试图在 open 函数中包含CorruptLoad:=xlExtractDataCorruptLoad:=xlRepairFile ,但这也没有做任何事情。

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:也许你的 Do 循环应该更像这样:

' 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.虽然这不是一个很好的解决方案,但我通过实际定义我希望使用的 .xls 文件的完整文件名使其工作。 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.这不是很好,因为它不能识别文件夹中的所有 .xls 文件,从而使循环无用,但现在它可以工作。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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