Avoiding paragraph marks

S

scotty

Hello,

I'm trying to copy each line of a text (which is a name list) in an array; I
created a script like:


Dim i, j, oldname, newname
j = 1
For i = 1 To .FoundFiles.Count
oldname = .FoundFiles(i)
newname = ActiveDocument.Paragraphs(j)
Name oldname As newname
j = j + 1
Next i

Unfortunately, the so selected "newname" includes an invisible paragraph
mark, which leads to problems, as "newname" will actually become a file
name.

I think I can easily make a selection avoiding that paragraph mark, but I'd
rather stay out of the "select" command; is there a simple way to declare
"newname" as "ActiveDocument.Paragraphs(j) except paragraph mark" ?

Thanks very much in advance for any help,

Scotty
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < scotty > écrivait :
In this message, < scotty > wrote:

|| Hello,
||
|| I'm trying to copy each line of a text (which is a name list) in an
array; I
|| created a script like:
||
||
|| Dim i, j, oldname, newname
|| j = 1
|| For i = 1 To .FoundFiles.Count
|| oldname = .FoundFiles(i)
|| newname = ActiveDocument.Paragraphs(j)
|| Name oldname As newname
|| j = j + 1
|| Next i
||
|| Unfortunately, the so selected "newname" includes an invisible paragraph
|| mark, which leads to problems, as "newname" will actually become a file
|| name.
||
|| I think I can easily make a selection avoiding that paragraph mark, but
I'd
|| rather stay out of the "select" command; is there a simple way to declare
|| "newname" as "ActiveDocument.Paragraphs(j) except paragraph mark" ?

Try something quick and dirty like:

newname = Replace(newname, Chr(13), "")

after the line

newname = ActiveDocument.Paragraphs(j)

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jonathan West

scotty said:
Hello,

I'm trying to copy each line of a text (which is a name list) in an array; I
created a script like:


Dim i, j, oldname, newname
j = 1
For i = 1 To .FoundFiles.Count
oldname = .FoundFiles(i)
newname = ActiveDocument.Paragraphs(j)
Name oldname As newname
j = j + 1
Next i

Unfortunately, the so selected "newname" includes an invisible paragraph
mark, which leads to problems, as "newname" will actually become a file
name.

I think I can easily make a selection avoiding that paragraph mark, but I'd
rather stay out of the "select" command; is there a simple way to declare
"newname" as "ActiveDocument.Paragraphs(j) except paragraph mark" ?

Thanks very much in advance for any help,

First of all, it's a good idea to declare your vaiables as being of a
sepcific type, like this

Dim i as Long
Dim j as Long
Dim oldname as String
Dim newname as String

The reason is that this avoids possible confusion as to what kind of data
each variable contains.

Next, since i & j both contain the same values, you don't really need both
of them, so you can just use i.

On the issue of getting rid of the paragraph marks, there are two possible
approaches. One is to trim them from the string, and the other is not to
import them in the first place. The first approach would work like this

Dim i as Long
Dim oldname as String
Dim newname as String

For i = 1 To .FoundFiles.Count
oldname = .FoundFiles(i)
newname = ActiveDocument.Paragraphs(i)
newname = Left$(newname, Len(newname) - 1)
Name oldname As newname
Next i

(It is a good idea to indent lines within a for-next loop - it make it
easier to see where the loop starts & ends)

Alternatively, you can avoid importing the carriage return into the string
in the first place. This code would do that

Dim i as Long
Dim myRange as Range
Dim oldname as String
Dim newname as String

For i = 1 To .FoundFiles.Count
oldname = .FoundFiles(i)
Set myRange = ActiveDocument.Paragraphs(i).Range
myRange.MoveEnd Unit:=wdCharacter, Count:=-1
newname = myRange.Text
Name oldname As newname
Next i

In your case, the first option will probably be a bit quicker, but I've
included both so you can see how manipulating the objects themselves can get
you to where you want to go.
 
S

scotty

Thanks very much, Jonathan and Jean-Guy!

I succesfully included Jonathan option #2 in the script (the option which
avoids importing carriage returns in the first place);
I take note of the others suggestions for future uses (btw the Jean-Guy
"replace" instruction doesn't seem to work, but maybe I need to work on it;
anyway, you guys help me to keep learning and go on!

Best regards,

Scotty
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < scotty > écrivait :
In this message, < scotty > wrote:

|| Thanks very much, Jonathan and Jean-Guy!
||
|| I succesfully included Jonathan option #2 in the script (the option which
|| avoids importing carriage returns in the first place);
|| I take note of the others suggestions for future uses (btw the Jean-Guy
|| "replace" instruction doesn't seem to work, but maybe I need to work on
it;
|| anyway, you guys help me to keep learning and go on!

Are you using Word 97?
I believe it did not work under 97.... Since you did not mention your Word
version I took a chance...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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