Solved

wrap long string on multiple lines depending on number of characters

Posted on 2008-10-07
8
476 Views
Last Modified: 2008-10-08
Experts:

A query returns a long string from a database. I need to be able to take this long string - call it strTitle - and perform the following:

if the string is longer than 45 characters, go to the previous blank space (that is, the blank preceeding the 45th character), insert vbNewLine & 3 tab characters (chr(9)), and then continue the string. On this second line, perform the same test as on the first (that is, check that it does not exceed 45 characters, and if it does, back up to the nearest blank space, insert vbNewLine & 3 tab characters (chr(9)), and then continue the string.) Continue this process until the end of the string.

TIA!
crafuse
0
Comment
Question by:crafuse
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22660066
It might be useful to know where and how you are displaying this?  GDI+ has ways to handle word wrapping for you automatically with a layout Rectangle and StringFormat.
0
 

Author Comment

by:crafuse
ID: 22660166
TheLearnedOne -

I'm simply doing a replace of a bookmark in Word, and the string needs to be formatted in this manner.

This is my call to the replace function:

RangeReplace(docClient, "Titinv", RemoveSpaces(strtitle)) 'RemoveSpaces removes any blanks exceeding one character

And this is how I do the actual replace:

Private Sub RangeReplace(ByRef doc As Object, ByVal strBookmark As String, ByVal strValue As String, Optional ByVal blnMakeBold As Boolean = False)

        Dim wordRange As Object
        Dim wordBookmark As Object
        wordBookmark = doc.Bookmarks(strBookmark)
        wordRange = wordBookmark.Range
        wordRange.Text = strValue
        If blnMakeBold Then wordRange.Bold = True
        wordRange.Bookmarks.Add(strBookmark)
        wordRange = Nothing
        wordBookmark = Nothing
        Application.DoEvents()

    End Sub

So you see, I just need a function that behaves (I hope) the way I've described.

TIA
crafuse (somewhat unlearned!)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 22660941
Word automation is a completely different beast.  Are you looking to wrap before inserting text into the document?
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:crafuse
ID: 22661023
Yes, so that the function would return someling like

RangeReplace(docClient, "Titinv", (substrTitinv1 & chr(10) & chr(9) & chr(9) & chr(9) & subStrTitinv2 & chr(10) & chr(9) & chr(9) & chr(9) etcetera)

Where the substrTitinv* are the cut up peices of the string. Does this make sense?

Thanks,
crafuse
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 22670664
The following code works perfectly for me. I wrote this demo specifically for you.
        Dim str As String = "Please do not respond to this comment; we will be monitoring your question for activity from the Experts.  If you think that your question may be applicable in zones other than those listed above, please make a post to that effect in the Community Support question linked at the bottom of this comment."
        Dim str2 As New System.Text.StringBuilder
        Dim start = 0, index As Integer
        For i As Integer = 44 To str.Length - 1 Step 45
            index = str.LastIndexOf(" ", i)
            str2.Append(str.Substring(start, index - start) & "(Start:" & start & ", Index:" & index & vbCrLf & Chr(9) & Chr(9) & Chr(9))
            start = index
        Next
        str2.Append(str.Substring(start, str.Length - start))
        MessageBox.Show(str2.ToString)

Open in new window

0
 

Author Comment

by:crafuse
ID: 22671416
CodeCruiser -

Don't know what to say - that's better than awesome. But I'll leave it at that.
I made a couple of minor modifications - had to increment start = index + 1 to manage the blank on which the string chopped, and then had to make only a minor change to the actual bookmark's location, and now it works great. Many thanks!

Function FormatTitle(ByVal sSrc As String) As String

        Dim str2 As New System.Text.StringBuilder
        Dim start = 0, index As Integer
        For i As Integer = 34 To sSrc.Length - 1 Step 35
            index = sSrc.LastIndexOf(" ", i)
            'str2.Append(sSrc.Substring(start, index - start) & "(Start:" & start & ", Index:" & index & vbCrLf & Chr(9) & Chr(9) & Chr(9))
            str2.Append(LTrim(sSrc.Substring(start, index - start) & vbCrLf & Chr(9) & Chr(9) & Chr(9)))
            start = index + 1
        Next
        str2.Append(sSrc.Substring(start, sSrc.Length - start))
        FormatTitle = str2.ToString
        'MessageBox.Show(str2.ToString)

    End Function
0
 
LVL 14

Expert Comment

by:ptakja
ID: 22675764
Clever solution. I would recommend the following minor change to improve readability:

str2.AppendFormat("{0}{1}{2}{2}{2}",  sSrc.Substring(start, index - start).TrimStart(), vbCrLf, Chr(9))

If you are not familiar with the AppendFormat method of the StringBuilder (or the .Format method of the string object), you specify indexed placeholders in the string using the curly braces, and then specify the values that go into the placeholders. The 1st value in the list (or array with index 0) goes into placeholder 0, the next one into 1 etc....

Personally, I like this method a LOT better than concatinating strings using the "&" operator and all the extra quotes.

For example, what do you think is easier to read?

Dim strX As String = "The value '" & CurrentValue & "' is not valid the " & Operation & " operation."

OR

Dim strX As String = String.Format("The value '{0}' is not valid for the {1} operation.", CurrentValue, Operation)

Both of the lines above will result in an eqivalent value in the variable strX.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question