Solved

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

Posted on 2014-01-18
3
2,796 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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes, it should have been If srcPDF = "" Then. Sorry about that.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
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 teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.

772 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

10 Experts available now in Live!

Get 1:1 Help Now