?
Solved

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

Posted on 2014-01-18
3
Medium Priority
?
3,216 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:Bryce Bassett
[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 1200 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:Bryce Bassett
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

752 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