Solved

VB Script Microsoft Word Table remove extra line break

Posted on 2016-08-28
6
48 Views
Last Modified: 2016-08-31
How can I prevent the following code from adding a line

tbl.cell(row,5).range.text = replace(tbl.cell(row,5).range.text, "2017",(cfy+1))

Screenshot #1 (Table prior to script with header align at bottom):  http://www.screencast.com/t/NawU2AilNy
Screenshot #2 (Table after script with un-wanted line break disrupting the align at bottom appearance) :  http://www.screencast.com/t/R9NpAJoRm
0
Comment
Question by:maxdbase
  • 4
6 Comments
 
LVL 19

Accepted Solution

by:
crystal (strive4peace) - Microsoft MVP, Access earned 500 total points
ID: 41773739
Chr(10) = line-feed
chr(13) = carriage-return

not sure if Word uses Chr(10) like Excel or Chr(13) & Chr(10) ... but you can try replacing line breaks with 1 character and then replacing 2 characters with 1 character:

either
replace(replace(Text-To-Replace, chr(10), " "),"  "," ")

Open in new window

or
replace(replace(Text-To-Replace, chr(13) & chr(10), " "),"  "," ")

Open in new window

0
 

Author Comment

by:maxdbase
ID: 41773771
Wow, it worked,

http://www.screencast.com/t/ydKYV0xB

I slightly modified the example as follows

tbl.cell(row,c).Range.Text = replace(replace(tbl.cell(row,c).Range.Text, chr(10) , " "),"  "," ")
                              tbl.cell(row,c).Range.Text = replace(replace(tbl.cell(row,c).Range.Text, chr(13) , " "),"  "," ")

You're the best!!!
0
 
LVL 19
ID: 41773777
you're welcome ~ happy to help ... and thanks for sharing what you did. btw, if you are going to do multiple operations with the same object, it helps readability and performance to use WITH
with tbl.cell(row,c).Range
   .text = replace(replace(.Text, chr(10) , " "),"  "," ")
   .text = "blah blah"
end with 

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Expert Comment

by:aikimark
ID: 41775324
You might use vbCr, vbLF, and vbCrLf intrinsic constants in place of the Chr() functions.  This will make your code a bit easier to read/understand/maintain and be faster performer if you have a lot of data.

If you do have a lot of data and fast performance is a necessity, you might try invoking the Replace() functions against an array.  The transfer of data can take place in one from and one to operation (per area).  An example of this can be found in this article:
https://www.experts-exchange.com/articles/2253/Fast-Data-Push-to-Excel.html
0
 
LVL 19
ID: 41778568
thank you and you're welcome ~ happy to help
0
 
LVL 19
ID: 41778581
please also note aikimark's message with the constant names. When helping others, I like to explain what they are since you cannot always use constants (ie: in Access queries)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

895 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

16 Experts available now in Live!

Get 1:1 Help Now