Link to home
Start Free TrialLog in
Avatar of henrikba
henrikba

asked on

VBA/Word: Copy as plain text

When using MS Office 2000, the copy function normally returns RTF/HTML-results as default. This is very unpractical when I copy text from Word and paste it into eg. Outlook Express or Excel.

My normal work-around is to start Notepad, paste the clipboard there, and then copy it again. Another alternative (when the text is to be pasted into Office) is the Paste Special-function (paste as plain text).

I would like to copy content as plain text with an easy shortcut. I could not find any suitable built-in functions in Word, but I think VBA could be a solution.

Does anybody know a way to do this with VBA? (Eg. first copy, and then convert the clipboard to plain text)

Thanks a lot,
-HenrikB
ASKER CERTIFIED SOLUTION
Avatar of K7
K7

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of K7
K7

Hi
i just realises that there is a API call SetClipboardData i'm just working out how to use it and i'll give u the code as soon as i know.

Cat
Avatar of Richie_Simonetti
Just an idea,
Save the contents of clipboard to a string variable.
Set clipboard contents to value stored in that variable.
Paste the contents of clipboard wherever you want.
Do you need the code?
well thats what i tried to do but i couldent find a anything like the clipboard object in VB to then set clipboard contens to the contens of the variable.
So it would be interesting to know where functianality is implemented in Word.
Hi!

I've got no idea about MS Word 2K (lots of bugs too!), but this is what I know.

To set clipboard text to text-only format, do this:

Clipboard.SetText "Whatever text", vbCFText

OR

Clipboard.SetText MyString, vbCFText

That's it!

glass cookie : )

PS Some more info on formats:

Constant     Value     Description
vbCFRTF             -16639     Rich Text Format (.rtf file)
vbCFLink     -16640     DDE conversation information
vbCFText     1     Text (.txt file)
vbCFBitmap     2     Bitmap (.bmp file)
vbCFMetafile     3     Metafile (.wmf file)
vbCFDIB             8     Device-independent bitmap
vbCFPalette     9     Color palette
vbCFEMetaFile     14     Enhanced metafile (.emf file)
vbCFFiles     15     File list from Windows Explorer
But, there is a clipboard object in VB!
Well yes in VB there is but it is part of the VB library which is not referenced in Word so you don't get the clipboard object in word.
What you have to do is add a reference to VB6.OLB and then you can use the Clipboard object but every time i did that it crashed.
So i wrote a dll which can give you access to the clipboard object form word.

Cat

PS could you try and add VB6.OLB to the references in word and tell me if it crashes on you system as well, i'm just interested if that is a M$ 'feature' or if it is specific to my system.
thenks
Sorry, i forgot that was VBA.

Suposse you have jst set data on clipboard.
Try this:
Sub test()
Application.Selection.PasteSpecial , , , , wdPasteText
End Sub
Avatar of henrikba

ASKER

Hi guys, and thank you very much for your overwhelming help!! Im really sorry that I havent been here for so long, but I will try your different and interesting solutions now. I will return with supplements if needed.
Well, I have tried a bit here, and as you say, I miss the Clipboard object in Word. I get the selection-text to a string with:
my_text = Selection.Text.
I even think the string contains everything I need (including CR/LF etc). The challenge is to get this string into the clipboard.

When I try: "Clipboard.SetText my_text", I get error 424: Object required.


K7:
The VB6.OLB - I do not follow you here. Is this a file I should find on my system? Is this necessary if I go for your DLL-version?
The DLL; How can I create it? (I really want to, because it would be really cool and geeky to have my own DLL!) Can I install VB 6, write the three codelines you wrote, and compile as a DLL in a way? Can it be this simple? (I really have NO idea on creating DLLs, hehe)

glass_cookie:
Thank you for the tips, but by now I can't use the Clipboard object in VBA.

Richie_Simonetti:
About your last comment; pasting inside Word is no problem as I have mapped Ctrl-V to "Paste Special". I want to *copy* plain text to paste it in applications with no "Paste special"-function.
"I want to *copy* plain text to paste it in applications with no "Paste special"-function."

Take this in mind:
You copy a rich text section of word document.
Open Notepad and paste it.
Which is the result text?
Here, we don't need to format text before past it.
;)
hi
the VB6.OLB is a file that comes with VB6, it contains the clipboard object so bay adding this file to the references in word should enable you to use the clipboard.  And i htink that the problems i have been experiancing are specific to my system so it should work on ur system.  It would bprobably be the easisest solution.
The option with the writing ur own dll wouldent need that file (VB6.OLB) but it would requre other files, which come with vb6.
And yes to write the dll you have to install VB start a activeX Dll project, write those 3 lines, compile the dll and then add that dll to the references in word.

and then use it like this

Dim Clipboard As New Class1
Clipboard.Copy Selection.Text

i have written and packaged it a dell which maps all of the clipboard functions, i could sent it to you if u want so you don't have to install VB (but the package is over 4mg)

Cat

K7 is rigth, but i think that solution is a real heavy one.
I am thinking on a "dirty" way:
Could we paste the contents of clipboard in a text box and then, copy the contents of text box back to clipboard, and... paste the contents of clipboard on the target application?
K7:
I grabbed my VB 6.0 Learning edition, and found VB6.OLB in a folder called WIN98. I placed the file in my "Program Files", and linked it with Word. When I try to access the Clipboard-object, my Word crashes :-/
(I'm running Windows 2k and Office 2k)

Well, it would be nice to try your package - if you could send it to me, I would really apreciate it! It seems quite big, but if it's not too much trouble for you: My address is bakkulf@spray.no.


Richie_Simonetti:
I were up to the same idea myself, but stranded on the coding here too; I get my selection as a string with plain text, but HOW on earth can I get this to the clipboard without the clipboard-object from K7? Hm...

Again, I'm very greatful for the help I get, I'm closer than ever :)
Well, may be we can turn our approach upside-down.
Instead of code in Word-VBA, why don't we code in plain VB?
I mean, from VB we can access Word Object Model and for sure, we already have Clipboard object!
"I were up to the same idea myself, but stranded on the coding here too; I get my selection as a string
with plain text, but HOW on earth can I get this to the clipboard without the clipboard-object from
K7? Hm..."

Is a solution but at which cost in resource terms and performance hit!!!!

Anyway, if you have Hwnd of textbox, we could use API to put/get things on/off clipboard.
Example:
'We have a textbox called txtClip with already contains that infamous text we need to paste as plain one.

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_COPY = &H301
Private Const WM_PASTE = &H302

' this put our text on clipboard which is plain already.
sendmessage txtClip.hwnd, WM_COPY, 0&, byval 0

' this get our text on clipboard which is plain already.
sendmessage target.hwnd, WM_PASTE, 0&, Byval 0

What do you think?

Optionaly, we can do a little class inside Word with Clipboard funcionality.
Hi
when i sed add it to the references i ment the references to files used by word to run, i'm unclear if u did that.  to do that you have to open that visual basic editor from the Tools->Macro menue in word.  in the editor click on tools->references and add the file to the list.
i tried this method on a friends computer and it didn't work so it might be specific to systems with Win2K and Word2K i don't know.

I emailed u the dll and the project to creat it.  having VB installed the dll should work, but i'm not sure if it is possible to open thins kind of project in the Learning version of VB.  

i also like Richie_Simonetti solution of using a txtbox.  i also thought of API calls but directly to the clipboard but i was unable to get them to work. so with his solution u could just have a hidden form with a txtbox on it.  you could make it that form like a clopboard object by adding public subs so u could have a sub

Public sub Copy (Str as String)
  txtBox.Text = Str
  sendmessage txtBox.hwnd, WM_COPY, 0&, byval 0
end sub

if you call the form clipboard you could do the folowing in ur macro
Clipboard.Copy Selectio.Text

it dosent realy folow naming conventions but i like it

BTW Why dosen't M$ expose more of the capabilities of the underling clases of there controles???


Cat
Well, if your version of VB allows you to create activex dll, you could try this little class:
(Save it as clsClip.cls and add it to a new ActiveX dll project, compile it and reference it in Word)

'*********** begin file ***************
VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsClip"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit

Public Function SetClipboard() As String
Dim txt As String
txt = Clipboard.GetText
Clipboard.SetText txt
SetClipboard = txt
End Function

' ********** end of file **********************
Hi henrikba,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will suggest to:

    Accept Richie_Simonetti's comment(s) as an answer.
*** I need input.  Are you sure this will work?  It's amazing that there is no Clipobard object

henrikba, if you think your question was not answered at all or if you need help, you can simply post a new comment here.  Community Support moderators will follow up.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
yes. There is no clipboard object in Office as such. With Application object you could manage clipboard contents to some extents.
Let me develop a little idea before close this. :)
Hi all,
I am terribly sorry - I had forgotten my whole question history here. Haven't been logged into EE for months!!

1) By now, I have not received a working solution. Still, I think the best "non-tested" way is what Richie_Simonetti calls "dirty"; copy text, paste into text-field, and copy again. With this method, I could get a plain text copied from Word, ready to paste into any other application.

2) Because I have been so lazy, and all the others here (especially Richie_Simonetti and K7) have tried so hard to help me; how could I award some "honour" and points to them?

3) Richie_Simonetti; if you come up with a working solution, I would really appreciate it! But; do not put too much effort into this; I'm getting used to my original method now; starting Notepad (I've made a shortcut; I press WindowsKey-R (RUN), "n" and ENTER, and then I have Notepad), and copy it further on from there. Not really optimal, but working :)

Best regards,
-HenrikB
hi henrikba,
Thanks for returning to finalize this question.  

You can easily select any single expert to get the points by clicking the [Accept comment as Answer] button next to that expert's post.  Then click the "Excellent" radio button and then click [Submit].

If you want to split the points between two experts, add a new comment naming the experts and indicating how many points to give each.

If you stop responding again, I will reassert my original recommendation and a Moderator will forcibly accept Richie_Simonetti's comment as an answer.  It would be better all around if you finalize this one year old 'easy' 50-pt question as soon as possible.

-- Dan
I'll try to follow up now, and I would really appreciate any new comments on the question. I am totally agreeing - the question is old and (was) low-rated, and does not belong in an active EE-community.
-H
Recommended Dispostion:

  Accept Richie_Simonetti's comment(s) as an answer.

DanRollins -- EE database cleanup volunteer
OK, doesn't seem like we get more responses; can you please split the total 200 points to Richie_Simonetti and K7? Thanks for the help everybody, and sorry for the delays.

Regards, Henrik
A Moderator will handle this within a few days.
Recommended Dispostion:

   split points between Richie_Simonetti and K7

DanRollins -- EE database cleanup volunteer
Per recommendation, force-accepted.

Netminder
CS Moderator

Richie_Simonetti: points for you at https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20328617