Images, compression, & file size

Anonymous
2010-10-20T15:13:56+00:00

Hi Folks,

I have an Excel file I am working on with an image box that has click event code to insert a picture. I've been trying to include code to automatically compress the image. The file size gets huge.

The file size is 48 kb, if I insert a 5.0 MB JPEG image the file size jumps to 76 MB!

Generally I compress all images using MSO Picture Manager first to around 100 kb, which minimized the file size expansion, but I'm trying to set up the form so that file compression happens automatically.

One thing I noticed is that if I check the properties of the image box after inserting a JPG, it says the image = (bitmap), which if the jpeg is being converted to a bitmap on insertion it would explain why the file size gets so large (I think).

This is the image insertion code, SheetName and ImageName are public variables:

Private Sub ImageInsert()

' Photo insertion/removal

Dim NewImg, Delimg As Long

Application.ScreenUpdating = False

NewImg = MsgBox("Insert New Photo?", vbYesNoCancel)

    If NewImg = vbYes Then

        Worksheets(SheetName).OLEObjects(ImageName).PrintObject = True

        FileToOpen = Application.GetOpenFilename( _

            "All Files (*.jpg),*.jpg, All Files (*.bmp),*.bmp")

        If FileToOpen <> False Then

            Worksheets(SheetName).OLEObjects(ImageName).Object.Picture _

                = LoadPicture(FileToOpen)

            With Worksheets(SheetName).OLEObjects(ImageName).Object

                .BackColor = &H80000005

                .BorderStyle = fmBorderStyleNone

                .PictureSizeMode = fmPictureSizeModeZoom

                If Val(Application.Version) < 12 Then Run "Module1.CompressPics2003"

                If Val(Application.Version) >= 12 Then Run "Module1.CompressPics2007"

            End With

        End If

    ElseIf NewImg = vbNo Then

        If Worksheets(SheetName).OLEObjects(ImageName). _

                      Object.Picture Is Nothing Then

                      Worksheets(SheetName).OLEObjects(ImageName).PrintObject = False

                      GoTo Skip

            End If

        Delimg = MsgBox("Remove Current Photo?", vbYesNo)

            If Delimg = vbYes Then

                Worksheets(SheetName).OLEObjects(ImageName).Object.Picture = LoadPicture("")

                    With Worksheets(SheetName).OLEObjects(ImageName)

                        .Object.BackColor = &H8000000F

                        .Object.BorderStyle = fmBorderStyleSingle

                        .PrintObject = False

                    End With

            ElseIf Delimg = vbNo Then

            End If

    ElseIf NewImg = vbCancel Then

        End If

Skip:

Application.ScreenUpdating = True

End Sub

These are the subs for image compression:

Private Sub CompressPics2003()

    With Worksheets(SheetName).OLEObjects(ImageName)

        Application.CommandBars.FindControl(ID:=6382).Execute

        SendKeys "%A%W{ENTER}", False

    End With

End Sub

Private Sub CompressPics2007()

    Application.CommandBars.ExecuteMso "PicturesCompress"

End Sub

I realize there is some strange behavior inherent in Excel where OLE objects are concerned, but I'm hoping someone can offer a solution.

Thanks in advance for any help!

Scott

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Oldest
  1. Anonymous
    2010-10-20T16:18:45+00:00

    I wonder if excel suffers in the same way word does... If the image is resizes any compression of the image is lost.

    The work around was to size images to the required size, compress them and then insert them int he document... The document did not then bloat out as you appear to be experiencing.

    Just a work around, not a fix.


    Rik_UK Please mark the message or messages that answer your question as the "Answer" or vote if a reply has been helpful.

    Was this answer helpful?

    0 comments No comments
  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Anonymous
    2010-10-20T19:29:27+00:00

    suznal:

    For Excel 2007 use:

    Application.CommandBars.ExecuteMso "PicturesCompress"

    instead of:

    Application.CommandBars.FindControl(ID:=6382)

    That should solve your problem!

    Thanks for the suggestion & code - I think you may be right and I need to avoid using ActiveX image boxes because of the way Excel handles the OLEObjects.

    Was this answer helpful?

    0 comments No comments
  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more