Left, Mid, Right(String) - problem

G

Gina

Hi.

I have to break down a string into some more convenient parts... but the
string should be cut where a <space> is

If numLetters > fixedLen Then
strTemp = Left(strToComp, fixedLen)
strRest = Mid(strToComp, fixedLen + 1)
pos = InStr(fixedLen - 10, strTemp, " ")

If pos <> 0 Then
posTemp = pos - 1
Else
posTemp = pos
End If

--> strFirst = Left$(strTemp & vbCrLf, posTemp)
.TypeText Text:=strFirst
.MoveDown Unit:=wdLine, Count:=1
strRest = Mid$(strToComp, pos + 1)
strTemp = strRest

.....
Had some problems --> when pos = 0 so I use posTemp
Think that could give again problems if a user types more than one space ...
maybe 10, which could easily be

what I actually need is not the fixedLen - 10 (assuming that most words
aren't 10 chars long ... oh dear but I don't know what I could use instead
hope I could explain my problem somehow clear ?

I would be grateful for any help or other suggetions how to get it sorted
properly
TIA
Gina
 
P

PC Datasheet

Take a look at the the InStr function in the Help file. You need to use that
in what you are doing.
 
J

John Nurick

Hi Gina,

This may give you some ideas:


Function WrapStringToLength(S As Variant, MaxLength As Long) As Variant
Dim arWords As Variant
Dim strLine As String
Dim strLines As String
Dim j As Long

If IsNull(S) Then Exit Function

arWords = Split(S, " ")
For j = 0 To UBound(arWords)
If Len(strLine) + Len(arWords(j)) + 1 <= MaxLength Then
'Next word fits on current line
strLine = strLine & arWords(j) & " "
ElseIf Len(arWords(j)) >= MaxLength Then
'Next word occupies a whole line
strLine = strLine & vbCrLf & arWords(j) & vbCrLf
Else
'Next word doesn't fit on current line so end line
j = j - 1
strLines = strLines & strLine & vbCrLf
strLine = ""
End If
DoEvents
Next
strLines = strLines & strLine

If Right(strLines, 2) = vbCrLf Then
strLines = Left(strLines, Len(strLines) - 2)
End If

WrapStringToLength = strLines
End Function
 
R

Rob Oldfield

Can I just ask what this is for? If it's a form or report why not just
resize the relevant control and let Access worry about where to break the
lines?
 
G

Gina

Hi John,

your code looks very good .... I love functions

Big ! Thanks ! for this

Gina :))
 
G

Gina

Rob ... it's not that easy

problem is that users will have to type short or long text (as some kind of
description of work depending on the number of material somewhere else)

so it's a memo field. when I send the whole stuff to word (invoice
template )there's a fixed grid/table on it .... the lines there are not
allowed to grow in width or height.
so the text lines do not fit when they are too long or in other words some
of the text won't be visible then.

Gina
 
D

Douglas J. Steele

I realize that, Gina. I was simply pointing out to Steve (PC Datasheet) that
his suggestion was inappropriate, since you were already using InStr.

In any case, I think you've already got an answer.
 

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