Template workflow assistance

I have a workflow process with which working with a Word Template, I need to accomplish the following:

Save the Active Document to a predefined location depended upon the users selection in the form.
Close the document.
Open a new instance based upon the template.

The users are opening the template in the standard way (click on a shortcut, recieve a Document1 [2,3,4,etc]).

Currently I have the first two requirements completed.  The Document instance of the template is saved to the location and closed.  But when it gets to the point of creating the new instance, nothing happens.  I understand why it is happening (When the ActiveDocument.Close method fires, it closes the document instance and it's attatched TemplateProject instance leaving just a Normal Template instance running).  I have tried to open a new document instance using a method placed in Normal (really not the preferred way I would like to take this), but this too has proven a little more difficult to accomplish.

Any assistance would be greatly appreciated.

-saige-
Sub NextDocument()
    Dim strTempData As String
    strPath = "P:\"
    strAppPath = TemplateProject.ThisDocument.Path & "\Surgery Form.dot"
    strDocTitle = ActiveDocument.FormFields("Text1").Result & " " _
    & Replace(ActiveDocument.FormFields("Text17").Result, "/", "-")
    Select Case ActiveDocument.FormFields("AttendDoctor").Result
        Case "Dr. Lawrence Mandel"
            strPath = strPath & "Surgery Scheduling\"
        Case "Dr. William Cruce"
            strPath = strPath & "Surgery Scheduling-Dr. Cruce\"
        Case Else
            MsgBox ("A doctor was not selected.  Please select a doctor and try again.")
            Exit Sub
    End Select
    strSaveString = strPath & strDocTitle & ".doc"
    With ActiveDocument
        .SaveAs FileName:=strSaveString
        .Close
    End With
    MsgBox ("File has been saved to: " & strSaveString & _
    "  Please continue.")
    Documents.Add Template:=strAppPath, NewTemplate:=False
End Sub

Open in new window

LVL 12
weareitAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GrahamSkanRetiredCommented:
Have you tried opening the new document before saving and closing the previous one?
0
weareitAuthor Commented:
I have not tried that, it really seems convaluted to accomplish this that way but Microsoft is not known for always taking the logical path when providing a development path.

How would you recommend accomplishing this?

By moving the Documents.Add Template:=strAppPath, NewTemplate:=False line above the With ActiveDocument line?

If so, how would I then reference the previous document to once again make it the ActiveDocument (since opening a new instance is going to automatically provide focus to the new instance)?

-saige-
0
Joanne M. OrzechManager, Document Services CenterCommented:
Just jumping here.... maybe the following code?  Although Graham is MUCH better at this stuff than I am...

Sub NextDocument()
    Dim strTempData As String
    strPath = "P:\"
    strAppPath = TemplateProject.ThisDocument.Path & "\Surgery Form.dot"
    strDocTitle = ActiveDocument.FormFields("Text1").Result & " " _
    & Replace(ActiveDocument.FormFields("Text17").Result, "/", "-")
    Select Case ActiveDocument.FormFields("AttendDoctor").Result
        Case "Dr. Lawrence Mandel"
            strPath = strPath & "Surgery Scheduling\"
        Case "Dr. William Cruce"
            strPath = strPath & "Surgery Scheduling-Dr. Cruce\"
        Case Else
            MsgBox ("A doctor was not selected.  Please select a doctor and try again.")
            Exit Sub
    End Select
    strSaveString = strPath & strDocTitle & ".doc"
    With ActiveDocument
        .SaveAs FileName:=strSaveString
        .Close
    End With
    MsgBox ("File has been saved to: " & strSaveString & _
    "  Please continue.")
     'NEW CODE HERE
    tFormLetter = ActiveDocument.Name
    Documents.Add Template:=strAppPath, NewTemplate:=False
    tNewLetter = ActiveDocument.Name
    Documents(tFormLetter).Close savechanges:=wdSaveChanges
    Documents(tNewLetter).Activate
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

weareitAuthor Commented:
Thanks I'll try that but I think I'll have to make a change or two to the code you provided:

Sub NextDocument()
    Dim strTempData As String
    strPath = "P:\"
    strAppPath = TemplateProject.ThisDocument.Path & "\Surgery Form.dot"
    strDocTitle = ActiveDocument.FormFields("Text1").Result & " " _
    & Replace(ActiveDocument.FormFields("Text17").Result, "/", "-")
    Select Case ActiveDocument.FormFields("AttendDoctor").Result
        Case "Dr. Lawrence Mandel"
            strPath = strPath & "Surgery Scheduling\"
        Case "Dr. William Cruce"
            strPath = strPath & "Surgery Scheduling-Dr. Cruce\"
        Case Else
            MsgBox ("A doctor was not selected.  Please select a doctor and try again.")
            Exit Sub
    End Select
    strSaveString = strPath & strDocTitle & ".doc"
    With ActiveDocument
        .SaveAs FileName:=strSaveString
    'These below would be removed
    '    .Close
    'The above would be removed
    End With
    MsgBox ("File has been saved to: " & strSaveString & _
    "  Please continue.")
     'NEW CODE HERE
    tFormLetter = ActiveDocument.Name
    Documents.Add Template:=strAppPath, NewTemplate:=False
    tNewLetter = ActiveDocument.Name
    Documents(tFormLetter).Close savechanges:=wdSaveChanges
    Documents(tNewLetter).Activate
End Sub

Open in new window

0
weareitAuthor Commented:
Thanks, the modifications you made worked perfectly.  I did have to make the change that I thought I would, but overall a quick and concise solution.
0
Joanne M. OrzechManager, Document Services CenterCommented:
Wonderful!  Thanks so very much...glad to have been of assistance.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.