Solved

wrap long string on multiple lines depending on number of characters

Posted on 2008-10-07
8
466 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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…

930 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

19 Experts available now in Live!

Get 1:1 Help Now