Modify code

Zahhhaaaa

Board Regular
Joined
Jun 29, 2011
Messages
62
Is it possible to modify this code so that it allows me to select .jpg-pictures?

Code:
   Sub loopyarray()

   Dim filenames As Variant

   ' set the array to a variable and the True is for multi-select
   filenames = Application.GetOpenFilename(, , , , True)

      counter = 1

      ' ubound determines how many items in the array
      While counter <= UBound(filenames)

         'Opens the selected files
         Workbooks.Open filenames(counter)

         ' displays file name in a message box
         MsgBox filenames(counter)

         'increment counter
         counter = counter + 1

      Wend
   End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hello,

Code:
filenames = Application.GetOpenFilename("Pictures (*.jpg; *.jpeg),*.jpeg", Title:= _
                    "Please select a file")
 
Upvote 0
Thanks for responce.. but it gives me an error for this line "While counter <= UBound(filenames)", runtime error 13, type mismatch.. Did i miss something...?
 
Upvote 0
One more question too, how can I modify this code that it opens me a messagebox and allows me to browse thru folders, and then select wanted file? I need to insert pictures and folder for pics can be different sometimes, that's why I want that program opens me a messagebox so I can find them by myself

Code:
Sub InsertPicture()
    InsertAllPix Range("A50"), _
                "J:\Excel\Kuvat\", _
                "*.jpg"
End Sub
 
Upvote 0
Hello,

Choosing cancle on the getopenfilename dialog sets the variable filenames to false, which would then return the error for me.

I have not choosen "OK" so I cannot say what happens in the loop.

The below brings up an Inputbox for file path and for drive. From there I am able to browse folders. I am using Vista and Excel 2007.

See how this does for you:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> loopyarray()<br><br><SPAN style="color:#00007F">Dim</SPAN> filenames   <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> counter     <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> fPath       <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> fDrive      <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>     <br>     <SPAN style="color:#007F00">'''Drive to look in, change default to suit</SPAN><br>fPath = InputBox("Input file path:", "fPath", "C:\Users\Standard Account\Pictures")<br>    <SPAN style="color:#007F00">'''File Path, change default to suit</SPAN><br>fDrive = InputBox("Drive?", "Select a Drive", "C")<br>    <br>    ChDrive (fDrive)<br>    ChDir (fPath)<br>        <SPAN style="color:#00007F">With</SPAN> Application<br>            filenames = .GetOpenFilename("Pictures (*.jpg; *.jpeg),*.jpeg", Title:= _<br>                            "Please select a file", MultiSelect:=True)<br>            ChDrive (Left(.DefaultFilePath, 1)) <SPAN style="color:#007F00">'''Return to Default</SPAN><br>            ChDir (.DefaultFilePath) <SPAN style="color:#007F00">'''Return to Default</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">If</SPAN> filenames = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'''File not found or Cancle was pressed (avoids error)</SPAN><br>    <br>    counter = 1<br><br>      <SPAN style="color:#007F00">' ubound determines how many items in the array</SPAN><br>          <SPAN style="color:#00007F">While</SPAN> counter <= <SPAN style="color:#00007F">UBound</SPAN>(filenames)<br>    <br>             <SPAN style="color:#007F00">'Opens the selected files</SPAN><br>             Workbooks.Open filenames(counter)<br>    <br>             <SPAN style="color:#007F00">' displays file name in a message box</SPAN><br>             MsgBox filenames(counter)<br>    <br>             <SPAN style="color:#007F00">'increment counter</SPAN><br>             counter = counter + 1<br>          <SPAN style="color:#00007F">Wend</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

-Jeff
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,144
Members
452,891
Latest member
JUSTOUTOFMYREACH

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