Solved

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

Posted on 2014-01-18
3
2,934 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
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 …

815 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

9 Experts available now in Live!

Get 1:1 Help Now