copy text to notepad

W

ward

Hello,

I'm struggling wiht the following VBA problem:
I have some text in a textbox control, and i want to copy
this text to a new instance of Notepad (so that the user
can print the text using the notepad print functionality)
How can i do this?
Maybe some copy-paste method?

This is how far i got (which does not work obviously):

txtLog.Copy
Shell "Notepad", vbNormalFocus

thanks,

Ward
 
M

Martin Seelhofer

Hi Ward
I'm struggling wiht the following VBA problem:
I have some text in a textbox control, and i want to copy
this text to a new instance of Notepad (so that the user
can print the text using the notepad print functionality)
How can i do this?

Use a variable of type DataObject (don't forget the keyword New)
to write the text of your textbox into the clipboard. Then start
Notepad in the way you already do and Send some keys to it:

Dim dobj As New DataObject
Dim npad As Double

dobj.SetText "Hello world!" ' change this to your textbox
dobj.PutInClipboard
npad = Shell("Notepad.exe", vbNormalFocus)
AppActivate npad
SendKeys "^v"



Cheers,
Martin
 
J

Jay Freedman

Hi, Ward,

Because Notepad doesn't support macros or scripting, trying to automate it
from VBA isn't going to work. Instead, use Word itself. Create a temporary
document, print it, and close the document without saving. Here's some
sample code:

Private Sub cmdPrint_Click()
Dim tmpDoc As Document

If Len(txtLog.Text) > 0 Then
Application.ScreenUpdating = False
Set tmpDoc = Documents.Add
With tmpDoc
.Range.Text = txtLog.Text
.PrintOut Background:=False
.Close SaveChanges:=wdDoNotSaveChanges
End With
End If
Me.Hide
End Sub
 
W

ward

-----Original Message-----
Hi Ward
Use a variable of type DataObject (don't forget the keyword New)
.................
Cheers,
Martin


Works great, thanks!
Ward
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top