Solved

VB Script Microsoft Word Table remove extra line break

Posted on 2016-08-28
6
66 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 20

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 20
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 20
ID: 41778568
thank you and you're welcome ~ happy to help
0
 
LVL 20
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
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.

685 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