WalterInOz
Registered User.
- Local time
- Today, 17:29
- Joined
- Apr 11, 2006
- Messages
- 93
Hi all,
I'm stuck with some VBA code and am hoping you can give me some suggestions what the problem might be and (more importantly) how to solve it.
I have a template file on the server (StudyPlan.doc) that people need to fill in before they can start a new study. There's a button on the frmNewStudyPlan with the code below behind it. What I want to achieve is that when users click that button a copy of the template is placed in the folder ~Study_Plans on the server and the name of the new plan is username_date_StudyPlan. That part works fine.
Next thing I'd like to see is for MSWord to open that file. That part doesn't work. There's a problem with the path but I cannot figure it out. I suppose the problem lies in the variable part (username or date) of the path. If I replace the filepath in the code above with the name of the file it works fine. Cannot a path deal with these variables?
Anyone any suggestions?
Dim strMsg As String
Dim stDocName As String
Dim stLinkCriteria As String
Dim LWordDoc As String
Dim oApp As Object
Dim fileObj As Object
Dim strFileName As String
Dim strBackupFileName As String
'strFileName is Name of the Study Plan you want to save, add the .doc extension
strFileName = "StudyPlan.doc"
strBackupFileName = Format$(Nz(Environ("username")) & Format$(Now(), "yyyymmdd") & strFileName)
'path to database to back-up
strFileName = "R:\GLP_Study_docs\" & strFileName
'path to folder in which the back-up should be saved
strBackupFileName = "R:\~Study_Plans\New_Study_Plans\" & strBackupFileName
Set fileObj = CreateObject("scripting.filesystemobject")
fileObj.copyfile strFileName, strBackupFileName, True
DoEvents 'give system time to copy the file
'Path to the word document
LWordDoc = "R:\~Study_Plans\New_Study_Plans\ " & strBackupFileName
If Dir(LWordDoc) = "" Then
MsgBox "Document not found."
Else
'Create an instance of MS Word
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True
'Open the Document
oApp.Documents.Open FileName:=LWordDoc
End If
I'm stuck with some VBA code and am hoping you can give me some suggestions what the problem might be and (more importantly) how to solve it.
I have a template file on the server (StudyPlan.doc) that people need to fill in before they can start a new study. There's a button on the frmNewStudyPlan with the code below behind it. What I want to achieve is that when users click that button a copy of the template is placed in the folder ~Study_Plans on the server and the name of the new plan is username_date_StudyPlan. That part works fine.
Next thing I'd like to see is for MSWord to open that file. That part doesn't work. There's a problem with the path but I cannot figure it out. I suppose the problem lies in the variable part (username or date) of the path. If I replace the filepath in the code above with the name of the file it works fine. Cannot a path deal with these variables?
Anyone any suggestions?
Dim strMsg As String
Dim stDocName As String
Dim stLinkCriteria As String
Dim LWordDoc As String
Dim oApp As Object
Dim fileObj As Object
Dim strFileName As String
Dim strBackupFileName As String
'strFileName is Name of the Study Plan you want to save, add the .doc extension
strFileName = "StudyPlan.doc"
strBackupFileName = Format$(Nz(Environ("username")) & Format$(Now(), "yyyymmdd") & strFileName)
'path to database to back-up
strFileName = "R:\GLP_Study_docs\" & strFileName
'path to folder in which the back-up should be saved
strBackupFileName = "R:\~Study_Plans\New_Study_Plans\" & strBackupFileName
Set fileObj = CreateObject("scripting.filesystemobject")
fileObj.copyfile strFileName, strBackupFileName, True
DoEvents 'give system time to copy the file
'Path to the word document
LWordDoc = "R:\~Study_Plans\New_Study_Plans\ " & strBackupFileName
If Dir(LWordDoc) = "" Then
MsgBox "Document not found."
Else
'Create an instance of MS Word
Set oApp = CreateObject(Class:="Word.Application")
oApp.Visible = True
'Open the Document
oApp.Documents.Open FileName:=LWordDoc
End If