Duplicate data on First document and No data on the Second documen

J

Jeffery B Paarsa

Hello all,

Following is the macro code that will be executed.

Option Explicit
Private Sub CMDPrint_Click()
' some codes....
With ActiveDocument
.Unprotect
.Bookmarks("PFName").Range _
.InsertBefore PFName
.Bookmarks("PFName").Range.Font.Color = wdColorRed
.Bookmarks("PLName").Range _
.InsertBefore PLName
.Bookmarks("PLName").Range.Font.Color = wdColorRed
ActiveDocument.Fields.Update
Application.Visible = True
NPPkg.Hide
' .PrintOut
End With
Call openMedHistory(NPPkg.PFName.Text, NPPkg.PLName.Text) '<-- Problem
End Sub
Sub openMedHistory(ByRef PFName As String, ByRef PLName As String)
WordBasic.DisableAutoMacros 1
Word.Documents.Add Template:="H:\Mdoc\(ALL)MRPMedHist.dot", Visible:=False
With ActiveDocument
' .Unprotect
.Bookmarks("PFName").Range _
.InsertBefore PFName
.Bookmarks("PFName").Range.Font.Color = wdColorRed
.Bookmarks("PLName").Range _
.InsertBefore PLName
.Bookmarks("PLName").Range.Font.Color = wdColorRed
ActiveDocument.Fields.Update
.Protect wdAllowOnlyFormFields, Noreset:=True
Application.Visible = True
End With
' WordBasic.DisableAutoMacros 0
End Sub

As you may notify I have identical two Bookmarks on these two templates
called "PFName" & "PLName". Everything is working perfect in "Sub
CMDPrint_Click()" until:
Call openMedHistory(NPPkg.PFName.Text, NPPkg.PLName.Text) '<-- Problem

As you may notice on this routine "openMedHistory" I am trying to open and
add a new document by suppressing it's Macro execution on this Template and
just populate the same fields of PFName & PLName that I have collected thru
the UserForm of NPPkg and put it on the same Bookmark fields of "PFName &
PLName" that exist on the second template/document of (ALL)MRPMedHist.doc.
Here is where the problem starts. When my both Documents are displayed if I
have entered for example "F" for PFName and "L" for PLName, data is being
duplicated on my first document like "FF" in PFName and "LL" in PLName and
the PFName & PLName on the second document "(ALL)MRPMedHist.doc" is empty. I
know I have refrencing problem on "openMedHistory" but I don't know how I
can correct that. I have used "ByVal" with no difference in the result. Any
idea?
 
J

Jean-Guy Marcil

Jeffery B Paarsa was telling us:
Jeffery B Paarsa nous racontait que :
Hello all,

Following is the macro code that will be executed.

Option Explicit
Private Sub CMDPrint_Click()
' some codes....
With ActiveDocument
.Unprotect
.Bookmarks("PFName").Range _
.InsertBefore PFName
.Bookmarks("PFName").Range.Font.Color = wdColorRed
.Bookmarks("PLName").Range _
.InsertBefore PLName
.Bookmarks("PLName").Range.Font.Color = wdColorRed
ActiveDocument.Fields.Update
Application.Visible = True
NPPkg.Hide
' .PrintOut
End With
Call openMedHistory(NPPkg.PFName.Text, NPPkg.PLName.Text) '<--
Problem
End Sub
Sub openMedHistory(ByRef PFName As String, ByRef PLName As String)
WordBasic.DisableAutoMacros 1
Word.Documents.Add Template:="H:\Mdoc\(ALL)MRPMedHist.dot",
Visible:=False With ActiveDocument
' .Unprotect
.Bookmarks("PFName").Range _
.InsertBefore PFName
.Bookmarks("PFName").Range.Font.Color = wdColorRed
.Bookmarks("PLName").Range _
.InsertBefore PLName
.Bookmarks("PLName").Range.Font.Color = wdColorRed
ActiveDocument.Fields.Update
.Protect wdAllowOnlyFormFields, Noreset:=True
Application.Visible = True
End With
' WordBasic.DisableAutoMacros 0
End Sub

As you may notify I have identical two Bookmarks on these two
templates called "PFName" & "PLName". Everything is working perfect
in "Sub CMDPrint_Click()" until:
Call openMedHistory(NPPkg.PFName.Text, NPPkg.PLName.Text) '<--
Problem

As you may notice on this routine "openMedHistory" I am trying to
open and add a new document by suppressing it's Macro execution on
this Template and just populate the same fields of PFName & PLName
that I have collected thru the UserForm of NPPkg and put it on the
same Bookmark fields of "PFName & PLName" that exist on the second
template/document of (ALL)MRPMedHist.doc. Here is where the problem
starts. When my both Documents are displayed if I have entered for
example "F" for PFName and "L" for PLName, data is being duplicated
on my first document like "FF" in PFName and "LL" in PLName and the
PFName & PLName on the second document "(ALL)MRPMedHist.doc" is
empty. I know I have refrencing problem on "openMedHistory" but I
don't know how I can correct that. I have used "ByVal" with no
difference in the result. Any idea?

Use Object variables to refer to your documents. Do not use ActiveDocument,
if you do, only do so when you are certain that you have only one document
opened.

So, in your code, in each sub, declare a variable like this:

Sub Number1

Dim docStartingDoc As Document

Set docStartingDoc = ActiveDocument
'or
'Set docStartingDoc = Documents("Name of current document.doc")

With docStartingDoc
.Unprotect
.Etc...
End With

End Sub


Sub Number2()

Dim docSecondOne As Document

Set docSecondOne =
Word.Documents.Add(Template:="H:\Mdoc\(ALL)MRPMedHist.dot", _
Visible:=False)
With docSecondOne
.Unprotect
.Etc...
End With

End Sub

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jeffery B Paarsa

Jean,

Thank you thank you thank you.... It worked great but somehow after all 3
docs are printed I see that WinWord is still active when I open the Task
manager... I don't see the WinWord under the Applications tab but I surely
see it running under the Processes tab. How can I end the task after I am
done? It is strange after I am done I opened the first Template and after I
went to Macro/Visual Basic Editor I noticed that all my 3 templates along
with 3 identical documents opened from those templates can be seen on Project
Explorer or the visual basic editor! Why don't they go away?
 
J

Jean-Guy Marcil

Jeffery B Paarsa was telling us:
Jeffery B Paarsa nous racontait que :
Jean,

Thank you thank you thank you.... It worked great but somehow after
all 3 docs are printed I see that WinWord is still active when I open
the Task manager... I don't see the WinWord under the Applications
tab but I surely see it running under the Processes tab. How can I
end the task after I am done? It is strange after I am done I opened
the first Template and after I went to Macro/Visual Basic Editor I
noticed that all my 3 templates along with 3 identical documents
opened from those templates can be seen on Project Explorer or the
visual basic editor! Why don't they go away?

How do you instantiate the Word Application?
How do you close your three documents?

Show us the relevant code.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jeffery B Paarsa

Hello,

I instanciate the word by double clicking on the template .dot and here is
the code that I execute.

' some code.....
NPPkg.Hide
Call openHippa(NPPkg.PFName.Text, NPPkg.PLName.Text)
Call openMedHistory(NPPkg.PFName.Text, NPPkg.PLName.Text)
Call openRegInfo(NPPkg.PFName.Text, NPPkg.PLName.Text)
Call openProgNote(NPPkg.PFName.Text, NPPkg.PLName.Text)
.Protect wdAllowOnlyFormFields, Noreset:=True
.Close
End With
End Sub
Sub openHippa(ByRef PFName As String, ByRef PLName As String)
Dim HippaForm As Document
WordBasic.DisableAutoMacros 1
Set HippaForm = Word.Documents.Add(Template:="D:\Maryam doc\(ALL)MR Hippa
Form.dot", Visible:=False)
With HippaForm
.Unprotect
.Bookmarks("PFName").Range _
.InsertBefore PFName
.Bookmarks("PFName").Range.Font.Color = wdColorRed
.Bookmarks("PLName").Range _
.InsertBefore PLName
.Bookmarks("PLName").Range.Font.Color = wdColorRed
ActiveDocument.Fields.Update
.Protect wdAllowOnlyFormFields, Noreset:=True
.PrintOut
.Close '<= I added this now document get close with a prompt dialog
box that
' I don't want... How can I send a No response?
End With
End Sub

I did not have the .Close before. Now that I put the .Close it prompts for
Save Yes, No, Cancel. I don't want to save... How can I send the No response
without having the operator to click on No button?
 
J

Jean-Guy Marcil

Jeffery B Paarsa was telling us:
Jeffery B Paarsa nous racontait que :
Hello,

I instanciate the word by double clicking on the template .dot and
here is the code that I execute.

' some code.....
NPPkg.Hide
Call openHippa(NPPkg.PFName.Text, NPPkg.PLName.Text)
Call openMedHistory(NPPkg.PFName.Text, NPPkg.PLName.Text)
Call openRegInfo(NPPkg.PFName.Text, NPPkg.PLName.Text)
Call openProgNote(NPPkg.PFName.Text, NPPkg.PLName.Text)
.Protect wdAllowOnlyFormFields, Noreset:=True
.Close
End With
End Sub
Sub openHippa(ByRef PFName As String, ByRef PLName As String)
Dim HippaForm As Document
WordBasic.DisableAutoMacros 1
Set HippaForm = Word.Documents.Add(Template:="D:\Maryam doc\(ALL)MR
Hippa Form.dot", Visible:=False)
With HippaForm
.Unprotect
.Bookmarks("PFName").Range _
.InsertBefore PFName
.Bookmarks("PFName").Range.Font.Color = wdColorRed
.Bookmarks("PLName").Range _
.InsertBefore PLName
.Bookmarks("PLName").Range.Font.Color = wdColorRed
ActiveDocument.Fields.Update
.Protect wdAllowOnlyFormFields, Noreset:=True
.PrintOut
.Close '<= I added this now document get close with a prompt
dialog
box that
' I don't want... How can I send a No response?
End With
End Sub

I did not have the .Close before. Now that I put the .Close it
prompts for Save Yes, No, Cancel. I don't want to save... How can I
send the No response without having the operator to click on No
button?

That is simple enough...:

.Close wdDoNotSaveChanges

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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