Hi,
I'm using VB macros in microsoft Word Templates to pull client ID and name and place on the document. I have begun today to modify my macro to open "SaveAs" dialogue window. That part is working correctly. However, in my SaveAs window i am coding the file name to pull the Name , document type and file type. When I added a variable dtdate to the file name I get " Run Time Error "13" Type Mismatch. The variable is dtdate and I set it equal to "Now" or "Date". Please find below my code.
'==================================
'==================================
Private Sub cmdSave_Click()
pgnum = 1
namevar = frmSave.txtname.text
idvar = frmSave.txtID.text
dtdate = Now
Proceedvar = 1
Dim oldname, savFileName As Boolean
'THE NEXT BUTTON WAS JUST PRESSED
'check to see that all fields are filled in with something
'If a field is blank do not proceed
If frmSave.txtID = "" Then
MsgBox ("You need to fill in a Patient ID Number")
txtID.SetFocus
Proceedvar = 0
End If
If frmSave.txtname = "" Then
MsgBox ("You need to fill in a name")
Proceedvar = 0
txtname.SetFocus
End If
'
If Proceedvar = 1 Then ' TESTED FOR BLANK FIELDS
' Now that all fields are filled
' Move collected data to document
While pgnum < 3
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=pgnum
Selection.Find.Replacement.ClearFormatting
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With Selection.Find
.text = "(name)"
.Replacement.text = namevar
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Replace the patien's ID with data from the form
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "(number)"
.Replacement.text = idvar
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
pgnum = pgnum + 1
Wend
Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, Name:=1
' next we will bring up the "Save As" dialog box
cd1.Filter = "Microsoft Word Document (*.doc)"
UserName = Environ("username")
Pathvar = CurDir
Testvar = "I:\" + UserName
Mylen = Len(Testvar)
MyNew = Mid(Pathvar, 1, Mylen)
Mycheck = Pathvar Like Testvar
If MyNew = Testvar Then
cd1.InitDir = Pathvar
Else
cd1.InitDir = Testvar
End If
' if the user clicks cancel on the Save As dialog box,
' we do not want to save
With Selection.Find
.text = cd1.InitDir
.Replacement.text = thisdir
End With
cd1.FileName = namevar + dtdate + "_Format" + ".doc"
'Name savFileName As InitDir + cd1.FileName + ".doc"
cd1.ShowSave
ActiveDocument.SaveAs FileName:=cd1.FileName
frmSave.Hide
End If ' Ends the Test for blank fields
' Go to the end of the Document and prepare for note entry
End Sub
Thanks
I'm using VB macros in microsoft Word Templates to pull client ID and name and place on the document. I have begun today to modify my macro to open "SaveAs" dialogue window. That part is working correctly. However, in my SaveAs window i am coding the file name to pull the Name , document type and file type. When I added a variable dtdate to the file name I get " Run Time Error "13" Type Mismatch. The variable is dtdate and I set it equal to "Now" or "Date". Please find below my code.
'==================================
'==================================
Private Sub cmdSave_Click()
pgnum = 1
namevar = frmSave.txtname.text
idvar = frmSave.txtID.text
dtdate = Now
Proceedvar = 1
Dim oldname, savFileName As Boolean
'THE NEXT BUTTON WAS JUST PRESSED
'check to see that all fields are filled in with something
'If a field is blank do not proceed
If frmSave.txtID = "" Then
MsgBox ("You need to fill in a Patient ID Number")
txtID.SetFocus
Proceedvar = 0
End If
If frmSave.txtname = "" Then
MsgBox ("You need to fill in a name")
Proceedvar = 0
txtname.SetFocus
End If
'
If Proceedvar = 1 Then ' TESTED FOR BLANK FIELDS
' Now that all fields are filled
' Move collected data to document
While pgnum < 3
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=pgnum
Selection.Find.Replacement.ClearFormatting
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
With Selection.Find
.text = "(name)"
.Replacement.text = namevar
End With
Selection.Find.Execute Replace:=wdReplaceAll
' Replace the patien's ID with data from the form
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.text = "(number)"
.Replacement.text = idvar
End With
Selection.Find.Execute Replace:=wdReplaceAll
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
pgnum = pgnum + 1
Wend
Selection.GoTo What:=wdGoToLine, Which:=wdGoToNext, Name:=1
' next we will bring up the "Save As" dialog box
cd1.Filter = "Microsoft Word Document (*.doc)"
UserName = Environ("username")
Pathvar = CurDir
Testvar = "I:\" + UserName
Mylen = Len(Testvar)
MyNew = Mid(Pathvar, 1, Mylen)
Mycheck = Pathvar Like Testvar
If MyNew = Testvar Then
cd1.InitDir = Pathvar
Else
cd1.InitDir = Testvar
End If
' if the user clicks cancel on the Save As dialog box,
' we do not want to save
With Selection.Find
.text = cd1.InitDir
.Replacement.text = thisdir
End With
cd1.FileName = namevar + dtdate + "_Format" + ".doc"
'Name savFileName As InitDir + cd1.FileName + ".doc"
cd1.ShowSave
ActiveDocument.SaveAs FileName:=cd1.FileName
frmSave.Hide
End If ' Ends the Test for blank fields
' Go to the end of the Document and prepare for note entry
End Sub
Thanks