Word 2007: changing Shape properties very slow (compared to Word 2

D

dpomt

Hello,

in Word 2007, setting a Shapes property take quite long.
In my sample below, executing "FormatShape" takes 20 seconds on my machine.
I do not have the exactly value but this was quite faster in Word 2003.

Any ideas why this code executes so slow in Word 2007?

Thanks,
Dieter



--------------------------------------------------------------------
Dim sh1 As Shape
Set sh1 = ActiveDocument.Shapes.AddPicture(FileName:="c:\temp\image.jpg",
LinkToFile:=True, SaveWithDocument:=False)
Call FormatShape(sh1)
--------------------------------------------------------------------
Public Sub FormatShape(sh As Shape)
sh.Fill.Visible = msoTrue
sh.Fill.Solid
sh.Fill.ForeColor.RGB = RGB(255, 255, 255)
sh.Fill.Transparency = 0#
sh.Line.Weight = 0.75
sh.Line.DashStyle = msoLineSolid
sh.Line.Style = msoLineSingle
sh.Line.Transparency = 0#
sh.Line.Visible = msoFalse
sh.LockAspectRatio = msoTrue
sh.PictureFormat.Brightness = 0.5
sh.PictureFormat.Contrast = 0.5
sh.PictureFormat.ColorType = msoPictureAutomatic
sh.PictureFormat.CropLeft = 0#
sh.PictureFormat.CropRight = 0#
sh.PictureFormat.CropTop = 0#
sh.PictureFormat.CropBottom = 0#
sh.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
sh.RelativeVerticalPosition = wdRelativeVerticalPositionPage
sh.Left = CentimetersToPoints(0)
sh.Top = CentimetersToPoints(0)
sh.LockAnchor = False
sh.WrapFormat.AllowOverlap = True
sh.WrapFormat.Side = wdWrapBoth
sh.WrapFormat.DistanceTop = CentimetersToPoints(0)
sh.WrapFormat.DistanceBottom = CentimetersToPoints(0)
sh.WrapFormat.DistanceLeft = CentimetersToPoints(0)
sh.WrapFormat.DistanceRight = CentimetersToPoints(0)
sh.WrapFormat.Type = 3
sh.ZOrder 5

sh.ZOrder msoSendToBack
sh.ZOrder msoSendBehindText
end sub
--------------------------------------------------------------------
 
J

\Ji Zhou [MSFT]\

Hello dpomt,

I have a quick test in my side with the codes you post, but I cannot
reproduce the performance issue you described. Besides your codes, I have
added several lines to print the current time, which will help for
analyzing which line cost the most time.

Public Sub FormatShape(sh As Shape)
Debug.Print Now
sh.Fill.Visible = msoTrue
Debug.Print Now
sh.Fill.Solid
Debug.Print Now
sh.Fill.ForeColor.RGB = RGB(255, 255, 255)
Debug.Print Now
sh.Fill.Transparency = 0#
Debug.Print Now
sh.Line.Weight = 0.75
Debug.Print Now
sh.Line.DashStyle = msoLineSolid
Debug.Print Now
sh.Line.Style = msoLineSingle
Debug.Print Now
sh.Line.Transparency = 0#
Debug.Print Now
sh.Line.Visible = msoFalse
Debug.Print Now
sh.LockAspectRatio = msoTrue
Debug.Print Now
sh.PictureFormat.Brightness = 0.5
Debug.Print Now
sh.PictureFormat.Contrast = 0.5
Debug.Print Now
sh.PictureFormat.ColorType = msoPictureAutomatic
Debug.Print Now
sh.PictureFormat.CropLeft = 0#
Debug.Print Now
sh.PictureFormat.CropRight = 0#
Debug.Print Now
sh.PictureFormat.CropTop = 0#
Debug.Print Now
sh.PictureFormat.CropBottom = 0#
Debug.Print Now
sh.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
Debug.Print Now
sh.RelativeVerticalPosition = wdRelativeVerticalPositionPage
Debug.Print Now
sh.Left = CentimetersToPoints(0)
Debug.Print Now
sh.Top = CentimetersToPoints(0)
Debug.Print Now
sh.LockAnchor = False
Debug.Print Now
sh.WrapFormat.AllowOverlap = True
Debug.Print Now
sh.WrapFormat.Side = wdWrapBoth
Debug.Print Now
sh.WrapFormat.DistanceTop = CentimetersToPoints(0)
Debug.Print Now
sh.WrapFormat.DistanceBottom = CentimetersToPoints(0)
Debug.Print Now
sh.WrapFormat.DistanceLeft = CentimetersToPoints(0)
Debug.Print Now
sh.WrapFormat.DistanceRight = CentimetersToPoints(0)
Debug.Print Now
sh.WrapFormat.Type = 3
Debug.Print Now
sh.ZOrder 5
Debug.Print Now
sh.ZOrder msoSendToBack
Debug.Print Now
sh.ZOrder msoSendBehindText
Debug.Print Now
End Sub

The output in my side are as follows,
11/17/2008 4:02:22 PM
11/17/2008 4:02:22 PM
11/17/2008 4:02:22 PM
11/17/2008 4:02:22 PM
11/17/2008 4:02:22 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM
11/17/2008 4:02:23 PM

From my result, we can see that every line of the codes finishes in one
second. The performance issue does not occur in my side. To better support
this issue, would you mind clarifying about the detailed reproducible
environment,

1.Which OS version are you running, and have you applied the service pack 1
of the Word 2007?

2.Does the performance issue occurs on every machine you test or just some
machines in your side?

3.After running my codes, can we see a line cause the performance issue
apparently or every line executes slowly?

I am sorry to take these questions to you, but to do a future
investigation, I need to reproduce the issue in my side. So the
reproducible environment is very important to know. Thanks for your
understanding on this.

Have a good day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dpomt

Hello Ji,

thanks your reply.
1.Which OS version are you running, and have you applied the service pack 1
of the Word 2007?
MS Vista Ultimate SP1, Word 2007 SP1
2.Does the performance issue occurs on every machine you test or just some
machines in your side? yes


3.After running my codes, can we see a line cause the performance issue
apparently or every line executes slowly?
no


I have tested on different machines, with and without SP1 applied, same
behavior on all tested scenarios.

Here are my test results (I have done four runs on each machine):
machine 1: 10s/15s/18s/20s
machine 2: 12s/12s/16s/18s
When calling code multiple times for same document, execution time
additionally slows done. E.g. for machine 1, first took 10s while fourth run
already took 20s.

Here is the detailled debug output for run 1 on machine 1:

17.11.2008 15:58:24
17.11.2008 15:58:24
17.11.2008 15:58:25
17.11.2008 15:58:25
17.11.2008 15:58:26
17.11.2008 15:58:26
17.11.2008 15:58:26
17.11.2008 15:58:27
17.11.2008 15:58:27
17.11.2008 15:58:28
17.11.2008 15:58:28
17.11.2008 15:58:28
17.11.2008 15:58:28
17.11.2008 15:58:29
17.11.2008 15:58:29
17.11.2008 15:58:30
17.11.2008 15:58:30
17.11.2008 15:58:31
17.11.2008 15:58:31
17.11.2008 15:58:32
17.11.2008 15:58:33
17.11.2008 15:58:34
17.11.2008 15:58:34

I have done further tests with *other* images and now it's getting clearer.
When inserting small images , code executes in 1-2s also on my machine.
With large images, I get the slow execution times.
My large image is a 300dpi JPG with size 29,7cmx21cm (I place it in behind
the text). I was able to reproduce the behavior with a newly created JPG, so
it seems not to be dependant on the image itselft but on the size of the
image.

Best regards
dpomt
 
J

\Ji Zhou [MSFT]\

Hello dpomt,

Sorry for the delay and thanks for the detailed information you provided.
Yes, with a larger image, now I can reproduce the same issue as you
described. Based on my test, the speed of setting the picture shape's
properties is related to the image's scale size(height*width). When I drag
the picture shape to be a small scale one, the codes perform faster.

Actually, from my research, I have also found that, in the Word 2007, when
we use the VBA codes to set the picture shape's, the picture in the Word
document will be changed synchronously. That really costs the execution
time a lot, especially when the ScreenUpdating property is true. So, to
reduce the execution time, I tried to set the ScreenUpdating property to
FALSE and set it back to TRUE in the end. This approach improves the
performance to 6 seconds. And the executing time now is always to be 6s no
matter how many times we run our Macros. Please see my codes below,

Sub Test()
Dim sh1 As Shape
Set sh1 = ActiveDocument.Shapes.AddPicture(FileName:="D:\test.jpg",
LinkToFile:=False, SaveWithDocument:=True)
Call FormatShape(sh1)
End Sub

Public Sub FormatShape(sh As Shape)

Application.ScreenUpdating = False

Debug.Print Now & " Beginning"
Debug.Print Now & " Fill.Visible"
sh.Fill.Visible = msoTrue
Debug.Print Now & " Fill.Solid"
sh.Fill.Solid
Debug.Print Now & " Fill.ForeColor.RGB"
sh.Fill.ForeColor.RGB = RGB(255, 255, 255)
Debug.Print Now & " Fill.Transparency"
sh.Fill.Transparency = 0#
Debug.Print Now & " Line.Weight"
sh.Line.Weight = 0.75
Debug.Print Now & " Line.DashStyle"
sh.Line.DashStyle = msoLineSolid
Debug.Print Now & " Line.Style"
sh.Line.Style = msoLineSingle
Debug.Print Now & " Line.Transparency"
sh.Line.Transparency = 0#
Debug.Print Now & " Line.Visible"
sh.Line.Visible = msoFalse
Debug.Print Now & " LockAspectRatio"
sh.LockAspectRatio = msoTrue
Debug.Print Now & " PictureFormat.Brightness"
sh.PictureFormat.Brightness = 0.5
Debug.Print Now & " PictureFormat.Contrast"
sh.PictureFormat.Contrast = 0.5
Debug.Print Now & " PictureFormat.ColorType"
sh.PictureFormat.ColorType = msoPictureAutomatic
Debug.Print Now & " PictureFormat.CropLeft"
sh.PictureFormat.CropLeft = 0#
Debug.Print Now & " PictureFormat.CropRight"
sh.PictureFormat.CropRight = 0#
Debug.Print Now & " PictureFormat.CropTop"
sh.PictureFormat.CropTop = 0#
Debug.Print Now & " PictureFormat.CropBottom"
sh.PictureFormat.CropBottom = 0#
Debug.Print Now & " RelativeHorizontalPosition"
sh.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
Debug.Print Now & " RelativeVerticalPosition"
sh.RelativeVerticalPosition = wdRelativeVerticalPositionPage
Debug.Print Now & " Left"
sh.Left = CentimetersToPoints(0)
Debug.Print Now & " Top"
sh.Top = CentimetersToPoints(0)
Debug.Print Now & " LockAnchor"
sh.LockAnchor = False
Debug.Print Now & " WrapFormat.AllowOverlap"
sh.WrapFormat.AllowOverlap = True
Debug.Print Now & " WrapFormat.Side"
sh.WrapFormat.Side = wdWrapBoth
Debug.Print Now & " WrapFormat.DistanceTop"
sh.WrapFormat.DistanceTop = CentimetersToPoints(0)
Debug.Print Now & " DistanceBottom"
sh.WrapFormat.DistanceBottom = CentimetersToPoints(0)
Debug.Print Now & " WrapFormat.DistanceLeft"
sh.WrapFormat.DistanceLeft = CentimetersToPoints(0)
Debug.Print Now & " WrapFormat.DistanceRight"
sh.WrapFormat.DistanceRight = CentimetersToPoints(0)
Debug.Print Now & " WrapFormat.Type"
sh.WrapFormat.Type = 3
Debug.Print Now & " ZOrder"
sh.ZOrder 5
Debug.Print Now & " ZOrder"
sh.ZOrder msoSendToBack
Debug.Print Now & " ZOrder"
sh.ZOrder msoSendBehindText
Debug.Print Now & " Ending"

Application.ScreenUpdating = True
End Sub

Hope this helps more or less. And at this time, 6s seems to be the best
performance I manage to achieve. Would you mind letting me know if it can
fit your demand? For lacking of the Office product's debugging symbols, it
is really hard to investigate the internal cause of this performance issue.
Thus, I am also consulting the product team on this issue meantime to see
if anyone there can help on this. I will inform you as soon as I get any
reply from them.

Have a good day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dpomt

Hello Ji,

thanks for your reply and thanks for providing details about the
ScreenUpdating property. Code now executes in 5s at my machine, that's much
better :)

However, I still think this should be faster and hence I really appreciate
you have done a request to the product team. I am already excited about
hearing what they say on that issue.

Best regards
dpomt
 
J

\Ji Zhou [MSFT]\

Hello dpomt,

The product team is now aware of this issue and they are doing some more
investigation on it. And this issue is scheduled to be fixed in the next
version. Again, thanks very much for your feedback, it helps Microsoft to
improve its product a lot.

Have a nice day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dpomt

Hello Ji,
The product team is now aware of this issue and they are doing some more
investigation on it. And this issue is scheduled to be fixed in the next
version.
Thanks for your feedback and the great news. I am glad that issue will get
fixed.

Best regards
dpomt
 
Top