Attn: Jon Peltier . . .Nayan
I will address this to Jon, but Nayan please understand it is to both of you.
So far, I have found 3 different paths to "My Documents" and I am now sure
there are many more.
Here is the code I used from your example. It works fine on my PC which has
"My Documents" located at: C:\documents and settings\default\my
documents\computerclasshandouts.xls
.. But Naturally, it doesn't work if "My Documents" is in any other path,
such as: C:\My Documents
.. Or, C:\.....\.........\My Documents.
Needless to say, if "My Documents" is located in any other path it will not
work.
1st a QUESTION: Shouldn't the ("MyDocuments") used in the Function, at the
end of ThePath = . . . have a space between the two words?
Function MyDocDirectory() As String
Dim WSHShell As Object
Dim ThePath As String
Set WSHShell = CreateObject("WScript.Shell")
ThePath = WSHShell.SpecialFolders("MyDocuments")
MyDocDirectory = ThePath
Set WSHShell = Nothing
End Function
Before we go further, I think I had better explain the usage of this Macro.
I have developed a Menu System that allows my clients to go anyplace to open
a file or link to another Excel page(s) in My Menu Workbook. This also
includes links to any website etc, etc, etc.
What I need this particular Macro to do is open a new instance of Excel along
with any existing Excel Workbook separate from My Menu. The user will copy
the Master Macro, rename the macro and add the name of their workbook to the
macro, replacing \classhandouts.xls.
When this macro Button is clicked, Excel opens in a new instance along with
the named workbook.
The user can have as many macros as needed to open different workbooks that
they frequently use.
I think as a general rule, 25 to 35 files will be the max that they use and
regardless of how many workbooks they have, they all can be in the "My
Documents" folder, as the user will have no need of searching for any file
they want to use. Again, there will be some people that this isn't practical
for, but I have talked with many users and with my many years of working with
Excel, this approach will save most people considerable time. Naturally, my
problem is that I am not a good programmer.
You wrote this note: You need to find the "My Documents" directory and then
see if the file is there . . . then proceed.
I agree with approach, but somehow the path to "My Documents" directory is
needed so that the routine then looks in the appropriate path which ends with
"My Documents" to open the file specified. My users will have access to this
routine and will modify the document name. See suggestions on: sFileName
below.
Here is what I tried from Jon's suggestion
Sub NewExcelWithWorkbook()
Dim sPath As String
Dim sFileName As String
Dim sFullName As String
Dim oXL As Object
Dim oWB As Object
sFileName = "classhandouts.xls" {EX: .....or..... "markshobbies.xls" .....
or....."timeshare2.xls"}
sPath = MyDocDirectory
sFullName = sPath & "\" & sFileName
If Len(Dir(sFullName)) = 0 Then
MsgBox "File " & sFileName & " was not found in " & sPath
Exit Sub
End If
Set oXL = CreateObject("Excel.Application")
oXL.Visible = True
Set oWB = oXL.Workbooks.Open("C:\documents and settings\default\my
documents\computerclasshandouts.xls")
End Sub
It only works when using the path described just above here.
Although I am not capable of writing the code, I will give my thoughts only
for the possibility of helping.
I think that 1st the "My Documents" directory has to be located along with
it's path.
This path would be stored in sPath.
The workbook to be opened is stored in sFileName.
Since the path and file name are now known, the procedure should beable to
open it. ??????
I bow to your far superior knowledge of programming over myself. Please help
me if you will.
I will be more than happy to respond to any questions you may have.
Regards
Matt@Launchnet
Jon said:
You need to find the My Documents directory, then see if the file is there,
then proceed.
Add this function to the module:
Function MyDocDirectory() As String
Dim WSHShell As Object
Dim ThePath As String
Set WSHShell = CreateObject("WScript.Shell")
ThePath = WSHShell.SpecialFolders("MyDocuments")
MyDocDirectory = ThePath
Set WSHShell = Nothing
End Function
Add these declarations to your procedure:
Dim sPath as String
Dim sFileName as String
Dim sFullName as string
Find the directory using the function above and check for file using Dir()
SFileName = "classhandouts.xls"
SPath = MyDocDirectory
sFullName = sPath & "\" & sFileName
If Len(Dir(sFullName)) = 0 then
MsgBox "File " & sFileName & " was not found in " & sPath
Exit Sub
End If
proceed from here through your procedure. You probably want to do this check
before creating the new instance of Excel.
If you want something real advanced, you can then offer the user the
opportunity to browse for the file after the message box instead of exiting.
This is most useful, because a semi-sophisticated user will not simply dump
all of their files into My Documents, but will probably have a hierarchy of
directories, e.g.,
...\My Documents\School\Math Class\classhandouts.xls
This helps keep the schoolwork separate from the MP3 files.
Dim iMsgAnswer as Long
Dim sMessage as String
If Len(Dir(sFullName)) = 0 then
sMessage = "File " & sFileName & " was not found in " & sPath
sMessage = sMessage & vbNewLine & vbNewLine
sMessage = sMessage & "Do you want to browse for it?"
iMsgAnswer = MsgBox (sMessage, vbQuestion + vbOKCancel)
If iMsgAnswer = vbCancel Then Exit Sub
sFullName = CStr(Application.GetOpenFileName("Excel Workbooks
(*.xls),*.xls"))
If sFullName = "False" then Exit Sub
If Len(Dir(sFullName)) = 0 then Exit Sub
End If
- Jon
-------
Jon Peltier, Microsoft Excel MVP
Peltier Technical Services, Inc.
774-275-0064
208-485-0691 fax
(e-mail address removed)
http://PeltierTech.com/
_______
I have the following Excel macro:
[quoted text clipped - 26 lines]
Thanks Much
Matt@Launchnet