Click here to Skip to main content
14,174,238 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
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!
Posted
Updated 23-Jun-11 1:47am
v2
Comments
Engineer0603 24-Jun-11 9:21am
   
Uros, that was perfect, thank you!

However....I ran into another snag once this code worked and feel ridiculous that I cannot solve it.

When the CommandButton5_Click sub is activated, it clears all forms and check boxes earlier up in the document that were already filled out. How do I keep these forms and checks boxes from being cleared?

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

wdDialogFileOpen is used to open a Word document so when you use it to select an image it opens it in a new document. You can use Application.FileDialog to display a FileDialog[^] :

Private Sub CommandButton5_Click()
'Adds pictures to document
Dim Count1 As Integer
Dim PictureNow As String
Dim InsertPics1 As Integer
Dim dlgOpen As FileDialog
InsertPics1 = InputBox("Question", _
    "InsertPicsTitle", _
    "How many pictures would you like to add?")
Set dlgOpen = Application.FileDialog( _
    FileDialogType:=msoFileDialogFilePicker)
dlgOpen.AllowMultiSelect = False
While Count1 < InsertPics1
    
    Selection.GoTo Name:="Check82"
    Selection.EndKey Unit:=wdLine
    Selection.TypeParagraph
    
    If dlgOpen.Show = -1 Then
        Selection.InlineShapes.AddPicture _
            FileName:=dlgOpen.SelectedItems(1)
    End If
   
    Selection.EndKey Unit:=wdLine
    Selection.TypeParagraph
        
    Count1 = Count1 + 1
    
Wend
End Sub
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190524.3 | Last Updated 23 Jun 2011
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100