Chariage Returns & Returns in Strings

D

Dax Arroway

I have a Macro that inserts a string of text. The text however are a couple
of paragraphs which need to be broken up. What are the characters to do
this?

My code, for example, says this:
Result="Billy went out to the market. His ball was blue. Jerry went to the
school. His ball was yellow. Mary stayed at home. She didn't have a ball."


What's returned is this:
Billy went out to the market. His ball was blue. Jerry went to the school.
His ball was yellow. Mary stayed at home. She didn't have a ball.

What I'd like returned is this:
Billy went out to the market. His ball was blue.
Jerry went to the school. His ball was yellow.
Mary stayed at home. She didn't have a ball.

How do I do that??? Thanks in advance for any help.
--Dax
 
J

Jay Freedman

The built-in value named vbCr represents a paragraph mark (which is a
more accurate name for "carriage return" -- considering that a
typewriter carriage is now mostly obsolete). So you could construct
your string as

Result="Billy went out to the market. His ball was blue." & vbCr & _
"Jerry went to the school. His ball was yellow." & vbCr & _
"Mary stayed at home. She didn't have a ball."

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
D

Dax Arroway

ARRrrrgh! Sufferin Suckatash! Gal-Darnit 255 character limit! Now I'm
throwing a Run-time error! I suppose the other answer is to put it in an
autotext entry. I'm trying to insert text into a table using a Macro. From
Doug Robins, my code is this:

Dim ddresult As String
ddresult = Selection.FormFields(1).result
Select Case ddresult
Case "Level 1"
For i = 2 To 5
Selection.Rows(1).Cells(i).Range.FormFields(1).result = "Result
for Level 1"
Next i
Case "Level 2"
For i = 2 To 5
Selection.Rows(1).Cells(i).Range.FormFields(1).result = "Result
for Level 2"
Next i
Case "Level 3"
For i = 2 To 5
Selection.Rows(1).Cells(i).Range.FormFields(1).result = "Result
for Level 3"
Next i
Case "Level 4"
For i = 2 To 5
Selection.Rows(1).Cells(i).Range.FormFields(1).result = "Result
for Level 4"
Next i
Case Else
For i = 2 To 5
Selection.Rows(1).Cells(i).Range.FormFields(1).result = "Result
for Level 5"
Next i
End Select

However, when I replce the result string I pass the limit. This is a
template that will be opened by others not on the server so I'm trying to get
everything into the form itself. Is there a way to replce the string
designation with an AutoText??? And if so, what would the code look like?
 
D

Dax Arroway

Hmmm. I think I hear what you're saying but my limited VBA knowledge
prevents me from making this work. I tried putting:

ActiveDocument.Unprotect
FmFld.Range.Fields(1).Result.Text = Str1
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

into the top of the code and it errors. I then tried putting it below the
Sub PCCDim1()
and the
Dim ddresult As String
but I got another error. I'm obviously doing something wrong.

Could you please tell me if I'm on the right path? And to let you know
about my full project, I've got a table that's 4 columns by 6 rows. First
row has a dropdown in it with 5 choices. Each choice will populate columns 2
to 4 with different data--the text strings I'm including in the Macros. So
for each row (I'm not sure if this is the best practice or not) I'm using an
instance of the same code (w/different names and different text strings) to
get the results I need. Therefore, each dropdown menu throughout column 1
runs a different Macro (PCCDim1, PCCDim2, etc.). Which makes me woder if I
need to include this code in each instance of the different Macros or if it
only needs to be called once.

So, A) am I using the right code? B) Am I putting it in the right place?
And C) Should I only put it once or in all the Macros?
 
J

Jay Freedman

Hi Dax,

Please help me to help you. Reply to this thread with the following
information:

- The number and the exact and complete text of the error message.

- The entire code of the macro that has the error, copied from the VBA
editor and pasted into the newsgroup post.

- If the error message includes a Debug button, click it. That will
open the VBA editor and yellow-highlight the line where the error
occurred. In the code you include in the post, point to that line.

Even without that information, I'll take a quick guess at what's
wrong: The code you inserted refers to a string named Str1, but the
rest of your macro doesn't contain any string with that name. Change
Str1 to the name of the string that contains the text to be inserted
into the field. And put the three lines *after* the place in the macro
that assigns the text to that string.

To answer your other question, you need these three lines in *every*
macro that tries to assign a long (more than 255 characters) string to
a form field. Putting it into only one of those macros is not
sufficient.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
D

Dax Arroway

Awesome, yes please help. Obviously, you're right, I don't have a text
string named Str1 and the code is inserted in the wrong place. But I don't
know how to "name" my text either. Case "Level 0" in the code below is as
far as I got in inserting the text into the macro but it is mostly in Cell(3)
which all of the long text, including paragraph marks, will reside. I didn't
include the other macros (PPCDim1, PPCD2, PPC4, 5, or 6), because they
actually don't break the 255 character limit and work fine, and even if they
do, with what you give me here I'll be able to apply there if needed. If I'm
counting characters right, Cell(4) will also throw the "StringTooLong"
run-time error.

Here's my code:

Sub PPCDim3()
'
' PPCDim3 Macro
' Macro created 1/23/2010 by Dax
'
ActiveDocument.Unprotect
!!! Run-time Error '424': Object required !!! -->
FmFld.Range.Fields(1).Result.Text = Str1
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

Dim ddresult As String
ddresult = Selection.FormFields(1).Result
Select Case ddresult
Case "Level 0"
For i = 2 To 4
Selection.Rows(1).Cells(2).Range.FormFields(1).Result = "Risk
Rating 0"
Selection.Rows(1).Cells(3).Range.FormFields(1).Result =
"Dangerousness/Lethality: Good impulse control and coping skills." & vbCr & _
"Interference with addiction recovery efforts: Emotional
concerns relate to negative consequences and effects of addiction. Able to
view these as part of addiction and recovery." & vbCr & _
"Social functioning: Relationships or spheres of social
functioning are being impaired but not endangered by patient's substance use.
Able to meet personal responsibilities and maintain stable meaningful
relationships despite the mild symptoms experienced." & vbCr & _
"Ability for self-care: Adequate resources and skills to
cope with emotional and behavioral problems." & vbCr & _
"Course of illness: Mild to moderate signs and symptoms with
good response to treatment in the past. Any past serious problems have a
long period of stability or past problems are chronic but do not pose
high-risk vulnerability."
Selection.Rows(1).Cells(4).Range.FormFields(1).Result =
"Low-intensity mental health services needed to coordinate medication and
case management services corrdinated with addiction and mental health
psycoeducation. Level I outpatient services that incorporates specific
services for dual diagnosis patients as well as chemical dependency only
patients and mental health only patients."

Next i
Case "Level 1"
For i = 2 To 4
Selection.Rows(1).Cells(2).Range.FormFields(1).Result = "Risk
Rating 1"
Selection.Rows(1).Cells(3).Range.FormFields(1).Result = "Long
string goes here."
Selection.Rows(1).Cells(4).Range.FormFields(1).Result = "Result
for Level 1 in Column 4"
Next i
Case "Level 2"
For i = 2 To 4
Selection.Rows(1).Cells(2).Range.FormFields(1).Result = "Risk
Rating 2"
Selection.Rows(1).Cells(3).Range.FormFields(1).Result = "Long
string goes here."
Selection.Rows(1).Cells(4).Range.FormFields(1).Result = "Result
for Level 2 in Column 4"
Next i
Case "Level 3"
For i = 2 To 4
Selection.Rows(1).Cells(2).Range.FormFields(1).Result = "Risk
Rating 3"
Selection.Rows(1).Cells(3).Range.FormFields(1).Result = "Long
string goes here."
Selection.Rows(1).Cells(4).Range.FormFields(1).Result = "Result
for Level 3 in Column 4"
Next i
Case "Level 4"
For i = 2 To 4
Selection.Rows(1).Cells(2).Range.FormFields(1).Result = "Risk
Rating 4"
Selection.Rows(1).Cells(3).Range.FormFields(1).Result = "Long
string goes here."
Selection.Rows(1).Cells(4).Range.FormFields(1).Result = "Result
for Level 4 in Column 4"
Next i
End Select
End Sub

I hope that's enough help for you to help me. I really appreciate it.
Thank you SO MUCH!
--Dax
 
J

Jay Freedman

OK, this makes more sense.

Think about the steps you need when you're inserting a string longer
than 255 characters in a form field, in plain English:

1. Assign the text to a string variable.
2. Unprotect the document.
3. Assign the string variable to the form field's result.
4. Reprotect the document.

You need to do those four steps, in that order, each time you insert a
long string. So the Case "Level 0" code becomes this (and note that I
declared the string variable LongString at the beginning of the macro,
which only needs to be done once; and I removed the "For i = 2 to 4"
loop because all that does is repeat the entire case three times):

Sub PPCDim3()

Dim LongString As String
Dim ddresult As String
ddresult = Selection.FormFields(1).Result
Select Case ddresult
Case "Level 0"
' For i = 2 To 4 <== get rid of this
Selection.Rows(1).Cells(2).Range.FormFields(1).Result =
"Risk Rating 0"

LongString = _
"Dangerousness/Lethality: Good impulse control and coping skills." &
vbCr & _
"Interference with addiction recovery efforts:
Emotional
concerns relate to negative consequences and effects of addiction.
Able to
view these as part of addiction and recovery." & vbCr & _
"Social functioning: Relationships or spheres of
social
functioning are being impaired but not endangered by patient's
substance use.
Able to meet personal responsibilities and maintain stable meaningful
relationships despite the mild symptoms experienced." & vbCr & _
"Ability for self-care: Adequate resources and skills
to
cope with emotional and behavioral problems." & vbCr & _
"Course of illness: Mild to moderate signs and
symptoms with
good response to treatment in the past. Any past serious problems
have a
long period of stability or past problems are chronic but do not pose
high-risk vulnerability."

ActiveDocument.Unprotect
Selection.Rows(1).Cells(3).Range.FormFields(1).Result =
LongString
ActiveDocument.Protect Type:=wdAllowOnlyFormFields,
NoReset:=True

LongString = _
"Low-intensity mental health services needed to coordinate medication
and
case management services corrdinated with addiction and mental health
psycoeducation. Level I outpatient services that incorporates
specific
services for dual diagnosis patients as well as chemical dependency
only
patients and mental health only patients."

ActiveDocument.Unprotect
Selection.Rows(1).Cells(4).Range.FormFields(1).Result =
LongString
ActiveDocument.Protect Type:=wdAllowOnlyFormFields,
NoReset:=True

' Next i <== get rid of this

When you modify Case "Level 1" and the other cases, follow the same
pattern.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
D

Dax Arroway

Thank you so much Jay, it's so close I can taste it! ...but she still
doesn't fly. I did as you instructed (and thank you very much for the
explaination in English! Makes total sense) getting rid of the <i = 2 to 4>
and <Next i> things (throughout all the macros!). Everything works (other
dropdowns in different rows, selections NOT Level 0 from the dropdown we're
working on, but when I select Level 0 from PCCDim3 I get a Run-time error
'4609' = String Too Long ...still. Clicking Debug highlights the
Selection.Rows(1).Cells(3).Range.FormFields(1).Result = LongString line.

I'm not sure what to do at this point. Maybe more information from me? I'm
running WinXP and Word03. Does that matter? I live in Oregon and it's not
raining outside--maybe that's it. *smile* I don't want the end user to be
able to modify the text so I unchecked the fill-in enabled boxes in the form
fields throughout columns 2, 3, and 4. Non of the form fields have bookmark
names. any other information helpful? When I cut and pasted the code into
the VE I had to 'skootch' things around to get rid of all the red font colors
so everything is black now exept for "Case" and "True".

Here's the entire PCCDim3 Macro:

Sub PPCDim3 ()

Dim LongString As String
Dim ddresult As String
ddresult = Selection.FormFields(1).Result
Select Case ddresult

Case "Level 0"
Selection.Rows(1).Cells(2).Range.FormFields(1).Result = "Risk
Rating 0"

LongString = _
"Dangerousness/Lethality: Good impulse control and coping
skills." & vbCr & _
"Interference with addiction recovery efforts: emotional
concerns relate to negative consequences and effects of addiction. Able to
view these as part of addiction and recovery." & vbCr & _
"Social functioning: Relationships or spheres of social
functioning are being impaired but not endangered by patient's substance use.
Able to meet personal responsibilities and maintain stable meaningful
relationships despite the mild symptoms experienced." & vbCr & _
"Ability for self-care: Adequate resources and skills to
cope with emotional and behavioral problems." & vbCr & _
"Course of illness: Mild to moderate signs and symptoms with
good response to treatment in the past. Any past serious problems have a
long period of stability or past problems are chronic but do not pose
high-risk vulnerability."

ActiveDocument.Unprotect
Selection.Rows(1).Cells(3).Range.FormFields(1).Result =
LongString <===Hightlighted Yellow
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

LongString = _
"Low-intensity mental health services needed to coordinate
medication and case management services corrdinated with addiction and mental
health psycoeducation. Level I outpatient services that incorporates
specific services for dual diagnosis patients as well as chemical dependency
only patients and mental health only patients."

ActiveDocument.Unprotect
Selection.Rows(1).Cells(4).Range.FormFields(1).Result = LongString
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True

Case "Level 1"
Selection.Rows(1).Cells(2).Range.FormFields(1).Result = "Risk
Rating 1"
Selection.Rows(1).Cells(3).Range.FormFields(1).Result =
"Dangerousness/lethality: Adequate impulse control and coping skills to deal
with any thoughts of harm to self or others."
Selection.Rows(1).Cells(4).Range.FormFields(1).Result = "Result
for Level 1 in Column 4"

Case "Level 2"

Selection.Rows(1).Cells(2).Range.FormFields(1).Result = "Risk
Rating 2"
Selection.Rows(1).Cells(3).Range.FormFields(1).Result =
"Dangerousness/lethality: Suicidal ideation or violent impulses which require
more than routine outpatient monitoring."
Selection.Rows(1).Cells(4).Range.FormFields(1).Result = "Result
for Level 2 in Column 4"

Case "Level 3"

Selection.Rows(1).Cells(2).Range.FormFields(1).Result = "Risk
Rating 3"
Selection.Rows(1).Cells(3).Range.FormFields(1).Result =
"Dangerousness/lethality: Frequent impulses to harm self or others. Patient
is not imminently dangerous in a 24-hour setting. No longer experiencing
command hallucinations and is responding to medication but needs further
stabiliztation."
Selection.Rows(1).Cells(4).Range.FormFields(1).Result = "Result
for Level 3 in Column 4"

Case "Level 4"

Selection.Rows(1).Cells(2).Range.FormFields(1).Result = "Risk
Rating 4"
Selection.Rows(1).Cells(3).Range.FormFields(1).Result =
"Dangerousness/lethality: Imminent risk of suicide; psychosis with
unpredictable, disorganized or violent behavior; or gross neglect of self
care."
Selection.Rows(1).Cells(4).Range.FormFields(1).Result = "Result
for Level 4 in Column 4"

End Select

End Sub
 
J

Jay Freedman

You're right, you were very close. Unfortunately I misled you on a small but
crucial detail. Sorry to have caused such trouble.

When you're using the LongString, you have to assign it to

....Range.Fields(1).Result.Text

instead of

....Range.FormFields(1).Result

I've tested it, and it works.

I'm happy for you that it isn't raining in Oregon. I'm in Pennsylvania, and
we've just had 2" of rain in the last 15 hours. Not quite what my friends in
California had recently, but not nice.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
D

Dax Arroway

HALLELUHA!!! And the clouds part and the sun shines and birds sing from the
heavens! Thank you SO much! It works perfectly! I really, really
appreciate your time and stick-to-itiveness to this. Computer science is
"funny" sometimes as to how exact you have to be. A missing period here or
misplaced case can really mess with you. I'm just glad you're smart enought
to know where it was. Lord knows I'm not... but we're learning all the time!
Hope the weather is nicer to ya. Till next time, thanks a million!

--Dax
 

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