2016-08-25 13 views
0

特定の範囲に画像を挿入しようとしていますが、その画像を元の寸法で挿入します。Excelの元の寸法の範囲に画像を追加する

次のコードは正常に動作しますが、画像のサイズが変更されます。コメントの

Sub InsertPictureInRangeAntes(path As String, PictureFileName As String, TargetCells As Range) 
'inserts a picture and resizes it to fit the TargetCells range 
Dim p As Shape, t As Double, l As Double, w As Double, h As Double 
    If dir(path, vbDirectory) = "" Then 
     MsgBox "Doesn't exists an image in this path", vbInformation 
     Exit Sub 
    Else: 
     path = path & PictureFileName 
    End If 
    'import picture 
    Set p = ActiveSheet.Shapes.AddPicture(Filename:=path, linktofile:=msoFalse, _ 
     savewithdocument:=msoCTrue, Left:=l, Top:=t, Width:=w, Height:=h) 
    'determine positions 
    With TargetCells 
     t = .Top 
     l = .Left 
     w = .Offset(0, .Columns.Count).Left - .Left 
     h = .Offset(.Rows.Count, 0).Top - .Top 
    End With 
    'position picture 
    With p 
     .Top = t 
     .Left = l 
     .Width = w'I dont know how to take the original dimensions 
     .Height = h 
    End With 
    Set p = Nothing 
End Sub 

任意の質問のポストを!

+0

使用AddPictureのチェックアウト[この](https://social.msdn.microsoft.com/Forums/office/en-US/5f375529-a002-4312-a54b-b70d6d3eb6ae/how-to-retrieve-image-dimensions-vba-?forum = accessdev)を使用します。画像のサイズをインポートする前にそれを判断するのに非常に良い方法のようです。 – Kyle

+0

@Kyle私はthxをチェックしています – TimeToCode

答えて

1

代わりPictures.Insert

Sub addPicture() 

    Dim pct 

    Set pct = Worksheets("Sheet1").Pictures.Insert("H:\My Documents\My Pictures\abc.jpg") 

    '/ Set Top,Left etc if required. 
    pct.Top = 1 
    pct.Left = 10 

End Sub 
+0

うまく働く、サポートのためのthx! – TimeToCode

関連する問題