Macro to open insert object dialog box with if and with statements

darijokesar

New Member
Joined
Dec 23, 2016
Messages
5
Hope somebody would be able to resolve this puzzle. Please take a look at the code I have created. It works just fine but with one repeated action I cannot seems to get rid of. Idea behind Macro is to open Insert Object dialog box so I can choose a pdf file and insert it in the sheet. Also, to size and move it to my particular needs. If user cancels it would exit the sub. Now, everything works fine but for some reason when pdf file is selected dialog box closes and opens for second time and then when file again selected it gets placed in the sheet. Any help to get rid of this would be greatly appreciated

Code:
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sub InsertObject()

   Dim fullFileName As String
   fullFileName = Application.GetOpenFilename("*.*, All Files", , , , False)
   
   If fullFileName = "False" Then
   Exit Sub
  End If
   ActiveSheet.OLEObjects.Add(ClassType:="AcroExch.Document.DC", Link:=False, DisplayAsIcon:=False).Activate
   ActiveSheet.Shapes.SelectAll
   With Selection.ShapeRange
        .LockAspectRatio = msoFalse
        .Height = Application.InchesToPoints(14.22)
        .Width = Application.InchesToPoints(9.29)
        .Top = Application.InchesToPoints(26.91)
        .Left = Application.InchesToPoints(91.8750393701)
  End With
End Sub
</code>
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Since you didn't reference the filename you picked already when you added the object in the OLEObjects.Add line, it was asking for a name there.

Try this:
Code:
Sub InsertObject()
'http://www.mrexcel.com/forum/excel-questions/982357-macro-open-insert-object-dialog-box-if-statements.html
   Dim fullFileName As String
   fullFileName = Application.GetOpenFilename("*.*, All Files", , , , False)
   
   If fullFileName = "False" Then
   Exit Sub
  End If
[COLOR=#00ff00]   'added the fullFileName to this line will open that file picked above.  Included locations while adding the file.[/COLOR]
   ActiveSheet.OLEObjects.Add Filename:=fullFileName, Link:=False, DisplayAsIcon:=False, _
    Left:=Application.InchesToPoints(91.8750393701), _
    Top:=Application.InchesToPoints(26.91), _
    Width:=Application.InchesToPoints(9.29), _
    Height:=Application.InchesToPoints(14.22)


End Sub
 
Upvote 0
It worked for me, but your placement is pretty far out (92 inches right and 24 down?), which put it about EG125 with default spacing. See if it's there. You may want to test it with top and left about 1 inch.
 
Upvote 0
You were right Bill, I had all columns after K hidden so I missed it. Not sure how I got those inches of mine, funny thing it placed doc in right place with my original code. Anyways, I have a question about height and width...I see your code has no reference to aspect ratio. I would like to stretch document a little bit.
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,266
Members
448,558
Latest member
aivin

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