PDA

View Full Version : Copying and pasting a Textbox's contents.



Drezz
09-25-2009, 12:17 PM
Hey all,

been trying to learn vba and ive come across an issue. :banghead: On a form im creating in Word 2003, im setting up a macro to copy and paste the contents of several textboxes to a new area of the document, however when the code is executed, it copies the textbox in its entirety and pastes a new textbox with the values in the new area, is there a way to copy just the value of whats in the textbox and paste it?

Heres the code im using

Selection.GoTo What:=wdGoToBookmark, Name:="Text"
ActiveDocument.FormFields("Text1").Copy
Selection.Paste
Selection.TypeText Text:=" "
ActiveDocument.FormFields("Text2").Copy
Selection.Paste
Selection.TypeText Text:=" "
ActiveDocument.FormFields("Text3").Copy
Selection.Paste

Id be grateful for any help on the matter. :bow:


Oh, and what would be a good code to clear a textbox without deleting it?

Tinbendr
09-25-2009, 01:18 PM
uuhhhhh, what are you trying to do? (Use the Result property with Formfields, by the way. But you can't use it with the Copy method)

If you want the same text to show up in several places in the document, then you add Ref fields that point to your Formfield. Then when you exit from the Formfield, it can call a Exit macro to update Ref Fields.

BYW, you need to quickly move from using Selection to using Range. You'll have much more control of your document.

Drezz
09-25-2009, 02:05 PM
Well on the form in one area I have a 3 Textboxes for various things like this

Other:{free text here}

the {free text here} is the text box btw, i put in a macro that copies what the user types into the text box and pastes it into another area of the document. the problem is that the code i showed up above copies the entire textbox, contents and all and pastes a new textbox in the new area so it looks like this

{free text here} {some more} {and the third}

I want it to copy so that the new area would look like

free text here some more and the third

No text boxes just the text.

Tinbendr
09-25-2009, 06:12 PM
.. {free text here} {some more} {and the third}

I want it to copy so that the new area would look like

free text here some more and the third

No text boxes just the text.Ok clear as mud...

Take a look at the attachment and tell me if this is what you are looking for.

Drezz
09-26-2009, 08:16 AM
sorry im not so good at explaining it, like i said im new to it just learning. one thing i forgot to mention which may be important is that the macro is attached to a command button. If it still seems confusing, ill attach the document. itll be monday though cause its at work. but thanks for the help so far, i appreciate it.

Drezz
09-28-2009, 07:02 AM
Here is the file, i warn u, im still just learning. anyway, the object of the form is for a person to select the checkboxes that apply to their situation, and type in any notes in the text boxes for various reasons, and click the submit button, this will prefill the area in the box at the bottom that the person will copy and use for other applications. the problem is when they hit the submit button it copies the entire textbox to the box below, i want just the text in the textbox copied below.

fumei
09-28-2009, 12:15 PM
"it copies the entire textbox to the box below, i want just the text in the textbox copied below"

Pay attention to what tinbendr already posted.

Use the Result property with Formfields, by the way
If I understanding the following correctly...
Selection.GoTo What:=wdGoToBookmark, Name:="Text"
ActiveDocument.FormFields("Text1").Copy
Selection.Paste
Selection.TypeText Text:=" "
ActiveDocument.FormFields("Text2").Copy
Selection.Paste
Selection.TypeText Text:=" "
ActiveDocument.FormFields("Text3").Copy
Selection.Paste
you are:

1. going to a bookmark - "Text"
2. inserting the contents of the formfield Text1, followed by a space ( " ")
3. followed by the contents of the formfield Text2, followed by a space ( " ")
4. followed by the contents of the formfield Text3

Yes? If so, then, this does that. Because you want to copy the contents - not the formfield itself - then use .Result...which is the contents.
Dim DocFF As FormFields
Set DocfFF = ActiveDocument.FormFields
ActiveDocument.Bookmarks("Text").Range.Text = _
DocFF("Text1").Result & " " & _
DocFF("Text2").Result & " " & _
DocFF("Text3").Result

Drezz
09-28-2009, 12:31 PM
o jeez that works beautifully, sorry i wuz a little dunce, thanks for all the help.

fumei
09-28-2009, 01:16 PM
Just to hopefully help out here. It is better to use objects.
ActiveDocument.FormFields("Check1").CheckBox.Value = False
ActiveDocument.FormFields("Check2").CheckBox.Value = False
ActiveDocument.FormFields("Check3").CheckBox.Value = False
ActiveDocument.FormFields("Check4").CheckBox.Value = False
ActiveDocument.FormFields("Check5").CheckBox.Value = False
ActiveDocument.FormFields("Check6").CheckBox.Value = False
ActiveDocument.FormFields("Check7").CheckBox.Value = False
ActiveDocument.FormFields("Check8").CheckBox.Value = False
ActiveDocument.FormFields("Check8a").CheckBox.Value = False
ActiveDocument.FormFields("Check9").CheckBox.Value = False
ActiveDocument.FormFields("Check9a").CheckBox.Value = False
ActiveDocument.FormFields("Check10").CheckBox.Value = False
ActiveDocument.FormFields("Check11").CheckBox.Value = False
ActiveDocument.FormFields("Check12").CheckBox.Value = False
ActiveDocument.FormFields("Check13").CheckBox.Value = False
ActiveDocument.FormFields("Check14").CheckBox.Value = False
ActiveDocument.FormFields("Check15").CheckBox.Value = False
ActiveDocument.FormFields("Check16").CheckBox.Value = False
ActiveDocument.FormFields("Check17").CheckBox.Value = False
ActiveDocument.FormFields("Check18").CheckBox.Value = False
ActiveDocument.FormFields("Check19").CheckBox.Value = False
ActiveDocument.FormFields("Check20").CheckBox.Value = False
ActiveDocument.FormFields("Check21").CheckBox.Value = False
ActiveDocument.FormFields("Check22").CheckBox.Value = False
ActiveDocument.FormFields("Check23").CheckBox.Value = False
ActiveDocument.FormFields("Check24").CheckBox.Value = False
ActiveDocument.FormFields("Check25").CheckBox.Value = False
ActiveDocument.FormFields("Check26").CheckBox.Value = False
ActiveDocument.FormFields("Check27").CheckBox.Value = False
ActiveDocument.FormFields("Check28").CheckBox.Value = False
ActiveDocument.FormFields("Check29").CheckBox.Value = False
ActiveDocument.FormFields("Check30").CheckBox.Value = False
ActiveDocument.FormFields("Check31").CheckBox.Value = False
ActiveDocument.FormFields("Check32").CheckBox.Value = False
ActiveDocument.FormFields("Check33").CheckBox.Value = False
ActiveDocument.FormFields("Check34").CheckBox.Value = False
ActiveDocument.FormFields("Check35").CheckBox.Value = False
ActiveDocument.FormFields("Check36").CheckBox.Value = False
ActiveDocument.FormFields("Check37").CheckBox.Value = False
ActiveDocument.FormFields("Check38").CheckBox.Value = False
ActiveDocument.FormFields("Check39").CheckBox.Value = False
ActiveDocument.FormFields("Check40").CheckBox.Value = False
ActiveDocument.FormFields("Check41").CheckBox.Value = False
If you use objects, you can shorten that tedious ActiveDocument.FormFields.
Dim DocFF As FormFields ' notice the plural!
Set DocFF = ActiveDocument.FormFields
DocFF("Check1").CheckBox.Value = False
DocFF("Check2").CheckBox.Value = False
DocFF("Check3").CheckBox.Value = False
DocFF("Check4").CheckBox.Value = False
DocFF("Check5").CheckBox.Value = False
DocFF("Check6").CheckBox.Value = False
DocFF("Check7").CheckBox.Value = False
DocFF("Check8").CheckBox.Value = False
DocFF("Check8a").CheckBox.Value = False
DocFF("Check9").CheckBox.Value = False
DocFF("Check9a").CheckBox.Value = False
DocFF("Check10").CheckBox.Value = False
DocFF("Check11").CheckBox.Value = False
DocFF("Check12").CheckBox.Value = False
DocFF("Check13").CheckBox.Value = False
DocFF("Check14").CheckBox.Value = False
DocFF("Check15").CheckBox.Value = False
DocFF("Check16").CheckBox.Value = False
DocFF("Check17").CheckBox.Value = False
DocFF("Check18").CheckBox.Value = False
DocFF("Check19").CheckBox.Value = False
DocFF("Check20").CheckBox.Value = False
DocFF("Check21").CheckBox.Value = False
DocFF("Check22").CheckBox.Value = False
DocFF("Check23").CheckBox.Value = False
DocFF("Check24").CheckBox.Value = False
DocFF("Check25").CheckBox.Value = False
DocFF("Check26").CheckBox.Value = False
DocFF("Check27").CheckBox.Value = False
DocFF("Check28").CheckBox.Value = False
DocFF("Check29").CheckBox.Value = False
DocFF("Check30").CheckBox.Value = False
DocFF("Check31").CheckBox.Value = False
DocFF("Check32").CheckBox.Value = False
DocFF("Check33").CheckBox.Value = False
DocFF("Check34").CheckBox.Value = False
DocFF("Check35").CheckBox.Value = False
DocFF("Check36").CheckBox.Value = False
DocFF("Check37").CheckBox.Value = False
DocFF("Check38").CheckBox.Value = False
DocFF("Check39").CheckBox.Value = False
DocFF("Check40").CheckBox.Value = False
DocFF("Check41").CheckBox.Value = False

Better yet, if what you are doing - i.e. your purpose - is trying to set all the checkboxes to False, then:

Sub MakeFalse()
Dim DocFF As FormFields
Dim oFF As FormField
Set DocFF = ActiveDocument.FormFields
For Each oFF In DocFF
If oFF.Type = wdFieldFormCheckBox Then
oFF.Result = False
End If
Next
End Sub

DocFF is all the formfields in the document.
oFF is any formfield.

For Each oFF In DocFF
(for any formfield in the document formfields)

If oFF.Type = wdFieldFormCheckBox Then
(if that formfield is a checkbox)

oFF.Result = False
(make it unchecked, i.e. False)

Done.

fumei
09-28-2009, 01:24 PM
"sorry i wuz a little dunce"

No apologies needed, and you were not a dunce at all. I just want to point out (again...I know I am tedious about this), that the vast majority of good VBA coding is thinking. As you can see in the MakeFalse procedure above, the coding part is quite simple really. It is grasping the concepts (thinking) that is difficult.

The concept of ONE variable containing ALL the formfields in the document;
Dim DocFF As FormFields
Set DocFF = ActiveDocument.Formfields


The concept of ONE variable to refer to any formfield (oFF As FormField);

The concept of testing ANY formfield .Type property to see if it is a checkbox;
If oFF.Type = wdFieldFormCheckBox Then


Technically speaking, you do not actually need the DocFF object variable, but it is handy thing to know about, as you can use the concept for other things.
Sub MakeFalse()
Dim oFF As FormField
For Each oFF In ActiveDocument.FormFields
If oFF.Type = wdFieldFormCheckBox Then
oFF.Result = False
End If
Next
End Sub
Does exactly the same thing.

mdmackillop
09-29-2009, 05:15 AM
You can also be selective using numbers, either in a loop or in an array. You would need to cater for 8a on its own though.

Dim DocFF As FormFields ' notice the plural!
Set DocFF = ActiveDocument.FormFields
For i = 1 to 22
DocFF("Check" & i).CheckBox.Value = False
Next
For i = 23 to 44
DocFF("Check" & i).CheckBox.Value = True
Next

fumei
09-29-2009, 11:22 AM
Although....it is a very good idea to start using intelligible names. Check23 does not indicate or mean anything. I know it is tedious to come up with meaningful names when you have so many, but in the long run, meaningful names are good things.

Drezz
09-29-2009, 01:27 PM
ok love the code examples, working great, btw, is there any way to keep the cursor from going to the beginning of the line after copying and pasteing the text? just in case i need it to paste in more stuff.

fumei
09-29-2009, 01:40 PM
Yes...do not use the cursor at all.