Line added to pasted richtext in Word using VB

Posted on 2006-10-22
Last Modified: 2010-08-05
I have a VB application that builds a report in Word. The program grabs text from a database and pastes it into a Word bookmark. The text is richtext in nature. It all works like a charm except when it is pasted into Word it adds a line, not sure if it is a carriage return or line feed or what, but the results is that an extra line is always added. Here is the code that does the grabbing and the putting...

            Case Is = "Memo"
                frmMain.Controls(MyMemoField).SelStart = 0
                frmMain.Controls(MyMemoField).SelLength = Len(frmMain.Controls(MyMemoField).TextRTF)
                Clipboard.SetText frmMain.Controls(MyMemoField).SelRTF, vbCFRTF
                With mobjOdocument.Application.selection
                    .PasteAndFormat (wdPasteDefault)
                End With

That little extra line is messing up a realy nice report otherwise. Any ideas on where it is coming from or how to get rid of it would be greatly appreciated.
Question by:glenn_grubb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17786654
you might try this:

 Case Is = "Memo"
                frmMain.Controls(MyMemoField).SelStart = 0
                frmMain.Controls(MyMemoField).SelLength = Len(frmMain.Controls(MyMemoField).TextRTF)-2
                Clipboard.SetText frmMain.Controls(MyMemoField).SelRTF, vbCFRTF
                With mobjOdocument.Application.selection
                    .PasteAndFormat (wdPasteDefault)
                End With

or really check if the last character of the richtext is a carriage return/line feed, and only remove that from the richtext..

Author Comment

ID: 17786904
I solved the problem myself. Shoot!, I should have been more generous with the points!

I went to Word and recorded a macro that did what I was hoping to do. basically go to the cell where the Richtext was pasted and move to the end of the cell and hit the backspace key. I copied some of that macro and pasted it into my VB program and with a little of trial and error found that this change to my code will achieve waht i was looking for:

                With mobjOdocument.Application.Selection
                    .PasteAndFormat (wdPasteDefault)
                    .TypeBackspace 'to remove extra line feed
                End With

I added the snippet  .TypeBackspace 'to remove extra line feed to my original code and it worked.

LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 0 total points
ID: 17786918
Good work.
Question PAQed.

angelIII, page editor

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

Suggested Solutions

Title # Comments Views Activity
Surface Book vs Surface Pro 4 109
Word macro referencing data 8 39
Code snippet to grab a cell content of a table cell using VBA 7 37
Word Personalized Watermarks 5 59
Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…

740 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