View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Limiting files to open to only files with certain name.

Dim myMsg as string

mymsg = Engineer_2.value & vblf & "You can only open ..."

....

msgbox mymsg

Brian wrote:

How would I add the message to read like this

MsgBox = msg Engineer_2.value "you can only a Open Installer Form", ,
"C.E.S."

Engineer_2.value is a value from a combo box.

I tried it but I get a compile Error: Expected End of Statement

What did i do wrong?

"Dave Peterson" wrote:

Check the name of the file after the user gives it to you--and only open it if
you think it's ok:

Option Explicit
' Open Existing Eng Spec 9 Control Button
Private Sub Open_Existing_Eng_Spec_9_Click()

Dim FileToOpen As Variant 'could be boolean
Dim bk As Workbook
Dim LastBackSlashPos As Long

FileToOpen = Application.GetOpenFilename("SPEC (*.xlsm), Spec*.xlsm")

If FileToOpen = False Then
MsgBox "The Open Method Failed, No Eng Spec was Opened", , "C.E.S."
Exit Sub
End If

LastBackSlashPos = InStrRev(FileToOpen, "\", -1, vbTextCompare)

If UCase(Mid(FileToOpen, LastBackSlashPos + 1, 4)) < UCase("SPEC") Then
MsgBox "must start with SPEC"
Exit Sub
End If

Set bk = Workbooks.Open(Filename:=FileToOpen)

End Sub

You may want to add a check for the extension or anything else you can think
of...

(instrrev was added in xl2k. If you're supporting xl97, then you'll have to
parse that name some other way (looping backwards from the last character until
you come to a backslash is as good as any).)

Brian wrote:

It works to open the file, but you can open any of the files no matter what
the name is.

"Rick Rothstein" wrote:

You didn't say where you wanted the "Spec" to be in the name, so I assumed
in the front...

FileToOpen = Application.GetOpenFilename("SPEC (*.xlsm), Spec*.xlsm")

Note that you had a misplaced quote mark in your example statement.

--
Rick (MVP - Excel)


"Brian" wrote in message
...
I have a User Form with a Control Button that is for opening Exsisting
Files.
I only want to Open Files with the word "Spec" in the name? Is there a way
to
only show & Open Excel Files with "Spec' in the name?

I have the following code that opens the Dialog Box, but it shows all the
files with the ".xlsm" extension. I want to narrow it down to only files
with
"Spec" in the name. It would be nice if it would show all the differnt
excel
file extensions.


' Open Existing Eng Spec 9 Control Button
Private Sub Open_Existing_Eng_Spec_9_Click()

FileToOpen = Application.GetOpenFilename("SPEC" (*.xlsm), *.xlsm")

If FileToOpen = False Then

MsgBox "The Open Method Failed, No Eng Spec was Opened", ,
"C.E.S."

Exit Sub

End If

Set bk = Workbooks.Open(Filename:=FileToOpen)
End Sub

.


--

Dave Peterson
.


--

Dave Peterson