Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have read the link here.


[^]


But now User want to change to put picture into the selected range.

How can I modify it.

Thanks all.

What I have tried:

Dim myRange As Range
  Dim width, height As Integer

  m = ActiveCell.Row
  n = ActiveCell.Column
  MyCol = ActiveCell.Column
  mystring = Selection.Address(False, False)
  MsgBox (mystring)
  Set myRange = Selection
  width = myRange.width
  height = myRange.height
  Dim fd As FileDialog

  Set fd = Application.FileDialog(msoFileDialogFilePicker)
  With fd
      .Filters.Clear
      .Filters.Add "Picture Files", "*.bmp;*.jpg;*.gif;*.png"
      .ButtonName = "Select"
      .AllowMultiSelect = False
      .Title = "Choose Photo"
      .InitialView = msoFileDialogViewDetails
      .Show
  End With
  ActiveSheet.Range(mystring).Select
  ActiveSheet.Shapes.AddPicture Filename:=fd.SelectedItems(1), _
  LinkToFile:=msoFalse, _
  SaveWithDocument:=msoCTrue, _
  Left:=ActiveSheet.Range("photograph2").Left + 2, _
  Top:=ActiveSheet.Range("photograph2").Top + 2, _
  width:=width, _
  height:=height
Posted
Updated 27-Mar-18 1:36am
Comments
Maciej Los 26-Mar-18 13:38pm    
What's wrong with your code?
hmanhha 27-Mar-18 3:23am    
I am don't know how to resize the pictures to fit the range that user select. Thanks.

1 solution

I tested this:
VB
Option Explicit 'force variable declaration


Sub InsertImageIntoSelection()
    'variables
    Dim rng As Range, wsh As Worksheet
    Dim sFileName As String
    
    'in case of error goto error handler
    On Error GoTo Err_InsertImageIntoSelection
    
    'get selection
    Set rng = Application.Selection
    'get worksheet
    Set wsh = rng.Parent
    'define filename
    sFileName = "C:\SomeImage.jpg"
    'add image into selection
    wsh.Shapes.AddPicture sFileName, msoFalse, msoTrue, _
                rng.Left, rng.Top, rng.Width, rng.Height
    
Exit_InsertImageIntoSelection:
    On Error Resume Next 'ignore errors
    'clean up!
    Set rng = Nothing
    Set wsh = Nothing
    Exit Sub
    
Err_InsertImageIntoSelection:
    MsgBox Err.Description, vbExclamation, Err.Number
    Resume Exit_InsertImageIntoSelection
End Sub

and it works great!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900