Re: Duplicate data on First document and No data on the Second doc

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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?

--
Jeff B Paarsa


"Jean-Guy Marcil" wrote:

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
jmarcilREMOVE@xxxxxxxxxxxxxxxxxxxxxxx
Word MVP site: http://www.word.mvps.org



.



Relevant Pages