Select File from Directory Query not working

gruntingmonkey

Active Member
Joined
Mar 6, 2008
Messages
444
Office Version
  1. 365
Platform
  1. Windows
I have some code which is opening up a file directory from Excel but I have a couple of issues.
Primarily it debugs when the file is being set (Bolded below). I get a type mismatch and I cant figure it out why!

Further to that, and this isnt important really, but is there a way of limiting results to word files, ie excel, PDFs etc dont show in the directory to be choosen?


Code:
Sub SelectProposalFromDirectory()

Dim SummaryWB As Documents
Dim vrtSelectedItem As Variant
Dim UserNm As String

UserNm = (Environ$("Username"))

With Application.FileDialog(msoFileDialogOpen)
    .InitialFileName = "C:\Users\" & UserNm & "\Workplace Name\Sales - Documents\Proposals\"
    .AllowMultiSelect = False
    .Show
    For Each vrtSelectedItem In .SelectedItems
       [B] Set SummaryWB = Documents.Open(vrtSelectedItem)[/B]
    Next
End With

If SummaryWB Is Nothing Then Exit Sub


End Sub

Any help would be appreciated!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
SummaryWB should not be declared as Documents.

I think it should be declared as Document but I'm not sure as it's not clear where you are running this code or what type of files you are working with.
 
Upvote 0
When changed to Document it brings up a "Object required" error.

The code is literally just a button in Excel to bring up a folder location on Sharepoint. My intentions is to open a word file.

SummaryWB can be changed if required.
 
Last edited:
Upvote 0
If this code is in Excel then Document/Documents don't really exist.

If you want to open a Word document with code in Excel you need to an instance of Word.

Try something like this.
Code:
Option Explicit

Sub SelectProposalFromDirectory()
Dim wrdApp As Object
Dim SummaryWB As Object
Dim UserNm As String

    UserNm = Environ$("Username")

    With Application.FileDialog(msoFileDialogOpen)
    
        .InitialFileName = "C:\Users\" & UserNm & "\Workplace Name\Sales - Documents\Proposals\*.doc*"
        
        .AllowMultiSelect = False
        
        If .Show = -1 Then
            Set wrdApp = CreateObject("Word.Application")
            Set SummaryWB = wrdApp.Documents.Open(.SelectedItems(1))
            wrdApp.Visible = True
        Else
            MsgBox "Cancelled!", vbInformation
        End If
        
    End With

End Sub
 
Upvote 0
Oh, that makes sense now from something I learned earlier today.

Thank you so much for your help.
 
Upvote 0
Actually, Is there a simple way of also getting the filepath and name at at the same time?
 
Upvote 0
Not sure what you mean.:eek:

The way you are getting the path in the code seems pretty straightforward.
 
Upvote 0
....I'm being slow today.

Thats placing the location into the file dialog. I want it once a file has been chosen. Something like.

filepath="C:\Users" & UserNm & "\Blah Blah\Sales - Documents\Proposals\Cheese\Sandwich"
filename=(.SelectedItems(1)) 'yum.doc

I think thats what I want!
 
Last edited:
Upvote 0
Try this.
Code:
Option Explicit

Sub SelectProposalFromDirectory()
Dim wrdApp As Object
Dim SummaryWB As Object
Dim UserNm As String
Dim strFileName As String
Dim strFullPath As String
Dim strPath As String

    UserNm = Environ$("Username")

    With Application.FileDialog(msoFileDialogOpen)
    
        .InitialFileName = "C:\Users\" & UserNm & "\Workplace Name\Sales - Documents\Proposals\*.doc*"
        
        .AllowMultiSelect = False
        
        If .Show = -1 Then
        
            strFullPath = .SelectedItems(1)
            
            strFileName = Mid(strFullPath, InStrRev(strFullPath, "\") + 1)
            strPath = Replace(strFullPath, strFileName, "")
            
            Set wrdApp = CreateObject("Word.Application")
            Set SummaryWB = wrdApp.Documents.Open(strFullPath)
            wrdApp.Visible = True
        Else
            MsgBox "Cancelled!", vbInformation
        End If
        
    End With

End Sub
 
Upvote 0
Thanks Norie. I really appreciate the update on this. I can definitely work with this now to get to my goal!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top