简体   繁体   English

将 xlsm 保存为 xlsx 后,为什么打开新文件会关闭旧文件?

[英]After saving an xlsm as xlsx, why does opening the new file close the old one?

The Background:背景:
I am trying and failing to save and modify a file that has code in a "no code" format.我正在尝试保存和修改具有“无代码”格式代码的文件,但未能成功。 I have been experimenting, and I can open a file that already exists from within an .xlsm and it works as expected.我一直在试验,我可以打开一个 .xlsm 中已经存在的文件,它按预期工作。 The script continues to run and I can modify and close the second file from within the script of the first file:脚本继续运行,我可以在第一个文件的脚本中修改和关闭第二个文件:

Sub testExistingWorkbook()

    Dim wbkName As String
        wbkName = "WorkWithWorkbookTest.xlsx"

    Workbooks.Open (wbkName)
    '~~>...code to modify goes here
    Workbooks(wbkName).Close

End Sub

However, if I save a copy of the first file without macros, and then open and modify that macro-free file afterward, the file with code closes when the file without code opens.但是,如果我保存没有宏的第一个文件的副本,然后打开并修改该无宏文件,则当打开没有代码的文件时,带有代码的文件将关闭。 Although the code looks like it is trying to do the same thing, it does not work as in the first example.尽管代码看起来像是在尝试做同样的事情,但它不像第一个示例那样工作。

I have tried many different configurations but in every case the script stops running the second I open the next file.我尝试了许多不同的配置,但在每种情况下,脚本都会在我打开下一个文件时停止运行。 The example code below, for example, stops after 'Workbooks.Open (wbkName)'.例如,下面的示例代码在“Workbooks.Open (wbkName)”之后停止。 At that moment it simultaneously closes the .xlsm, an action that my code does not seem to request (but I think must be implicit in the code).在那一刻,它同时关闭了 .xlsm,这是我的代码似乎没有请求的操作(但我认为必须隐含在代码中)。 Initially I had coded it to create an object as it opened: 'Set wbk2 = Workbooks.Open(wbkName)' but I found this did not work in either scenario.最初我编码它以在打开时创建一个对象:'Set wbk2 = Workbooks.Open(wbkName)'但我发现这在任何一种情况下都不起作用。 Then I thought maybe it was a function of 'Workbooks' being a collection of all open workbooks, so if I opened a file that was not already opened, it would essentially knock another workbook out of the collection (silly, I know).然后我想这可能是“工作簿”是所有打开工作簿的集合的一个功能,所以如果我打开一个尚未打开的文件,它本质上会从集合中剔除另一个工作簿(愚蠢,我知道)。 But that possibility is eliminated by the example code above.但是上面的示例代码消除了这种可能性。 For a time I thought maybe it was because the name was the same and somehow not differentiating between the .xlsx and .xlsm, but the below code uses a different name and results in the same problem, so that that is not a factor.有一段时间我想可能是因为名称相同并且在某种程度上没有区分 .xlsx 和 .xlsm,但下面的代码使用不同的名称并导致相同的问题,所以这不是一个因素。

Sub testSaveAsWorkbook()

    Dim wbkName As String
        wbkName = "SuperSecretWorbookTest.xlsx"

    ThisWorkbook.SaveAs Filename:=wbkName, FileFormat:=61
    Workbooks.Open (wbkName)
    '~~>Code would go here, but the original file has closed and only
    '   the new SuperSecretWorkbookTest.xlsx is open
    Workbooks(wbkName).Close

End Sub

The Question:问题:
In thinking about and writing this question, I realized that the problem really starts at the moment I execute 'ThisWorkbook.SaveAs Filename:=wbkName, FileFormat:=61' because that is when the first workbook is closed, and the new workbook takes its place.在思考和写这个问题时,我意识到问题真正开始于我执行 'ThisWorkbook.SaveAs Filename:=wbkName, FileFormat:=61' 的那一刻,因为那是第一个工作簿关闭的时候,新工作簿将其地方。 The IDE just doesn't know it yet, so even stepping through the code it only realizes the .xlsm file is no longer active when it tries executing the next line of code. IDE 只是还不知道它,因此即使单步执行代码,它也只会意识到 .xlsm 文件在尝试执行下一行代码时不再处于活动状态。

So my question is why does 'Workbooks.SaveAs' close the first file like this?所以我的问题是为什么“Workbooks.SaveAs”会像这样关闭第一个文件? And more importantly, what can I do to keep my file open and finish the script?更重要的是,我该怎么做才能保持文件打开并完成脚本?

If you were to do this manually by going to File > SaveAs to save an xlsm as an xlsx , and clicking "Yes" to continue saving as a macro-free workbook, you'd see that the xlsm is "no longer open" — only the new xlsx is.如果您通过转到File > SaveAs手动执行此操作,将xlsm保存为xlsx ,然后单击“是”继续保存为无宏工作簿,您会看到xlsm “不再打开”——只有新的xlsx是。 Which is as it should be, you've saved it as such.应该是这样,你已经保存了它。 The Workbooks.Open call doesn't close the original xlsm ; Workbooks.Open调用不会关闭原始xlsm it's already gone.它已经消失了。

In other words, in response to your observation:换句话说,响应你的观察:

In thinking about and writing this question, I realized that the problem really starts at the moment I execute ThisWorkbook.SaveAs Filename:=wbkName, FileFormat:=61 because that is when the first workbook is closed, and the new workbook takes its place.在思考和编写这个问题时,我意识到问题真正开始于我执行ThisWorkbook.SaveAs Filename:=wbkName, FileFormat:=61的那一刻ThisWorkbook.SaveAs Filename:=wbkName, FileFormat:=61因为那是第一个工作簿关闭的时候,新工作簿取而代之。

That is exactly what SaveAs does.这正是SaveAs所做的。

Workbook.SaveCopyAs "doesn't modify the open workbook in memory." Workbook.SaveCopyAs “不修改内存中打开的工作簿。” Perhaps you can first save a copy, open the copy and then save as xlsx in the end and delete the xlsm copy.也许你可以先保存一个副本,打开副本,最后保存为xlsx ,删除xlsm副本。

Sub testSaveAsWorkbook1()

    Dim wbkName As String, wbkExtension As String
    wbkName = "SuperSecretWorbookTest"
    wbkExtension = ".xlsm"

    ThisWorkbook.SaveCopyAs Filename:=wbkName & wbkExtension

    Dim copyWbk As Workbook
    Set copyWbk = Workbooks.Open(wbkName)

    ' Do whatever you want with the copy

    copyWbk.SaveAs Filename:=wbkName, FileFormat:=61
    Kill copyWbk.Path & "\" & wbkName & wbkExtension
    copyWbk.Close SaveChanges:=False

End Sub

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

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