Solved

Word 2010 VBA - How to set file type in wdDialogFileOpen?

Posted on 2014-01-18
3
2,878 Views
Last Modified: 2014-01-18
I'm using Word's built-in File Open dialog to let the user select a PDF file.  I'm only using the dialog to intercept the file name, then actually displaying it within a PDF viewer window.

I'd like my code to limit the list of files they see to PDF files, the equivalent of manually setting this drop-down arrow on the right.  
Open File Dialog set to PDF
Here's my code, and a few things that work for other types of dialogs that don't seem to work here.

With Dialogs(wdDialogFileOpen)
       ' .Format = wdFormatPDF  'does not work
       ' .Filter = "PDF Files (*.pdf)"  'does not work
       ' .Name = "*.PDF*"  'does not work
        If .Display Then
            If .Name <> "" Then
                sourcepdf = .Name
            End If
        End If
    End With

Open in new window


It works fine, but shows all files.  Any ideas?
0
Comment
Question by:versatilebb
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Steven Harris earned 400 total points
ID: 39791590
You may find it easier with msoFileDialogOpen.  This way, you can clear the Filter and only add .pdf back in:

Sub OpenFile()
    Dim dlgOpen As FileDialog
    Dim srcPDF As String
    Set dlgOpen = Application.FileDialog(FileDialogType:=msoFileDialogOpen)
    
    With dlgOpen
        .Filters.Clear
        .Filters.Add "PDF Files", "*.pdf"
        .AllowMultiSelect = False
        .Show
        srcPDF = .SelectedItems(1)
    End With
    
    If srcPDF <> "" Then
        Exit Sub
    End If
    'run your code against srcPDF here
End Sub

Open in new window

0
 

Author Comment

by:versatilebb
ID: 39791623
That did the trick.    But did you mean to say if sfcPDF = """ then exit sub?  I also had to put in a statement to get it to proceed if they pick Cancel in the dialog.  This seems to work:

Dim srcPDF As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(FileDialogType:=msoFileDialogOpen)
    
    With dlgOpen
        .Filters.Clear
        .Filters.Add "PDF Files", "*.pdf"
        .AllowMultiSelect = False
        .Show
        On Error Resume Next
        srcPDF = .SelectedItems(1)
    End With
    On Error GoTo 0
    
    If srcPDF = "" Then
        Exit Sub
    End If

Open in new window

0
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39791630
Yes, it should have been If srcPDF = "" Then. Sorry about that.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now