VBA macro stops executing on Word 2003 but works on Word XP

M

MDIH EMR

I have a macro that extracts information from a protected form, assembles a
file name and path, saves the file, prints the file and then opens a new
document using the template file. On Word XP the macro executes completely.
On Word 2003 the macro goes away after the file is saved. I can see the
difference in that after the file is saved the macro code is no longer
attached. Any suggestions?
 
D

Doug Robbins - Word MVP

Not without seeing the code.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
M

MDIH EMR

Here is the code. It stops executing immedately after the save file.
Thanks

' Unprotect form
ActiveDocument.Sections(1).ProtectedForForms = False

' Load name from form into work area
PatFullName = ActiveDocument.FormFields("PatName").Range.Text

' Load Provider Name into ProvPath var
ProvPath = ActiveDocument.FormFields("ProvName").Range.Text

' Load Date of Service from form into DateofSvc var
DateofSvc = ActiveDocument.FormFields("DoS").Range.Text

' Protect form
ActiveDocument.Sections(1).ProtectedForForms = True

' Find end of first name string
Firstspace = InStr(PatFullName, " ")

' Load first name into new field
PatFirstName = Left(PatFullName, Firstspace - 1)

' Delete first name from full name string
PatFullName = Mid(PatFullName, Firstspace + 1)

' Load patient last name into new field
PatLastName = PatFullName

' Build file save as path
ProgNotePathandFileName = "\\mdihcpsi\users\Transcription\CFP Mona
York\Local " & ProvPath & "\To Be Printed " & ProvPath & "\" & DateofSvc & "
" & PatLastName & " " & PatFirstName & ".doc"

' Save file
ActiveDocument.SaveAs ProgNotePathandFileName

' Build message confirming file save
FileSavedAs = "Progress Note saved as " & ProgNotePathandFileName

' Display confirmation
MsgBox FileSavedAs

' Build path and file name for new document template
TemplatePath = "C:\Progress Note Templates\Prog Note " & ProvPath & ".dot"

' Open new document
Documents.Add Template:=TemplatePath
 
G

Graham Mayor

It is not necessary to unprotect the form, which is as well because your
code does not unprotect the document (but it does appear to make the form
unstable). That aside, assuming the paths for the document and the template
for the new document exist, the code does indeed work in Word 2003. However,
I think you may do better with:

Dim PatFullName() As String
Dim ProvPath As String
Dim DateofSVc As String
Dim PatFirstName As String
Dim PatLastName As String
Dim ProgNotePathAndFileName As String
Dim FileSavedAs As String

' Load name from form into work area
PatFullName = Split(ActiveDocument.FormFields("PatName").Result,
Chr(32))
' Load Provider Name into ProvPath var
ProvPath = ActiveDocument.FormFields("ProvName").Result
' Load Date of Service from form into DateofSvc var
DateofSVc = ActiveDocument.FormFields("DoS").Result
' Load first name into new variable
PatFirstName = PatFullName(0)
' Load patient last name into new variable
PatLastName = PatFullName(1)
' Build file save as path
ProgNotePathAndFileName = "\\mdihcpsi\users\Transcription\CFP Mona
York\Local " _
& ProvPath & "\To Be Printed " & ProvPath & "\" & DateofSVc & " " _
& PatLastName & " " & PatFirstName & ".doc"
' Save file
ActiveDocument.SaveAs ProgNotePathAndFileName
' Build message confirming file save
FileSavedAs = "Progress Note saved as " & ProgNotePathAndFileName
' Display confirmation
MsgBox FileSavedAs
' Build path and file name for new document template
TemplatePath = "C:\Progress Note Templates\Prog Note " _
& ProvPath & ".dot"
' Open new document
Documents.Add Template:=TemplatePath


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

MDIH EMR

Thank you Graham,
I will be testing later today

Graham Mayor said:
It is not necessary to unprotect the form, which is as well because your
code does not unprotect the document (but it does appear to make the form
unstable). That aside, assuming the paths for the document and the template
for the new document exist, the code does indeed work in Word 2003. However,
I think you may do better with:

Dim PatFullName() As String
Dim ProvPath As String
Dim DateofSVc As String
Dim PatFirstName As String
Dim PatLastName As String
Dim ProgNotePathAndFileName As String
Dim FileSavedAs As String

' Load name from form into work area
PatFullName = Split(ActiveDocument.FormFields("PatName").Result,
Chr(32))
' Load Provider Name into ProvPath var
ProvPath = ActiveDocument.FormFields("ProvName").Result
' Load Date of Service from form into DateofSvc var
DateofSVc = ActiveDocument.FormFields("DoS").Result
' Load first name into new variable
PatFirstName = PatFullName(0)
' Load patient last name into new variable
PatLastName = PatFullName(1)
' Build file save as path
ProgNotePathAndFileName = "\\mdihcpsi\users\Transcription\CFP Mona
York\Local " _
& ProvPath & "\To Be Printed " & ProvPath & "\" & DateofSVc & " " _
& PatLastName & " " & PatFirstName & ".doc"
' Save file
ActiveDocument.SaveAs ProgNotePathAndFileName
' Build message confirming file save
FileSavedAs = "Progress Note saved as " & ProgNotePathAndFileName
' Display confirmation
MsgBox FileSavedAs
' Build path and file name for new document template
TemplatePath = "C:\Progress Note Templates\Prog Note " _
& ProvPath & ".dot"
' Open new document
Documents.Add Template:=TemplatePath


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

MDIH EMR

I removed the document protection sections for extracting field information.
But here is what happens now. I have given up on the Word 2002 system and
have two systems each with Word 2003. I have checked the patch levels and
verified that they both appear to be "identical". I develop the template on
system "A" and test the VBA macro. The project window in the editor shows 3
entries:

Normal
- Microsoft Word Objects
- ThisDocument

Project(Document1)
- Microsoft Word Objects
- ThisDocument

Project(PatientLetter)
- Microsoft Word Objects
- ThisDocument

The macro executes perfectly. The file is saved with the correct name and
then printed to the correct printer.

I copy the document template to system "B". Apply the digital signature,
save the template and open the document. I then test again. First the
document opens as: Document 3 (on system "A" it was document 1). Second the
Project window now shows:

Normal (with two sub entries at same level)
- New macros
- ThisDocument

Project (Document3)
- Reference to PatientLetter
- ThisDocument

Project (PatientLetter)
- ThisDocument

As I step through the macro, when the document is saved, the macro code goes
away (and thus ceases execution) and the project window now shows:

Normal (with two sub entries at same level)
- New macros
- ThisDocument

Project (New Named PatientLetter)
- ThisDocument

I am probably missing something simple. Any suggestions would be greatly
appreciated.

Thanks,
Bryan
 
M

MDIH EMR

I removed the document protection sections for extracting field
information.
But here is what happens now. I have given up on the Word 2002 system and
have two systems each with Word 2003. I have checked the patch levels and
verified that they both appear to be "identical". I develop the template on
system "A" and test the VBA macro. The project window in the editor shows 3
entries:

Normal
- Microsoft Word Objects
- ThisDocument

Project(Document1)
- Microsoft Word Objects
- ThisDocument

Project(PatientLetter)
- Microsoft Word Objects
- ThisDocument

The macro executes perfectly. The file is saved with the correct name and
then printed to the correct printer.

I copy the document template to system "B". Apply the digital signature,
save the template and open the document. I then test again. First the
document opens as: Document 3 (on system "A" it was document 1). Second the
Project window now shows:

Normal (with two sub entries at same level)
- New macros
- ThisDocument

Project (Document3)
- Reference to PatientLetter
- ThisDocument

Project (PatientLetter)
- ThisDocument

As I step through the macro, when the document is saved, the macro code goes
away (and thus ceases execution) and the project window now shows:

Normal (with two sub entries at same level)
- New macros
- ThisDocument

Project (New Named PatientLetter)
- ThisDocument

I am probably missing something simple. Any suggestions would be greatly
appreciated.

Thanks,
Bryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top