VBA Application.Documents.Open with Dynamic Filename

jagrenet

Board Regular
Joined
Feb 23, 2022
Messages
81
Office Version
  1. 365
  2. 2013
  3. 2011
  4. 2010
Platform
  1. Windows
Hello Gurus,

I have a question regarding the process mentioned in the Title.
When working with the "Document.Open" method, I have one particular document that the filename changes or varies just slightly from client to client.
For instance -
wd.Documents.Open("C:\Monthly_Reports\Customer_01\My Customer Report - John.docx")
Or
wd.Documents.Open("C:\Monthly_Reports\Customer_02\My Customer Report - Mary.docx")
Or
wd.Documents.Open("C:\Monthly_Reports\Customer_03\My Customer Report - Bob.docx")

My question is, can I use an asterisk in place of the client's name, (John, Mary, Bob) to have the code still open that document? If so, what is the format for the asterisk ..... does it require quotation marks, or a dot?
Such as -
wd.Documents.Open("C:\Monthly_Reports\Customer_01\My Customer Report - "*".docx")
Or
wd.Documents.Open("C:\Monthly_Reports\Customer_01\My Customer Report - *.docx")

I suppose I am asking if the asterisk can be used as a wildcard character in the filepath string and is that even "allowed"?
Thanks in advance,
Jeff
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I don't believe your idea of a wildcard would work for Documents.Open, but you can work around that. Using the Dir function, you would be able to list out filenames/folders, and with that retrieved info, store the filename in a variable, then open it.
 
Upvote 0
Solution
I don't believe your idea of a wildcard would work for Documents.Open, but you can work around that. Using the Dir function, you would be able to list out filenames/folders, and with that retrieved info, store the filename in a variable, then open it.
Thanks for that !! - DIR seems to be exactly what I was looking for however, I have never used it and need to do some further research on it. Would you have any examples you can share in the meantime ??
 
Upvote 0
Did you mean something like this?

VBA Code:
Sub OpenCustomer()
    Dim wordapp: Set wordapp = CreateObject("word.Application")
    wordapp.Documents.Open ("C:\Monthly_Reports\Customer_02\" & Dir("C:\Monthly_Reports\Customer_02\My Customer Report*.docx"))
    wordapp.Visible = True
End Sub
 
Upvote 0
Did you mean something like this?

VBA Code:
Sub OpenCustomer()
    Dim wordapp: Set wordapp = CreateObject("word.Application")
    wordapp.Documents.Open ("C:\Monthly_Reports\Customer_02\" & Dir("C:\Monthly_Reports\Customer_02\My Customer Report*.docx"))
    wordapp.Visible = True
End Sub
Thanks Tupe77 !! - I will test this as soon as I get an opportunity and report back.
 
Upvote 0
Did you mean something like this?

VBA Code:
Sub OpenCustomer()
    Dim wordapp: Set wordapp = CreateObject("word.Application")
    wordapp.Documents.Open ("C:\Monthly_Reports\Customer_02\" & Dir("C:\Monthly_Reports\Customer_02\My Customer Report*.docx"))
    wordapp.Visible = True
End Sub
***UPDATE*** - Thank you Tupe77 - that worked brilliantly !! I also added a question mark in place of the "x" in .docx (.docx = .doc?). I found this useful if the file format is not always ".docx". Thanks again for your help !!
 
Upvote 0

Forum statistics

Threads
1,223,315
Messages
6,171,400
Members
452,398
Latest member
InvoicingNoob

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