Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Word, PowerPoint, Outlook, and Other Office Products > Copy a string to clipboard


Reply
 
Thread Tools Display Modes
  #1  
Old 04-28-2004, 02:43 AM
DemonLama's Avatar
DemonLama DemonLama is offline
Regular
 
Join Date: May 2003
Location: Naaldwijk, TheNetherlands
Posts: 82
Default Copy a string to clipboard


How do I copy the text from a string to the clipboard so it can be pasted elsewhere. The copy to clipboard will be triggered from a word macro.

I read a lot other threads but the all used clipboard.settext and I can't find this function in Word (VBA)
Reply With Quote
  #2  
Old 04-28-2004, 07:39 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

VBA does not have direct access to the Clipboard object. You can use some API to accomplish this. There is a KB article on this topic on MSDN.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #3  
Old 04-28-2004, 03:21 PM
jharter jharter is offline
Newcomer
 
Join Date: Feb 2004
Location: Wisconsin
Posts: 7
Default

If you have already highlighted (selected) the text to copy then the VBA command is:

Selection.Copy

To paste the selection, put the cursor in the proper position and then:

Selection.Paste

You can use other code to change the position of the cursor. The macro recorder is useful for that.
Reply With Quote
  #4  
Old 04-28-2004, 04:28 PM
italkid's Avatar
italkid italkid is offline
Down...

Retired Moderator
* Expert *
 
Join Date: Dec 2002
Location: Belgium.
Posts: 6,731
Default

Quote:
Originally Posted by MKoslof
VBA does not have direct access to the Clipboard object.
Oooh, yes it does...
Look for the DataObject in the VBA helpfiles (GetFromClipboard, PutInClipboard etc...)
Reply With Quote
  #5  
Old 04-29-2004, 07:53 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

OK, well I mis-spoke. I typically have used API for this, and older Word Macros (I am talking Word 97 and WordBasic, stuff I really didn't work with) had to rely on the API.

Now, here is an article that better explains this:

http://support.microsoft.com/default...b;en-us;183644

Basically, I didn't realize this but these new methods are wrappers for the API commands. You need to create a DataObject in order to implement it, which makes sense.

Learn something new everyday (though I will stick with my API routines ).
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #6  
Old 04-29-2004, 08:06 AM
italkid's Avatar
italkid italkid is offline
Down...

Retired Moderator
* Expert *
 
Join Date: Dec 2002
Location: Belgium.
Posts: 6,731
Default

Quote:
Learn something new everyday (though I will stick with my API routines).
He...and i should learn more about API because i don't know much of that...
Reply With Quote
  #7  
Old 05-07-2004, 01:41 AM
DemonLama's Avatar
DemonLama DemonLama is offline
Regular
 
Join Date: May 2003
Location: Naaldwijk, TheNetherlands
Posts: 82
Question Example of PutInClipboard code?

Quote:
Originally Posted by italkid
Oooh, yes it does...
Look for the DataObject in the VBA helpfiles (GetFromClipboard, PutInClipboard etc...)

Could you give an example of how I should put a text string into the clipboard?? I have the following code for now but I don't think I've got it right:
Code:
Sub GetClipBoardText()
      Dim MyData As DataObject
      Set MyData = New DataObject
      Dim cString As String
      Let cString = "Hello VB Forum"
      On Error GoTo NotText
      ' Assign string variable to clipboard.
      MyData.SetText (cString)
      MyData.PutInClipboard
NotText:
      If Err <> 0 Then
         MsgBox "Data on clipboard is not text."
      End If
End Sub
Reply With Quote
  #8  
Old 05-17-2004, 11:20 AM
italkid's Avatar
italkid italkid is offline
Down...

Retired Moderator
* Expert *
 
Join Date: Dec 2002
Location: Belgium.
Posts: 6,731
Default

Something like :
Code:
Dim MyText As DataObject, TextStr As String Set MyText = New DataObject MyText.SetText CStr("Hello VB Forum") MyText.PutInClipboard TextStr = MyText.GetText() MsgBox TextStr


Sorry for the late reply but i had a little vacation...
Reply With Quote
  #9  
Old 05-18-2004, 12:40 AM
DemonLama's Avatar
DemonLama DemonLama is offline
Regular
 
Join Date: May 2003
Location: Naaldwijk, TheNetherlands
Posts: 82
Cool

Quote:
Originally Posted by italkid
Sorry for the late reply but i had a little vacation...
No problem... In fact, it forced me to figure it out myself, with success...

The code I came up with?

Code:
Public Sub copyToClipboard() Dim MyData As DataObject Dim cAddress As String Set MyData = New DataObject Let cAddress = compileAddress(True) MyData.SetText cAddress MyData.PutInClipboard End Sub

And by the way we're talking VBA here!!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing a value in the registry using VB sanctos File I/O and Registry 5 12-23-2003 09:09 AM
parsing FEINDFOLIO General 12 12-17-2003 07:17 AM
get information from file *.url sphinx Word, PowerPoint, Outlook, and Other Office Products 2 01-17-2003 03:05 AM
Loop to format file kingesk General 2 09-10-2000 05:01 PM
File format problem kingesk General 1 09-08-2000 04:52 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->