Fuzzhead was telling us:
Fuzzhead nous racontait que :
Document A and B are created from the same template. The template
holds the codes. I decide when its applicable. There are 4 userforms,
1 for each section. If I open the first userform in document A and
the first userform in Document B, I want to copy all the information
in document A's userform over to Document B's userform.
So, we take for granted that Document A and B are open active at the same
time.
In other words, Document B is created while document A is already open.
Right?
You wrote that you decide when you need to transfer the information from A
to B. This means that it cannot be automatic.
Since you need Document A opened to be able to transfer the information from
A to B, I would suggest that you use a button on a toolbar that will create
document B by using the information in the currently active document
(Document A).
Is the information that was first entered in Document A in formfields in the
protected section? If not, you will need bookmarks (and additional code to
unprotect/re-protect Document A and B) to get to the information.
You could use code like the following when creating document B:
'_______________________________________
Sub Create_DocB()
Dim frmTransfer As UserForm1
Dim docA As Document
Dim docB As Document
Set docA = ActiveDocument
Set frmTransfer = New UserForm1
With frmTransfer
.TextBox1.Text = docA.Bookmarks("Bookmark1").Range.Text
.TextBox2.Text = docA.Bookmarks("Bookmark2").Range.Text
.TextBox3.Text = docA.Bookmarks("Bookmark3").Range.Text
'etc
'or, if using formfields
' .TextBox1.Text = docA.FormFields("Text1").Result
' .TextBox2.Text = docA.FormFields("Text2").Result
' .TextBox3.Text = docA.FormFields("Text3").Result
'etc
.Show
'Here, user can modify some or all the info in the userfrom,
'but info from doc A is already "loaded"
'The userfrom is hidden by the click event on one of its button
Set docB = Documents.Add("C:\myPath\myTemplate.dot")
With docB
.Bookmarks("Bookmark1").Range.Text = .TextBox1.Text
.Bookmarks("Bookmark2").Range.Text = .TextBox2.Text
.Bookmarks("Bookmark3").Range.Text = .TextBox3.Text
'etc
'or, if using formfields
' .FormFields("Text1").Result = .TextBox1.Text
' .FormFields("Text2").Result = .TextBox2.Text
' .FormFields("Text3").Result = .TextBox3.Text
'etc
End With
End With
Unload frmTransfer
Set frmTransfer = Nothing
End Sub
'_______________________________________
Of course, if you use bookmarks instead of formfields, you will need to
recreate the bookmarks. For this I would suggest using a Private Sub:
'_______________________________________
Sub Create_DocB()
Dim frmTransfer As UserForm1
Dim docA As Document
Dim docB As Document
Set docA = ActiveDocument
Set frmTransfer = New UserForm1
With frmTransfer
.TextBox1.Text = docA.Bookmarks("Bookmark1").Range.Text
.TextBox2.Text = docA.Bookmarks("Bookmark2").Range.Text
.TextBox3.Text = docA.Bookmarks("Bookmark3").Range.Text
'etc
.Show
'Here, user can modify some or all the info in the userfrom,
'but info from doc A is already "loaded"
'The userfrom is hidden by the click event on one of its button
Set docB = Documents.Add("C:\myPath\myTemplate.dot")
FillBookmarks "Bookamrk1", docB, .TextBox1.Text
FillBookmarks "Bookamrk2", docB, .TextBox2.Text
FillBookmarks "Bookamrk3", docB, .TextBox3.Text
End With
Unload frmTransfer
Set frmTransfer = Nothing
End Sub
'_______________________________________
'_______________________________________
Private Sub FillBookmarks(strBookmarkName As String, _
docTarget As Document, strText As String)
Dim rgeText As Range
With docTarget
Set rgeText = .Bookmarks(strBookmarkName).Range
.Bookmarks(strBookmarkName).Range.Text = strText
.Bookmarks.Add strBookmarkName, rgeText
End With
End Sub
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org