Solved

wrap long string on multiple lines depending on number of characters

Posted on 2008-10-07
8
465 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
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now