#1
|
|||
|
|||
VBA and shapes very slow
Bonjour
Je maitrise mal votre langue, et je m'en excuse. Le texte qui suit est issu d'une traduction automatique, et il peut ne pas être clair. I use Word VBA to insert numerous shape objects in documents. Under Word 2003, the macro run in a immediate way. But under Word 2010, these macros are in practice unusable because their execution can be very slow. Here is an example: Code:
Sub MesureDuree() Dim i As Long, Boite As Shape, Debut As Date Debut = Time For i = 1 To 100 'Set Boite = Me.Shapes.AddTextbox(msoTextOrientationHorizontal, 4 * i, 72.75, 4, 19.5) Set Boite = Me.Shapes.AddShape(msoShapeRectangle, 4 * i, 72.75, 4, 19.5) Boite.Fill.ForeColor.RGB = RGB(2 * i, 2 * i, 2 * i) Next i MsgBox CStr((Time - Debut) * 24 * 3600) & " s" End Sub If I register(record) the new document in the size(format) Word 2003 (extension .doc, i.e. in mode(fashion) of compatibility) before throwing(launching) the macro, the execution is immediate. For the version with Textbox, the run time in Word 2010 achieves 50 seconds! What changed between both versions of Word to have such a slowing down, and can one avoid him while leaving the document with the format 2010? J'utilise Word VBA pour insérer de nombreux objets shape dans des documents. Sous Word 2003, les macros s'exécutent de façon instantanée. Mais sous Word 2010, ces macros sont en pratique inutilisables car leur exécution peut être très lente. Voici un exemple : .... J'ouvre un nouveau document sous Word 2010 et je colle la macro dans ThisDocument. L'exécution dure 6 secondes. Si j'enregistre le nouveau document au format Word 2003 (extension .doc, c.-à-d. en mode de compatibilité) avant de lancer la macro, l'exécution est immédiate. Pour la version avec Textbox, le temps d'exécution en Word 2010 atteint 50 secondes ! Qu'est ce qui a changé entre les deux versions de Word pour avoir un tel ralentissement, et peut on l'éviter tout en laissant le document au format 2010 ? |
#2
|
||||
|
||||
On my system (Win 7, Word 2010) execution is almost immediate - your timer says execution takes 0 seconds...
Tweaking the code to insert 2000 shapes takes less than 2 seconds.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thank you for your answer.
The mystery thickens, because I am not the only one to have this problem of slowing down. I asked two of my friends to make tests, and they met the same phenomenon with the same configuration as me (Windows7 and Office 2010). Could the problem come from the French version of Office? Je vous remercie pour votre réponse. Le mystère s'épaissit, car je ne suis pas le seul à avoir ce problème de ralentissement. J'ai demandé à deux de mes amis de faire des tests, et ils ont rencontré le même phénomène avec la même configuration que moi (Windows7 et Office 2010). Le problème pourrait-il venir de la version française d'office ? |
#4
|
||||
|
||||
I very much doubt the language (or regional settings, etc.) has anything to do with it. The problem is more likely be due to interference from a third-party addin or issues with other content in the document you're using.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Hello and thank you for your suggestions.
On your advice, I thus launch Word without complement, it changes nothing. I understood that at your home the execution of the macro is very fast. I thus made execute the macro to some friends who possess Office 2010. They all noticed the same slowing down. A progress however: if I hide the window of my document (lines 5. And 12.), the run time is divided by 36: it passes from 108 seconds to 3 seconds. Code:
Sub MesureDureebis() Dim i As Long, Boite As Shape, Debut As Date Debut = Time ActiveDocument.Windows(1).Visible = False For i = 1 To 500 Set Boite = Me.Shapes.AddShape(msoShapeRectangle, 4 * i, 72.75, 4, 19.5) Boite.Fill.ForeColor.RGB = RGB(2 * i, 2 * i, 2 * i) Next i ActiveDocument.Windows(1).Visible = True MsgBox CStr((Time - Debut) * 24 * 3600) & " s" End Sub Bonjour et merci pour vos suggestions. Sur vos conseils, je lance donc Word sans complément, cela ne change rien. J'ai cru comprendre que chez vous l'exécution de la macro est très rapide. J'ai donc fait exécuter la macro à quelques amis qui disposent d'Office 2010. Ils ont tous constaté le même ralentissement. Seul progrès : si je cache la fenêtre de mon document (lignes 5. et 12.), le temps d'exécution est divisé par 36 : il passe de 108 secondes à 3 secondes. J'avoue ne rien y comprendre ! |
#6
|
|||
|
|||
jpl,
I will confirm that the French version and regional settings appear to have nothing to do with it. I see the same behavior as you and your friends. My experience is that that the code starts out relatively fast and gets slower as it runs. Add Debug.Print i to the code and watch the immediate window. Here 100 runs in 0 sections, 500 2.001, 2000 18.99 (with your hide/show window mod). Before the mod, 2000 was going to take several minutes. Paul, I'm running Word 2010, Windows 7 (all updated) as well. I don't have any add-in loaded either. Do you think this could be related to a difference in print drivers or set setting that you might have applied? |
#7
|
||||
|
||||
I suppose it's conceivable printer drivers make a difference (I use the Adobe Acrobat Pro PDF driver as my default). No addins installed and I'm not aware of any other settings that might affect this.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Well it is simply baffling that you don't experience this problem while I and everyone else I know does
It is not my print driver. I changed to the one that you are using to no improvement. It has something to do with the number of the objects in the document. For example if I run the revised code with 500 entered it takes 2 sections. If I run that code again it takes 7 seconds. If I run it a third time it take 17 seconds. What view do you use? |
#9
|
|||
|
|||
Paul,
Here, there is a very noticeable difference in how the shapes appear between Word 2003/2007 and Word 2010/13/16. Are you seeing those differences on your end? |
#10
|
|||
|
|||
Hello, and thank you for the interest which you carry in my (our?) problem.
For information, I have no printer connected with my computer. Actually, the problem is specific in Word 2010: I modified the macro to change the document to the Word 2003 document format. The run time passes from 108 seconds to 1 second. Code:
Sub MesureDureeter() Dim i As Long, Boite As Shape, Debut As Date Debut = Time Me.DowngradeDocument For i = 1 To 500 Set Boite = Me.Shapes.AddShape(msoShapeRectangle, 4 * i, 72.75, 4, 19.5) Boite.Fill.ForeColor.RGB = RGB(2 * i, 2 * i, 2 * i) Next i Me.Convert MsgBox CStr((Time - Debut) * 24 * 3600) & " s" End Pour information, je n'ai aucune imprimante reliée à mon ordinateur. Effectivement, le problème est spécifique à Word 2010 : j'ai modifié la macro pour passer le document au format Word 2003. Le temps d'exécution passe de 108 secondes à 1 seconde. |
#11
|
|||
|
|||
The problem is across Word version 2010 and higher. While your changes resolves the speed issue, the result sin this particular case are not the same. Without your downgrade>convert steps, the code initiates and places the first object at the extreme left edge of the page. The first object appears as a thick black vertical line segment with a thick blue border. Successes line segments get lighter. With your downgrade>convert change the first object appears as a thick black vertical line segment with a thin black border (you can't distinguish the border from the line at first) at the margin.
I think the issue in this case might be the graphics engine used in Word 2010 and higher, but that doesn't explain why it happens to us and not Paul. Perhaps Paul is using .doc format to start with. We won't know until he replies. |
#12
|
||||
|
||||
Previously, when I ran the test, I simply copied the code into a new document (docx format) and ran it. Time to execute: 0 seconds
I've now saved the document in the docm format, then closed, re-opened the document and re-run the macro from post #5. Time to execute: 2.0 seconds on first run, 11.0 seconds on the second run if I don't clear the output from the first run, 2.0-3.0 seconds otherwise. Curiously, if I change the two references to: ActiveDocument.Windows(1).Visible to: Application.ScreenUpdating Time to execute: 70 seconds! All runs in Print Layout view.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
|||
|
|||
Paul,
Can I assume that that your Normal template is macro free .dotx file? My Normal is a macro-enabled .dotm format file. I tried your test by opening Word then saving the new file as a .docx file then running the code, but I got the same slow result. Yes, it is brutally slow with just Application.ScreenUpdating = False |
#14
|
||||
|
||||
Actually it's a .dotm with numerous macros I use for various things.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#15
|
|||
|
|||
As is mine. This is a strange issue that I am certainly unable to explain.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
shapes in header | mhagi | Word VBA | 2 | 10-16-2015 12:21 AM |
Slow internet = Slow Word... | User12344321 | Word | 4 | 09-21-2015 12:54 PM |
All Shapes on slide | excelledsoftware | PowerPoint | 9 | 09-15-2013 04:03 AM |
Where did map shapes go? | SueK | PowerPoint | 1 | 01-20-2011 04:30 AM |
My Shapes some appear some don't | Jean-Paul | Visio | 0 | 03-01-2006 01:38 AM |