Solved

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

Posted on 2014-01-18
3
3,099 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:

732 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