Why is freshly opened Workbook.Name different from Workbook title in Excel window?

眉间皱痕 提交于 2021-02-08 05:34:33

问题


I have created a file and referenced it as GlobalFile.
Then I saved it as "Glo" and then as "Ume". Then I reopen the saved files to have two distinct workbooks open on two different names in two different Workbook objects: GlobalFile and NightMareFile.

After opening, Excel windows has the correct headers as "Glo.xls" and "Ume.xls" respectively, but testing NightMareFile.Name results in "Glo.xlsx" !!!

How is this possible at all?


I'm on Win 10 64 bit, Excel 365 16 bit.

Already tried:

  • DoEvents before or after Open
  • RefreshAll after open
  • Excel restart brought no change.

What makes me pull my hair: Changing the order of the two open blocks fixes the name conflict: if "Ume" is opened first it has the correct name, as does "Glo".

Originally I had more named file versions saved and reopened, but only this one was always faulty, hence the new name: NightMareFile. No matter how I changed the order of files to open, this one always inherited the name of the file opened before him into another object variable.

Option Explicit

Sub main_control_routine()

    Dim GlobalFile As Workbook
    Dim NightMareFile As Workbook

    Set GlobalFile = Workbooks.Add
    Debug.Print "GlobalFile.Name: " & GlobalFile.Name

    Application.DisplayAlerts = False

    GlobalFile.SaveAs Filename:="Glo"
    Debug.Print "GLOBAL File ready!"

    'GlobalFile save as Ume
    GlobalFile.SaveAs Filename:="Ume"
    Debug.Print "GlobalFile.Name: As Ume " & GlobalFile.Name

    Application.DisplayAlerts = True

    'GLOBAL reopened to GlobalFile
    Set GlobalFile = Workbooks.Open("Glo", False)
    Debug.Print "GlobalFile.Name: " & GlobalFile.Name

    'Ume reopened to NightMareFile
    Set NightMareFile = Workbooks.Open("Ume", False)
    Debug.Print "NightMareFile.Name: " & NightMareFile.Name

End Sub

回答1:


If a workbook with the same name as the one you're trying to open is already open, and you're trying to assign a workbook object variable to the return value of the Open() method, then the end result can be unpredictable.

For example - if I run this with both workbooks "Glo" and "Ume" already open:

Sub main_control_routine()

    Dim wb As Workbook

    Set wb = Workbooks.Open("Glo.xlsx", False)
    Debug.Print wb.Name

    Set wb = Workbooks.Open("Ume.xlsx", False)
    Debug.Print wb.Name

    Set wb = Workbooks.Open("Glo.xlsx", False)
    Debug.Print wb.Name

    Set wb = Workbooks.Open("Ume.xlsx", False)
    Debug.Print wb.Name

End Sub

...this is the output:

Ume.xlsx
Ume.xlsx
Ume.xlsx
Ume.xlsx

Not what you'd expect.

In my testing it looks like instead of getting the intended workbook it returns a reference to the last-opened workbook.

The fix would be to always check if the workbook is already open before using Workbooks.Open() to get a reference to it.



来源:https://stackoverflow.com/questions/56260958/why-is-freshly-opened-workbook-name-different-from-workbook-title-in-excel-windo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!