问题
I'm using MS Office interop assemblies to build a Powerpoint addin that inserts slides and charts on the fly. But during insertion it take up to 20 seconds to complete the process from inserting the slide to insertion of the chart.
After doing extensive logging, I noticed few lines of code where our application gets stuck for 20+ seconds. Here are those lines:
powerpointChartObj.SetSourceData(Source:=String.Format("=Sheet1!A1:B{0}", 5))
powerpointSlideObject.Shapes("some shape name")
We continuously play with PowerPoint chart and slide objects during the slide insertion process but some time it gets blocked on one of above lines. And it's random on which line it gets blocked.
Following are the office libraries we are using:
- Microsoft Office 16.0 Object Library
- Microsoft Excel 16.0 Object Library
- Microsoft Graph 16.0 Object Library
- Microsoft PowerPoint 16.0 Object Library
- Microsoft Word 16.0 Object Library
- Microsoft.Office.Tools.dll
- Microsoft.Office.Tools.Common.dll
- Microsoft.Office.Tools.Common.v4.0.Utilities.dll
- Microsoft.Office.Tools.v4.0.Framework.dll
- Microsoft.Vbe.Interop
Test Environemnt
Office 2016
Windows 8.1 + Windows 10
16 GB RAM, Intel Core i5-4570 CPU
Any solution or workaround would be highly appreciated.
PS. Also tried using Office 15.0 Object libraries
Updated
I've tried EnableEvents and set it false forever. It boost up the performance during chart insertion/update scenarios but if we continuously make calls to PowerPoint COM components(e.g. Shapes, slides etc. to manipulate PowerPoint things), PowerPoint gets stuck for around 20 seconds again. Not sure if there is anything similar to EnableEvents available in PowerPoint interop.
回答1:
I don't have the rights to comment yet, so bear with me if this is not the perfect answer.
My experience with automation with office products is through VBS, but they both uses the COM-objects, so it probably works the same. I mostly use Excel, so powerpoint is not my strongest side.
My suggestion is to temporary disable screenupdates/events while adding data.
Here is an example (using VBS):
Application.Calculation = xlCalculationManual 'Excel specific
Application.ScreenUpdating = False 'All office products
Application.DisplayStatusBar = False 'All office products
Application.EnableEvents = False 'All office products
'your code running here
Application.EnableEvents = True 'All office products
Application. DisplayStatusBar = True 'All office products
Application.ScreenUpdating = True 'All office products
Application.Calculation = xlCalculationAutomatic 'Excel specific
Also avoid using a lot "select" calls in data, since it slows things down.
In my VBS scripts I got more or less no lag or problems by using this technique. I would also suggest that you create your own "please wait" popup screen while adding alot of data. For some calls do take time, even if you do it manually in Powerpoint / Excel.
Good luck, office automation can be a hazzle :)
来源:https://stackoverflow.com/questions/53004405/powerpoint-interop-assemblies-slowness-issue