OK, I didn't read that far down after I found the first problem. You have
several more problems in the code you originally posted:
- The expression Application(strNetworkPath).Found is meaningless in VBA. If
you want to know whether a folder exists, you can use the Dir() function
with the proposed folder name as the first argument and the constant
vbDirectory as the second argument. If the folder exists, the return value
of the function is the folder name; if it doesn't exist, the return function
is the empty string ("").
- To do that test, you should not have the backslash at the end of the
folder name; instead, make it part of the file name when you append that to
the path.
- The file name (Letter.dot) needs to be enclosed in double quotes. The rule
is: literal strings are enclosed in quotes, variable names are not enclosed
in quotes.
- You had a Dim statement naming a variable strname that was never used, but
no Dim statement for the variable strFile that was used. To avoid this sort
of misstep, always include the Option Explicit statement at the beginning of
each module (see
http://www.word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm).
- Although you're trying to make sure the correct folder is used, you
haven't checked that the template actually exists in the folder. You could
use the Dir() function again, with the vbNormal constant in the second
argument, or you can simply set an On Error statement to trap the problem
and handle it. The following sample code shows how that can work.
Sub OpenLetter()
Dim strNetworkPath As String
Dim strCPath As String
Dim strFile As String
strNetworkPath = "G:\Documents and Settings\" & Environ("USERNAME") &
"\Application Data\Microsoft\Templates\My Templates"
strCPath = Environ("USERPROFILE") & "\Application
Data\Microsoft\Templates\My Templates"
' Opens document based on letter template
If Dir(strNetworkPath, vbDirectory) <> "" Then
strFile = strNetworkPath & "\Letter.dot"
Else
strFile = strCPath & "\Letter.dot"
End If
On Error GoTo BadTemplate
Documents.Add Template:=strFile, NewTemplate:=False, DocumentType:=0
Exit Sub
BadTemplate:
MsgBox "Couldn't find " & strFile
End Sub
> Hi Jay
>
[quoted text clipped - 72 lines]
>>>
>>> End Sub
aehan - 18 Dec 2007 09:10 GMT
Hi Jay
Thank you so much for helping me, I appreciate not only the fact that you
showed me what to do, but also that you explained where I had gone wrong -
that really helps.
Have a great Christmas.
Aehan
> OK, I didn't read that far down after I found the first problem. You have
> several more problems in the code you originally posted:
[quoted text clipped - 130 lines]
> >>>
> >>> End Sub