How do you make ExcelWorkbook1.xslx visible and keep ExcelWorkbook2.xlsx invisible when both belong to the same Excel Application object ?
XlApp.Visible := True will automatically bring every workbooks in the xlApp object visible together. How do you target only workbook1 or workbook2 with the Visible property ?
MS lead me to confusion with their help file here and what I tried does not suceed.
https://docs.microsoft.com/en-us/office/vba/api/excel.application.visible
I am using this class to help me get handle to the 3 main Excel object:
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69841
For example I've tried things like :
xlWB := ExcelCOM.GetWorkbook(FichierKM)
xlWB.Windows(fileName).Visible := true
It has worked but is very unreliable and I can't understand what Excel expects.. I'm still reading through their object model trying to get clues
Thanks in advance
Excel COM Application.Visible to target specific workbook
Re: Excel COM Application.Visible to target specific workbook
I found a way that feels hacky to me but here's the sample. Is that even close to a good way of doing it ?
What's wierd about it is that the window is opened for a fraction of time and closed when the xlWB.Windows(FileName) is called.
What do you think about it and how would you approach this situation ?
Code: Select all
;...
if (!xlApp.Visible && WindowShouldBeVisible) {
xlApp.Visible := true
}
xlWB.Windows(FileName).Visible := WindowShouldBeVisible
;...
What do you think about it and how would you approach this situation ?
- flyingDman
- Posts: 2848
- Joined: 29 Sep 2013, 19:01
Re: Excel COM Application.Visible to target specific workbook
You are not stating what WindowShouldBeVisible is, but it should resolve to True or False. I have done some reading and could not find a better way. Call it "hacky" or something else as long as there is a way get it to work reliably... One thing is clear, making the app visible or not (i.e xl.visible := True / False) and making a workbook visible or not (i.e xl.Windows("filename").Visible := True / False) are 2 different things. So, if you need to open two excel files, one of which needs to visible and the other not, you need to include xl.visible := true but you can do it after hiding one of the workbooks to avoid having the second file "flash". So this would be a way to do it:
or, if there are a bunch of workbooks open, this would be a way to hide all but one:
To activate a hidden workbook, you'll have to make it visible first, then to activate it:
Code: Select all
Xl := ComObjCreate("Excel.Application")
Xl.Workbooks.Open("C:\Spreadsheets\test 123.xlsx")
Xl.Workbooks.Open("C:\Spreadsheets\test 456.xlsx")
; XL.Windows("test 123.xlsx").Activate ;not needed
Xl.Windows("test 456.xlsx").Visible := False
Xl.visible := true
Code: Select all
Xl := ComObjCreate("Excel.Application")
for x in xl.Workbooks
if (x.name != "test 123.xlsx") ;keep this one visible
xl.Windows(x.name).visible := false
Code: Select all
Xl.Windows("test 456.xlsx").visible := true
XL.Windows("test 456.xlsx").activate
14.3 & 1.3.7
Re: Excel COM Application.Visible to target specific workbook
@flyingDman
Thanks a lot FlyingDman.
I'll put all your examples in the class I'm using to make it work like you've shown here.
I really appreciate your help thanks again.![Mr. Green :mrgreen:](./images/smilies/icon_mrgreen.gif)
Thanks a lot FlyingDman.
Ah that's exactly what I wanted to acheive, nice.you need to include xl.visible := true but you can do it after hiding one of the workbooks to avoid having the second file "flash".
Nice find too, I didn't know that and was infact trying to activate a hidden workbook before showing the window in my current code.To activate a hidden workbook, you'll have to make it visible first, then to activate it
I'll put all your examples in the class I'm using to make it work like you've shown here.
I really appreciate your help thanks again.
![Mr. Green :mrgreen:](./images/smilies/icon_mrgreen.gif)