Compressing InlineShapes in code

P

Peter Karlström

Hi

In Word 2003 there is a GUI to compress images in the document, and it is
very useful and effective. A screen dump document of 2 MB ends up to 180 kB
if this funtion is used.

Now I have a customer who want this function to be built in an existing
COM-Addin.

If I record a macro and run it the compression stage seems to be omitted, and
there is no Compress method in the InlineShape object.

How can this funtion be accessed from code?

Regards
 
J

Ji Zhou [MSFT]

Hello Peter,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

This issue is discussed before in the MSDN Forum. Unfortunately there is no
resolution available for the issue. There is no registry keys / VBA object
model available for "Compress Pictures" option.

However, there are some workarounds for it:

1.We can use the clipboard as the bridge to get the image and compress it
by ourselves, and then paste it back. The following is the code for
illustration. It just convert the picture's format to the .gif format:

Public Sub ButtonClick(ByVal Ctrl As Office.CommandBarButton, ByRef Cancel
As Boolean) Handles btn.Click
Try
applicationObject.Selection.InlineShapes(1).Select()
applicationObject.Selection.Copy()
Dim i As Image = Clipboard.GetImage()
Dim tempDir As System.IO.DirectoryInfo =
System.IO.Directory.CreateDirectory("C:\temp")
tempDir.Attributes = IO.FileAttributes.Hidden
Dim r As Integer = New Random().Next(10000)
i.Save(tempDir.FullName + "\" + r.ToString() + ".gif",
System.Drawing.Imaging.ImageFormat.Gif)
Dim newImage As Image = Image.FromFile(tempDir.FullName + "\" +
r.ToString() + ".gif")
Clipboard.SetImage(newImage)
applicationObject.Selection.Paste()
Clipboard.Clear()
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End Sub

2.We can use SendKeys.Send to automate Word to compress the inline picture:

Public Sub ButtonClick(ByVal Ctrl As Office.CommandBarButton, ByRef Cancel
As Boolean) Handles btn.Click
SendKeys.Send("{ENTER}")
SendKeys.Send("{ENTER}")
applicationObject.CommandBars("Picture").Controls("&Compress
Pictures...").Execute()
End Sub

3.If we are trying to compress a lot of pictures in a lot of documents. We
can save the document as HTML format. Word will call the compress function
for us by default. And all of the pictures and their compressed version
will be stored in the HTML page's corresponding folder.

Public Sub ButtonClick(ByVal Ctrl As Office.CommandBarButton, ByRef Cancel
As Boolean) Handles btn.Click
applicationObject.ActiveDocument.SaveAs("C:\test.html",
Word.WdSaveFormat.wdFormatHTML)
End Sub


If you have any future questions on this, please let me know!


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.
 
P

Peter Karlström

Hi Ji Zhou

Thank you for your reply.

I'm sure one of these techniques will do just fine.

Best Regards
--
Peter Karlström
Midrange AB
Sweden


"Ji Zhou [MSFT]" said:
Hello Peter,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you.

This issue is discussed before in the MSDN Forum. Unfortunately there is no
resolution available for the issue. There is no registry keys / VBA object
model available for "Compress Pictures" option.

However, there are some workarounds for it:

1.We can use the clipboard as the bridge to get the image and compress it
by ourselves, and then paste it back. The following is the code for
illustration. It just convert the picture's format to the .gif format:

Public Sub ButtonClick(ByVal Ctrl As Office.CommandBarButton, ByRef Cancel
As Boolean) Handles btn.Click
Try
applicationObject.Selection.InlineShapes(1).Select()
applicationObject.Selection.Copy()
Dim i As Image = Clipboard.GetImage()
Dim tempDir As System.IO.DirectoryInfo =
System.IO.Directory.CreateDirectory("C:\temp")
tempDir.Attributes = IO.FileAttributes.Hidden
Dim r As Integer = New Random().Next(10000)
i.Save(tempDir.FullName + "\" + r.ToString() + ".gif",
System.Drawing.Imaging.ImageFormat.Gif)
Dim newImage As Image = Image.FromFile(tempDir.FullName + "\" +
r.ToString() + ".gif")
Clipboard.SetImage(newImage)
applicationObject.Selection.Paste()
Clipboard.Clear()
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End Sub

2.We can use SendKeys.Send to automate Word to compress the inline picture:

Public Sub ButtonClick(ByVal Ctrl As Office.CommandBarButton, ByRef Cancel
As Boolean) Handles btn.Click
SendKeys.Send("{ENTER}")
SendKeys.Send("{ENTER}")
applicationObject.CommandBars("Picture").Controls("&Compress
Pictures...").Execute()
End Sub

3.If we are trying to compress a lot of pictures in a lot of documents. We
can save the document as HTML format. Word will call the compress function
for us by default. And all of the pictures and their compressed version
will be stored in the HTML page's corresponding folder.

Public Sub ButtonClick(ByVal Ctrl As Office.CommandBarButton, ByRef Cancel
As Boolean) Handles btn.Click
applicationObject.ActiveDocument.SaveAs("C:\test.html",
Word.WdSaveFormat.wdFormatHTML)
End Sub


If you have any future questions on this, please let me know!


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top