# Macro to browse and insert picture file in word



## engineer0603 (Jun 23, 2011)

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!


----------



## Macropod (Jun 24, 2011)

Hi engineer,


Try something along the lines of:

```
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
```


----------

