How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask GREGMAXEY Your Own Question
GREGMAXEY
GREGMAXEY, Microsoft Word VBA Developer
Category: Microsoft Office
Satisfied Customers: 62
Experience:  I specialize is custom VBA solutions for Microsoft Office Word.
16468162
Type Your Microsoft Office Question Here...
GREGMAXEY is online now
A new question is answered every 9 seconds

Ask a Microsoft Word Question I would like to add text to

Customer Question

Ask a Microsoft Word Question
>I would like to add text to a document dependent on a choice i.e, tick box yes or no?

Any idea how this can be done easily
Submitted: 5 years ago.
Category: Microsoft Office
Expert:  GREGMAXEY replied 5 years ago.

There are lots of ways to do this depending on the version of Word and the level of complexity. How easy it is is really up to you. The simpliest is probably a protected form field checkbox with a macro assigned to run on exit.

Sub CBOnExit()
If ActiveDocument.FormFields("Check1").CheckBox.Value = True Then
ActiveDocument.Unprotect
ActiveDocument.Range.InsertAfter "Add this text."
ActiveDocument.Protect wdAllowOnlyFormFields, True
End If
End Sub

A more comprehensive method would involve adding the text to a specific targeted range perhaps using a userform.


See: http://gregmaxey.mvps.org/Create_and_employ_a_UserForm.htm

If you are not familiar with VBA then there probably isn't an "easy" way and you may have to hire someone to design your form for you.

Customer: replied 5 years ago.

I have office 2007 and 2010, so if I add a check box and drop the code in with my proposed text in this section:

 

"Add this text."

 

This will do it when ticked

 

How can I determine where in the document it will go, as in other sections I would like to alternate the text dependent on box ticked (e.g is exclusive or non exclusive) box for exclusive and a box for non exclusive, the text added to the document will depend on box ticked.

 

Thanks

Expert:  GREGMAXEY replied 5 years ago.
If you use Office 2010 then you could use a ContentControl checkbox and the ContentControl OnExit event. This way your form does not have to be protected. Since Word 2007 doesn't have a checkbox content control you could use either a content control dropdown (Yes/No) or you would have to use a protected form and a formfield checkbox.

Yes. If you use a formfield checkbox then replacing "Add this text." with your text would do it.

To fix where the text should go you will have to create a range and write the text to the range or define the text as a document variable and put the variable at the correct place. Information on how to do both is contained in the link I gave you. Writing and testing the actual code would take a fair bit of time.
GREGMAXEY and 2 other Microsoft Office Specialists are ready to help you
Customer: replied 5 years ago.

Hello again, you very kindly helped me yesterday.

I have almost cracked it , I am however having a problem with adding the following text via the macro compile error works fine with a single line but not as a paragraph, any idea?

Sub OnExitCB1B()
Dim oFld As FormFields
Dim oVars As Variables
Dim i As Long
Set oFld = ActiveDocument.FormFields
Set oVars = ActiveDocument.Variables
If oFld("Check9").CheckBox.Value = True Then
oVars("varCheck9Result").Value = _
"Exclusive relationship. Subject to clauses 3.3 and 3.4, the relationship between the parties as contemplated by this Agreement shall be exclusive"
a ISO shall refer all Prospects located in the Territory exclusively to FDGL and procure that FDGL is, in the Territory, the sole and exclusive receipt of all Prospects' referrals by ISO for the Equipment Services and/or services similar to the Equipment Services
b ISO not perform or provide, and shall procure that none of its Affiliates, nor any Entity other than FDGL performs or provides) in the Territory any Equipment Services (and/or all services of the same or similar description as the Equipment Services) for itself or for any of its Affiliates; and
c not promote or endorse the promotion to Prospects of services from a third party which are the same as, similar to or competitive with the Equipment Services (and/or all services of the same or similar description as the Equipment Services) and shall not engage, retain or assist any third party in any such promotion."

Else
oVars("varCheck9Result").Value = _
"Non-exclusive relationship. Subject to clause 3.4, the relationship between the parties as contemplated by this Agreement shall be non-exclusive"
a ISO may promote in or outside the Territory similar services from a competitor of FDGL; and
b nothing in this Agreement shall restrict the ability of either party to act as an agent, partner or joint venturer for or with any other Entity or to act on its own behalf in connection with the provision of any services.

End If
For i = ActiveDocument.Fields.Count To 1 Step -1
With ActiveDocument.Fields(i)
If .Type = wdFieldDocVariable Then .Update
End With
Next i
End Sub

Expert:  GREGMAXEY replied 5 years ago.
Sub ScratchMacro()
'A quick macro scratch pad created by Greg Maxey
MsgBox "Paragraphs are created using ""vbcr.""" & vbCr & "This is a new paragraph"
End Sub
Customer: replied 5 years ago.

Don't supose you could add to the macro and highight red, so that I can see how it fits in?

Much appreciated

 

Stephen

Expert:  GREGMAXEY replied 5 years ago.

Here is how one might look:

Sub Test()
MsgBox "Exclusive relationship. Subject to clauses 3.3 and 3.4, " _
& "the relationship between the parties as contemplated by this Agreement shall be exclusive." & vbCr + vbCr _
& "a. ISO shall refer all Prospects located in the Territory exclusively to" _
& " FDGL and procure that FDGL is, in the Territory, the sole and exclusive receipt of all Prospects' referrals" _
& " by ISO for the Equipment Services and/or services similar to the Equipment Services" & vbCr + vbCr _
& "b. ISO not perform or provide, and shall procure that none of its Affiliates, nor any Entity other" _
& " than FDGL performs or provides) in the Territory any Equipment Services (and/or all services of" _
& "the same or similar description as the Equipment Services) for itself or for any of its Affiliates; and" & vbCr + vbCr _
& "c. Not promote or endorse the promotion to Prospects of services from a third party which are the same as," _
& " similar to or competitive with the Equipment Services (and/or all services of the same or similar description as" _
& " the Equipment Services) and shall not engage, retain or assist any third party in any such promotion."
End Sub

Use "_" to break VBA code lines to make them easier to read. User vbCr to create one or more paragraph breaks in the resulting string text.

Customer: replied 5 years ago.
So do you use "_" to break the code up, what do you use to start a new line but not a new paragraph, this will stop the additional numbering in the docment and just put it on another line?
Expert:  GREGMAXEY replied 5 years ago.

Not sure I follow. Try:

Sub ScratchMacro()
ActiveDocument.Range.InsertAfter "Test" & Chr(11) & "test"
End Sub

Related Microsoft Office Questions