Macro to browse and insert picture file in word

engineer0603

New Member
Joined
Jun 23, 2011
Messages
1
I am trying to write some code that upon pushing a command button, it prompts the user for the number of pics they want to insert into a word doc and then initiates a loop that opens a browse window to let the user choose each picture. The issue I am having is that after the user chooses the picture, Word gives me the error “Run-time error ‘5174’. This file could not be found. (-1).” And then subsequently tries to open the .jpg or .bmp file in a separate word file.

What is wrong in the code below that will not allow the browsed-for picture to be inserted into my word document?

Private Sub CommandButton5_Click()
'Adds pictures to document
Dim Count1 As Integer
Dim PictureNow As String
Dim InsertPics1 As Integer
InsertPics1 = InputBox("Question", _
"InsertPicsTitle", "How many pictures would you like to add?")
While Count1 < InsertPics1
Selection.GoTo Name:="Check82"
Selection.EndKey Unit:=wdLine
Selection.TypeParagraph

Selection.InsertFile FileName:=Dialogs(wdDialogFileOpen).Show

Selection.EndKey Unit:=wdLine
Selection.TypeParagraph

Count1 = Count1 + 1
Wend
End Sub


Aside from the code above I have also tested the code

Selection.InlineShapes.AddPicture FileName:="N:\Test_Pic.bmp", _
LinkToFile:=False, SaveWithDocument:=True

Which works fine if I want to insert a pre-determined picture, but this does not allow the user to browse to find that picture and I have not figured out how to incorporate the “browse” function into the above code.

Any help to accomplish my goal would be appreciated.

Thanks!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi engineer,


Try something along the lines of:
Code:
Private Sub CommandButton5_Click()
'Adds pictures to document
Dim Count1 As Integer
Dim InsertPics1 As Integer
With Selection
  InsertPics1 = InputBox("Question" & vbCr & _
    "How many pictures would you like to add?", _
    "InsertPicsTitle", 0)
  If InsertPics1 > 0 Then .GoTo Name:="Check82"
  While Count1 < InsertPics1
    .Collapse wdCollapseEnd
    Dialogs(wdDialogInsertPicture).Show
    .InsertAfter vbCr
    .Collapse wdCollapseEnd
    Count1 = Count1 + 1
  Wend
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,060
Messages
6,128,549
Members
449,458
Latest member
gillmit

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