简体   繁体   English

Excel工作簿仅在运行代码完成后显示

[英]Excel workbook only shows after running code is finished

I have a program in an Excel workbook that opens a Powerpoint-File, updates the links within this file and closes it after that. 我在Excel工作簿中有一个程序,该程序可打开Powerpoint文件,更新此文件中的链接并在此之后将其关闭。 This works fine. 这很好。 Here is my problem: When the links are updated an Excel file with the source data is opened. 这是我的问题:更新链接后,将打开带有源数据的Excel文件。 After Powerpoint is closed this file stays open. 关闭Powerpoint后,此文件保持打开状态。 I want it to get closed because I repeat this process for many files and I can't end up with hundreds of open Excel files. 我希望将其关闭,因为我对许多文件重复了此过程,但最终无法获得数百个打开的Excel文件。

I tried the following: 我尝试了以下方法:

WBKs=Application.Workbooks.count
For i = WBKs to 1 Step -1
    If Workbooks(i).Name<>ThisWorkbook.Name then
        Workbooks(i).close savechanges:=False
    End if
Next i

Now comes the weird part. 现在来了怪异的部分。 Whenever I just run my code, WBKs always returns 1 and the Excel file only pops up after the code is finished. 每当我运行代码时,WBK始终返回1,并且仅在代码完成后才弹出Excel文件。 If I go through my code in debug mode it works. 如果我在调试模式下浏览代码,它将起作用。 The workbook pops up as soon as I enter debug mode. 进入调试模式后,工作簿将弹出。

I tried Applicatio.Wait in the hope that the file would show after a second. 我尝试了Applicatio.Wait ,希望文件能在一秒钟后显示出来。 The file only showed after the code was finished. 该文件仅在代码完成后显示。

I tried a Do While Loop to wait until the file is open. 我尝试了“ Do While Loop以等待文件打开。 Excel crashes because I never leave the loop. Excel崩溃是因为我从不离开循环。

Edit: I tried DoEvents as suggested. 编辑:我按照建议尝试了DoEvents Does not work either. 也不起作用。

This is just a workaround, but try using a brute force after x times your macro has run. 这只是一种解决方法,但是请在运行x倍的宏后尝试使用蛮力。 Store that x somewhere in workbook, save. x存储在工作簿中的某个位置,保存。 And kill excel process (all instances, including self) : 并杀死excel进程(所有实例,包括self):

Dim sKill As String

sKill = "TASKKILL /F /IM excel.exe"
Shell sKill, vbHide

Found here : VBA script to close every instance of Excel except itself 在这里找到: VBA脚本可关闭除自身以外的所有Excel实例

When running your macro next time, you will use that x as a starting point for next PPT file to update. 下次运行宏时,您将使用x作为下一个要更新的PPT文件的起点。

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

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