VBA Code - Picture (HD Resolution)

Spaztic

New Member
Joined
Jul 27, 2023
Messages
48
Office Version
  1. 365
Platform
  1. Windows
Hi! To insert pictures, I use the .Shapes.AddPicture so that the pictures aren't being treated as a hyperlink.

The issue I'm having is that when my code runs and it inserts a picture, sometimes the picture is grainy or if I adjust the picture, it is grainy. I noticed that 'use default resolution' is checked in the 'Compress Pictures' section.

It seems to be much better if I:
  • Select the picture
  • Picture Format
  • Compress Pictures
  • Select HD (330 ppi): good quality for high-definition (HD) displays
Is there code I can add when inserting the picture where the HD setting can be automatically applied to the picture?

VBA Code:
    Dim fNameAndPath As Variant
    Dim rng As Range
    Dim img As Shape
 
    fNameAndPath = Application.GetOpenFilename( _
        FileFilter:="Image Files (*.gif;*.jpg;*.png), *.gif;*.jpg;*.png", _
        Title:="Select an Image", _
        ButtonText:="Select")
      
    If fNameAndPath = False Then Exit Sub
 
    Set rng = ActiveCell
 
    Set img = ActiveSheet.Shapes.AddPicture(fileName:=fNameAndPath, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=0, Top:=0, Width:=-1, Height:=-1)
    With img
        If .Width > .Height Then
            .Width = rng.Width * 0.7
        Else
            .Height = rng.Height * 0.7
        End If
        .Left = rng.Left + (rng.Width - .Width) / 2
        .Top = rng.Top + (rng.Height - .Height) / 2
       .Placement = 1
       .PrintObject = True
    End With
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi Spaztic. No guarantees but you can trial this code. Adjust the sheet name to suit. HTH. Dave
VBA Code:
Sub test()
Sheets("Sheet1").Shapes.SelectAll
SendKeys "%e", True
SendKeys "~", True
SendKeys "{ENTER}"
Application.CommandBars.ExecuteMso "PicturesCompress"
End Sub
 
Upvote 0
This didn't work. What it did was change all the pictures to 'Email (96 ppi)' and I'm looking to change them to 'HD (330 ppi)'
 
Upvote 0
Hi again Spaztic. I did some more testing with the code and it seems like it compresses pictures in the first available format in the "Compress Pictures" dialog box. When you select the pics, is the HD (330ppi) compression available? The pics I trialed had different compression formats available. It did compress the pics in HD (330ppi) when it was available and it compressed others with the first available format available (eg. Print (220ppi)). I "borrowed" most of the code and adjusted it somewhat so I'm not real sure what to change if it's not working for you. Dave
 
Upvote 0
Found another bit of code which seems to so the same thing. Dave
VBA Code:
VBA.SendKeys "%W{ENTER}", True
Application.CommandBars.ExecuteMso ("PicturesCompress")
 
Upvote 0
Thanks,
What I found is that Excel is compressing the images automatically. When I go to File, More, Options, Advanced, Image Size and Quality...I see that it is compressing the images as they come in. When I check 'Do not compress images in file'... that is what I'm wanting when inserting pictures.

The issue is, this file will be used by many users and I don't want them to have to go and do this every time. I was hoping that I could 'un-compress' or block the original image from compressing through vba code.
 
Upvote 0
Interesting... I didn't know that was an option. If you select the "Do not compress images in file" option doesn't that stay with the file? I seem to have the same set up with my test file and when the pics are inserted they are at the default resolution and then after running the code the compression has changed. You could also adjust the default resolution to the 330 ppi so there would be no picture compression. That's all the suggestions I have. Good luck. Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,224,811
Messages
6,181,080
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top