Solved

VB Script Microsoft Word Table remove extra line break

Posted on 2016-08-28
6
39 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
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…
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.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

747 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

12 Experts available now in Live!

Get 1:1 Help Now