Coping info from form to form

F

Fuzzhead

I have a form that has text, check and combo boxes in it. Is there a way to
copy the information from a existing form thats filled out to a new form?
 
J

Jean-Guy Marcil

Fuzzhead was telling us:
Fuzzhead nous racontait que :
I have a form that has text, check and combo boxes in it. Is there a
way to copy the information from a existing form thats filled out to
a new form?

I think that more details would be useful if you want a meaningful reply.

Are you writing about userforms or a document protected for forms?
What do you mean by "New form"?
What is your overall purpose?
What have you tried so far?

--

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

Fuzzhead

Sorry about not enough information.

I have a protected document with 4 sections that are populated by their own
user form. When a new document is created I want to be able to get
information from one of the sections of the existing document, and put it
into the new document when it’s applicable, without unprotecting the existing
document. In the existing document, if I open section 1 using its user form,
how can I copy that information from that form and then open section 1's user
form in the new document and paste the information into it?
 
J

Jean-Guy Marcil

Fuzzhead was telling us:
Fuzzhead nous racontait que :
Sorry about not enough information.

I have a protected document with 4 sections that are populated by
their own user form. When a new document is created I want to be able
to get information from one of the sections of the existing document,
and put it into the new document when it's applicable, without
unprotecting the existing document. In the existing document, if I
open section 1 using its user form, how can I copy that information
from that form and then open section 1's user form in the new
document and paste the information into it?

OK, we are starting to go somewhere here... But I still do not understand
everything.

Let's see if I get this right...
You have "Document A" which is already completed and consists of 4 protected
sections.
Now you create "Document B" (How? File > New? or "Document A" Save As? or
From some other Template?)
You need to transfer some information from section 1 in "Document A" to
sewction 1 in "Document B" "when it's applicable", how would the code know
"when it is applicable"?
You wrote "if I open section 1 using its user form". Are there 4 userforms
attached to "Document A"? Is "Document A" a document created from a template
that holds the code, or is this code part of "Document A"? What do you mean
by "open section 1"?

--

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

Fuzzhead

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.
 
J

Jean-Guy Marcil

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
 
F

Fuzzhead

I tried to use what you said but it does not work. The problem is Doc A is
opened in one sesion of Word and Doc B is open in another session. I can not
create Doc B from Doc A. Is there a way if I had a copy and paste button in
my form1 to go in Doc A form1 and copy the info to a scratchpad and then go
over into Doc B form1 and paste the info from the scratchpad?
 
D

Doug Robbins - Word MVP

Use DOCVARIABLE fields to display the information in the document and have
your userform write the values from the form to document variables in
Document A. In the initialize event from Document B, have code that grabs
the values of the variables from Document A and display it in the controls
of the Document B userform.

--
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
 
F

Fuzzhead

I used bookmarks in my document, so I'm not sure what you are talking about
with using DOCVARIABLE. Here are my macros for one of my forms I use to
update the document. I am still lost on how to copy the info from Doc A form1
to Doc B form1.

Option Explicit
Private oBMs As Bookmarks
Private oRng As Word.Range
Dim oFF As FormFields

Private Sub CmdOK_Click()
Dim MyDate
Set oFF = ActiveDocument.FormFields
Set oRng = oBMs("RName").Range
oRng.Text = Text1.Text
oBMs.Add "RName", oRng
MyDate = Date
Set oRng = oBMs("RDate").Range
oRng.Text = MyDate
oBMs.Add "RDate", oRng
Set oRng = oBMs("RRisk").Range
oRng.Text = Cmb1.Value
oBMs.Add "RRisk", oRng
Set oRng = oBMs("RBases").Range
oRng.Text = Text3.Text
oBMs.Add "RBases", oRng
ActiveDocument.Saved = True
Me.Hide
End Sub

Private Sub UserForm_Initialize()
Dim MyDate
Dim myArray
Dim oRoot
Dim sDomain
Dim oSysInfo
Dim sDistinguishedName
Dim oUser
Dim oFirst
Dim oInt
Dim oLast
Dim oName
Set oFF = ActiveDocument.FormFields
myArray = Split("LOW|MEDIUM|HIGH", "|")
With Cmb1
.List = myArray
.ListIndex = 0
End With
MyDate = Date
On Error Resume Next
Set oFF = ActiveDocument.FormFields
Set oBMs = ActiveDocument.Bookmarks
Set oRoot = GetObject("LDAP://RootDSE")
sDomain = oRoot.Get("DefaultNamingContext")
Set oSysInfo = CreateObject("ADSystemInfo")
sDistinguishedName = oSysInfo.UserName
Set oUser = GetObject("LDAP://" & sDistinguishedName)
oFirst = oUser.givenname
oInt = oUser.initials
oLast = oUser.sn
If oInt = "" Then
oName = (oFirst & " " & oLast)
Else
oName = (oFirst & " " & oInt & " " & oLast)
End If
Text1.Text = oName
Text1.Enabled = False
Text2.Value = MyDate
Text2.Enabled = False
Text3.Value = oBMs("RBases").Range.Text
Cmb1.Value = oBMs("RRisk").Range.Text
End Sub
 
D

Doug Robbins - Word MVP

There are all sorts of problems with your code, not the least of which is
probably that

ActiveDocument.Saved = True

does not save the document. Rather, it sets a flag to tell Word that it has
been saved. As a result, the information that you are writing to the
bookmarks is not being saved.

To make use of document variables in the Private Sub CmdOK_Click() of the
Doc A, you would use

With ActiveDocument
.Variables("varRRisk").Value = Cmb1.Value
.Variables("varRBases").Value = Text3.Text
etc
.SaveAs "Drive:\Path\FileName"
End With

then in the Initialize event of the DocB form, you would use

Dim DocA as Document
Set DocA = Documents.Open("Drive:\Path\FileName")
With DocA
Text3.Text = .Variables("varRBases").Value
Cmb1.Text = .Variables("varRRisk).Value
.Close wdDoNotSaveChanges
End With

--
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
 
F

Fuzzhead

What I have is a main program that creates a record. When I need to attach a
document to it, I click a button to open Word and then open my template. When
I am done filling out my document the main program will ask me if I want to
save my word document to the main program. If I say yes it saves it and
closes Word. If I say no it deletes my word document and closes Word. The way
my macros are written now work fine. The question that was ask of me was that
when the main program creates a new record and we create a new Word document
was there a way to copy part of a existing document out of an existing main
program record. So my question is if I open an existing record document,
click on my command button that opens the form used to fill out the section I
want to copy. Then in the new record that main program has created, I click
to start Word and create a new Word document. I now have to Word sessions
running at the same time. Next I open that same section in the new document
using the same form that I did in the existing document. So I now have the
existing document form open and the same form open in the new document. How
do I copy everything from the existing open form over into the open new form?
 
D

Doug Robbins - Word MVP

If you do what I suggested with document variables, then in the Initialize
event of the DocB form, you would use

Dim DocA as Document
Set DocA = Documents.Open("Drive:\Path\FileName")
With DocA
Text3.Text = .Variables("varRBases").Value
Cmb1.Text = .Variables("varRRisk).Value
.Close wdDoNotSaveChanges
End With

That may not however be the best way. It may be better to do it from your
"main program" whatever that is, and which is something that you have only
now mentioned - six posts into the thread.
--
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
 
F

Fuzzhead

So if I understand you correctly you can not just open form1 in existing Doc
A and copy the info in form1 and then open the new Doc B form1 and paste the
info into it. This would be the only way that I can do it. The main program
encrypts the record and I do not have access to it.
 
J

Jean-Guy Marcil

Fuzzhead was telling us:
Fuzzhead nous racontait que :
So if I understand you correctly you can not just open form1 in
existing Doc A and copy the info in form1 and then open the new Doc B
form1 and paste the info into it. This would be the only way that I
can do it. The main program encrypts the record and I do not have
access to it.

I think you need to let go of the idea of Form1 for now.

Doc A is opened.
The information needed for B is in the A document.
It can be retrieved by any number of ways, two of which were suggested in
this thread (Bookmarks or Document Properties).
Once it is retrieved, it can be easily passed to a userform in order to
populate it.

In my opinion, the reason you cannot get adequate assistance in this thread
is due to the fact the we do not understand the relationship between DocA
and DocB as you have never clearly described how DocB is created in relation
to DocA.

Are they created simultaneously? Is B created at the user request once A has
been created and saved? Is B created from this mysterious main program long
after A has been created, saved and stored? Etc.

--

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

Fuzzhead

B is created from the main program (Passport) long after A has been created,
saved and stored. Doc A is an historical record containing applicable
information I would like to copy and paste into Doc B, thus saving time not
having to retype the same information over again.
 
J

Jean-Guy Marcil

Fuzzhead was telling us:
Fuzzhead nous racontait que :
B is created from the main program (Passport) long after A has been
created, saved and stored. Doc A is an historical record containing
applicable information I would like to copy and paste into Doc B,
thus saving time not having to retype the same information over again.

When documents are created from "Passport", can the user indicate if the
document to be created is totally new (Like an A document) or that it is a
document related to another one? How does the use tell the program that the
document to be created is connected in some way to another one that has
already been created and stored?

--

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

Fuzzhead

The Word documents are tied to each Passport record. I have to create new
Word documents every time a new Passport record is created. They are not
connected to each other at all other than they use the same template when
created.
 
J

Jean-Guy Marcil

Fuzzhead was telling us:
Fuzzhead nous racontait que :
The Word documents are tied to each Passport record. I have to create
new Word documents every time a new Passport record is created. They
are not connected to each other at all other than they use the same
template when created.

I am sorry, but I have really tried to understand and help, but I still
cannot understand the procedure you are using.
Earlier in the thread you wrote:

"The question that was ask of me was that
when the main program creates a new record and we create a new Word document
was there a way to copy part of a existing document out of an existing main
program record. So my question is if I open an existing record document,
(...)"

So, it seems that when you create a new record with its accompanying new
Word document, there can be some connection to an existing document. But
above you write that documents are not connected...

How is the program supposed to know which existing Word document it is
supposed to lift the information from if none of the records/documents are
connected in some way?

--

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

Fuzzhead

I apologies for all of the confusion. I thought this would be an easy
question because in my mined I know exactly what I am looking for. But I
found out that it’s a lot harder to explain it in words so you know what I’m
looking for.

I have to pick which document templates to use and attach them to the main
record. Then enter the information manually. I can go into history and find
similar records and open the attached Word documents to see if the
information in then are applicable. If it is, right now I would have to
retype that information manually into the new document. This gets very time
consuming. So I was thinking if both documents are created from the same
template and in the template each section has its own form used to fill in
the information if it’s new or edit existing information and I can have both
records open at the same time, I should be able to copy that sections
information for one document to the other using that sections form. I thought
this would be a easy question because in my mined I know exalty what I amy
looking for. But I found out that its a lot harder to explain it in words.
 

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