Replace function and carriage returns

B

bz

I am using Word 2000 and XP.

I have a table containing formfields. In a macro I output the contents of
selected formfields to a text file and then later insert this file into a new
Word document and use the text to table conversion to create a table.
Sometimes, if one of the formfields contains carriage returns, the result is
that the text following the carriage return begins on the first column of the
next row (i.e. not the next line in the same cell).

The best solution would be to have the carriage returns work correctly
(within the same cell).

An alternative is to strip out the carriage returns. I tried doing this
using the Replace function. Before writing out to the text file, I copy the
..result of the formfield to a string
(myString=ActiveDocument.FormFields("myString").Result).
I then try to replace: myString=Replace(myString,"^p",""). Also tried with
myString=Replace(myString,Chr(13),"").

The result is that no replacement is made. This is the case no matter what
I try to use as a replacement string.

Thanks in advance for any help, bz
 
S

StevenM

To: Bz,

Perhaps you need something like:

'****
'*
'* Function: StripCr
'*
'* When a string ends with a "Carriage Return" marker, or a "Carriage Return"
'* and "Line Feed" markers, this function will strip them off from a string.
'*
'****
Function StripCr(ByVal sStr As String) As String
Dim nPos As Long
nPos = InStr(sStr, Chr(13))
If nPos > 0 Then
sStr = Left(sStr, nPos - 1)
End If
StripCr = sStr
End Function

Steven Craig Miller
 
B

bz

Thanks very much for your help, which pointed me in the right direction.
I used an extension of what you suggested:

Function StripCr(ByVal sStr As String) As String

Dim nPos As Long
nPos = InStr(sStr,Chr(13))

Do While nPos > 0
sStr = Left(sStr,nPos-1) & " " & Right(sStr, Len)sStr-nPos))
nPos = InStr(sStr,Chr(13))
Loop
StripCr=sStr

End Function
 
M

MikeT

To: Steven M

Hi Steven,

I am working on an issue that is indirectly related to Bz's issue. I am
trying to import the contents of a .txt file into an MS Access table through
VBA. The file is pipe delimited, and I have been able to successfully place
the delimited values into their corresponding fields within the first row of
the destination table. What I cannot figure out is how to take the last
value of the first row, which contains the carriage return, and begin
populating the second row of the destination table with the values to the
immediate right of the carriage return.

Any thoughts?

Regards,

Mike
 
D

Doug Robbins - Word MVP

By "second row of the destination table" do you mean the second (next)
record in the table?

To do that, you would need to add a new record (or index to the next record)
and then write the data to the appropriate field in that record.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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