Page 320 - Microsoft Office Excel 2003 Programming Inside Out
P. 320

Microsoft Office Excel 2003 Programming Inside Out

                                    With FFs
                                        .Clear
                                        .Add “Pictures", “*.jpg"
                                    End With

                                    .AllowMultiSelect = True
             Chapter 13
                                    If .Show = False Then Exit Sub
                                     intCounter = 1
                                    For Each vaItem In .SelectedItems
                                        Worksheets(“ImagePreview”).Pictures.Insert _
                                            (.SelectedItems(intCounter))
                                        intCounter = intCounter + 1
                                    Next vaItem
                                 End With
                                 Exit Sub

                             Problem:
                                 MsgBox “You have not selected a valid picture."

                             End Sub
                             The AllowMultiSelect property is set to True, allowing the user to select multiple files. The
                             list box is cleared of any previous entries, and the For…Each Loop adds the items into the
                             FileDialogSelectedItems collection. The ListIndex property is set to 0 each time the user selects
                             a new file, and then the Change event procedure is executed loading the new image.

                             Inside Out

                             Determining if a File Exists Using FileDialog
                             Previously in this chapter, you saw how to determine if a file existed using the FileSearch
                             object. Now that you have reviewed the FileDialog object, review the following function.
                             The same results are returned, but a different method is used to achieve the same results.
                             The function will return True if the file exists and False if it isn’t found.

                             Function FileExists2(fname) As Boolean
                                 Set FileSys = CreateObject(“Scripting.FileSystemObject”)
                                 FileExists2 = FileSys.FileExists(fname)
                             End Function

                             The function created an instance of the FileSystemObject object. The FileSystemObject
                             gives you access to the computer’s file system. Once access is granted to the computer’s
                             file system, the function uses the FileExists property of the FileSys object to determine if
                             the file exists. The code is much simpler and more elegant than the earlier example.




                294
             Part 4:  Advanced VBA
   315   316   317   318   319   320   321   322   323   324   325