CommonDialog Reference Issue

kenrav

New member
Local time
Today, 16:59
Joined
Jun 20, 2020
Messages
29
I'm trying to create a function that lets users to scan a document and save it to a folder.

I start with "Dim scanDiag As New WIA.CommonDialog"

However, it throws a ""User-defined type not defined" error.

I am referencing the Microsoft Office 16.0 Object Library.

Thanks for your help. If you know of some code that works, that would be fine as well.

Ken
 
Hi. Welcome to AWF!

What is CommonDialog used for? If it's to browse for a file path, then you could try using the FileDialog object instead.

 
Common Dialog is an old feature.


Look at the FileSystemObject because there are ways to get it to give you a file-picker dialog object.
 
Here is code that I use, this function returns the full file path of the file the user selected.

It requires no references - which is highly preferable, since your users can be on different Office versions and late binding is key to avoiding those problems.

Code:
Function GetUserSelectedAttachment() As String
Dim strFilePath As String
Dim objFD As Object ' Application.FileDialog

' get the file
Set objFD = Application.FileDialog(3) ' msoFileDialogFolderPicker)
With objFD
    'setup File Dialog
    .AllowMultiSelect = False
    .ButtonName = "Select"
    .InitialView = 1 ' msoFileDialogViewList JAH 20110422
    .Title = "Select the attachment"

    'display file dialog box
    If .Show Then
        strFilePath = .SelectedItems(1)
    End If
End With

If (Right(strFilePath, 4) <> "xlsb") And (Right(strFilePath, 4) <> "xlsm") Then 'optional test I use for certain scenario
    GetUserSelectedAttachment = "Invalid"
    Exit Function
End If

GetUserSelectedAttachment = strFilePath

End Function

Credit to JAH , although I can't remember at the moment who that is.
 
Last edited:
Ken,
You need to reference the WIA library, that is the scanner dialog, not the old deprecated common file dialog:

Cheers,
Vlad
 

Users who are viewing this thread

Back
Top Bottom