PDA

View Full Version : Solved: Open Word File Using Just Part of Filename?



clhare
06-29-2007, 05:38 AM
Is it possible to open a Word file using just the first part of the filename instead of the entire filename?

For example, I number my documents so I can use similar descriptions yet keep the first few characters different. So, can I open file "01" without having to use "01--This is the description.doc" in the code?

lucas
06-29-2007, 07:16 AM
Are you talking about a file open dialog...a userform...a shortcut...more possibilities but.....

clhare
06-29-2007, 07:59 AM
Actually, I have a template that gets info from the user and then automatically opens the appropriate file. I was just wondering if I had to put the entire filename in order to open it or if I could just indicate the first couple digits of the filename.

fumei
06-29-2007, 09:18 AM
Cheryl....think about this.

To answer your question strictly speaking, no, of course not. And it has nothing to do with Word. Word talks to the OS (Windows I assume). The OS retrieves the file from the filesystem on the hard drive. It does that by having the name.

The name. Not part of a name. The name. How could it be otherwise? The file name is the file name. To retrieve a file you need the path and name. So let's look at your question.

Is it possible to open a Word file using just the first part of the filename instead of the entire filename?

Answer: absolutely not.

For example, I number my documents so I can use similar descriptions yet keep the first few characters different. So, can I open file "01" without having to use "01--This is the description.doc" in the code?

Open "without having to use "01--This is the description.doc"?

Answer: absolutely not.

HOWEVER....Option Explicit

Const strFile As String = "This is the description.doc"

Sub OpenABunch()
Dim j As Long
Dim myPrefix As Long
Dim var
' say either a number you know
' or code that gets the number of doc files
' from a folder...or whatever
myPrefix = 0
j = 1
For var = 1 To your number
If j = 9 Then
j = 0
myPrefix = myPrefix + 1
End If
Documents.Open Filename:= myPrefix & j & strFile
' do whatever
j = j + 1
Next
End Sub

Result? The code "uses" the string "This is the description.doc" as a CONSTANT, and simply stick an incremented number at the front of it.

The code uses the full file name...because it has to.

Variables start as: myPrefix = 0; j = 1

Filename = 01strFile
Filename = 02strFile
Filename = 03strFile
Filename = 04strFile
Filename = 05strFile
Filename = 06strFile
Filename = 07strFile
Filename = 08strFile
Filename = 09strFile
If j = 9 Then
j = 0
myPrefix = myPrefix + 1
End If
j = 9, so it is changed to 0 AND myPrefix (0) has 1 added to it.....so myPrefix & j is 10......

Filename = 10strFile
Filename = 11strFile
Filename = 12strFile
Filename = 13strFile
Filename = 14strFile
- and so on.

To repeat, NO! You can not open any file, EVER, without its name.

But you certainly CAN do things so you can get, and "use", the name.

The above is only one possibility. Although any solution will be some sort of variation of a CONSTANT and an incremented counter. That is if your files are indeed "01yadda.doc", "02yadda.doc"

In other words, the rest of the filename IS exactly the same.

mvidas
06-29-2007, 10:28 AM
As Gerry said, you could always find it...

Quite simply (will return a blank string if no file found):Function GetFirstFile(ByVal vPath As String, ByVal StartOfFile As String) As String
GetFirstFile = Dir(vPath & IIf(Right(vPath, 1) <> "\", "\", "") & StartOfFile & "*")
End Function
Sub AnExample()
MsgBox "'" & GetFirstFile("C:\", "01-") & "'"
End Sub


To get a little more complex:
Function GetFirstFile(ByVal vPath As String, ByVal StartOfFile As String, _
Optional ByVal FileExtension As String = "*") As String
If Right(vPath, 1) <> "\" Then vPath = vPath & "\"
GetFirstFile = Dir(vPath & StartOfFile & "*" & FileExtension)
End Function
Sub AnExample()
MsgBox "'" & GetFirstFile("C:\", "01-", "doc") & "'"
End Sub

fumei
06-29-2007, 11:54 AM
Bit more complex indeed, and it only deals with the first named file. Actually, it only can deal with ONE file. The OP wishes something to deal with more than one file.

At least that is how I interpreted it.

Actually, I have a template that gets info from the user and then automatically opens the appropriate file. I was just wondering if I had to put the entire filename in order to open it or if I could just indicate the first couple digits of the filename.The answer to this is a careful Yes.

You need to clarify, qualify, "that gets info from the user ".

If the "info" is just the initial characters, AND the rest of the filename for the choices the user makes are identical - as in 01- PLUS This is a description.doc - then simply make the rest of the filename a CONSTANT....and use it.

Say, for example, you have a dropdown (say named FileList). It lists:

01
02
03
04

The user selects 03, and (I assume) a commandbutton.
Option Explicit

Const strFile As String = "-This is the description.doc"
Const strPath As String = "C:\Yadda\MyFolder\"
Sub CommandButton1_Click()
Documents.Open Filename:=strPath & FileList.Text & _
strFile
End Sub

Result?

Documents.Open _
Filename:="C:\Yadda\MyFolder\03-This is the description.doc"


User selects 02 from the dropdown, and clicks the commandbutton.

Result?

Documents.Open _
Filename:="C:\Yadda\MyFolder\02-This is the description.doc"

Or whatever. What exactly is the "info" gained from the user?

fumei
06-29-2007, 12:49 PM
OR......

Suppose you have multiple "constants".

Say in folder c:\yadda\money\ you have:

01-finances.doc
02-finances.doc
03-finances.doc
04-finances.doc

And in folder c:\yadda\staffing\ you have:

01-Staffing Stats.doc
02-Staffing Stats.doc
03-Staffing Stats.doc
04-Staffing Stats.doc
05-Staffing Stats.doc
06-Staffing Stats.doc

Option Explicit
Const strStartPath As String = "c:\yadda\"
Const strFinancePath As String = "money\"
Const strStaffingPath As String = "staffing\"
Const strFinanceFiles As String = "-finances.doc"
Const strStaffingFiles As String = "-Staffing Stats.doc"

' a dropdown (say named, FolderList) with a listing of
' "Finance documents"
' "Staffing documents"
' selecting Finance documents populates the FileList
' dropdown with 01, 02, 03, 04
' selecting Staffing documents populates the FileList
' dropdown with 01, 02, 03, 04, 05, 06

Sub cmdOpenDoc_Click()
' commandbutton to open selected doc
Select Case FolderList
Case "Finance document"
Documents.Open _
Filename:= strStartPath & strFinancePath & _
FileList & strFinanceFiles
Case "Staffing document"
Documents.Open _
Filename:= strStartPath & strStaffingPath & _
FileList & strStaffingFiles
End Select
End Sub

User selects "Staffing document", then 04.....

"c:\yadda\staffing\04-Staffing Stats.doc" is opened.

User selects "Finance documents", then 04.....

"c:\yadda\money\04-finances.doc" is opened.


This could also be used to batch process. You could add a commandbutton - ProcessAll.' previous declarations.....

Sub cmdProcessAll_Click()
Dim var
Select Case FolderList
Case "Finance document"
For var = 0 to FileList.ListCount - 1
Documents.Open _
Filename:= strStartPath & strFinancePath & _
FileList.List(var) & strFinanceFiles
' Call another sub to do whatever to EACH file
' eg. Call DoSomething
Next
Case "Staffing document"
For var = 0 to FileList.ListCount - 1
Documents.Open _
Filename:= strStartPath & strFinancePath & _
FileList.List(var) & strFinanceFiles
' Call another sub to do whatever to EACH file
' eg. Call DoSomething
Next
End Select
End Sub

Result?

User selects "Finance documents" and clicks the ProcessAll commandbutton.

c:\yadda\money\ 01-finances.doc is processed.
c:\yadda\money\ 02-finances.doc is processed.
c:\yadda\money\ 03-finances.doc is processed.
c:\yadda\money\ 04-finances.doc is processed.

User selects "Staffing documents" and clicks the ProcessAll commandbutton.

c:\yadda\staffing\ 01-finances.doc is processed.
c:\yadda\staffing\ 02-finances.doc is processed.
c:\yadda\staffing\ 03-finances.doc is processed.
c:\yadda\staffing\ 04-finances.doc is processed.
c:\yadda\staffing\ 05-finances.doc is processed.
c:\yadda\staffing\ 06-finances.doc is processed.

Here is the code to do the populating.Sub FolderList_Change()
Dim FinanceArray()
Dim StaffingArray()
FinanceArray = Array("01", "02", "03", "04")
StaffingArray = Array("01", "02", "03", "04", "05", "06")
Select Case FolderList
Case "Finance documents"
FileList.Clear
For var = 0 To UBound(FinanceArray)
FileList.AddItem FinanceArray(var)
Next
FileList.ListIndex = 0
Case "Staffing documents"
FileList.Clear
For var = 0 To UBound(StaffingArray)
FileList.AddItem StaffingArray(var)
Next
FileList.ListIndex = 0
End Select
End SubEvery time the FolderList (containing "Finance documents", "Staffing documents") is changed, the FileList dropdown changes as well.

The user can either select one file to process, or click process all, and all of them are processed.