View Full Version : Opening Notepad and pasting


davesmith202
05-19-2008, 12:49 AM
How can I open notepad, store a variables value into the clipboard and then paste it into this open notepad windows?

Thanks,

Dave

ErikSnoek
05-19-2008, 01:19 AM
I'm not sure how to do it through clipboard, but it's pretty easy to do it with a temporary file. Take a look here:

Sub TextToNotepad()
Dim strText As String, strFileName As String, objFileSys As Object, objTextFile As Object
strText = "Throw this text to notepad!!"
strFileName = "c:\windows\temp\temptext.txt"

Set objFileSys = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFileSys.CreateTextFile(strFileName, True)
objTextFile.Write strText
objTextFile.Close

Shell "notepad " & strFileName
Kill strFileName
End Sub

I hope it will help you :)

davesmith202
05-19-2008, 02:12 AM
That is great thank you. :)

I also want to try something else. Can I set up a command button that when clicked will store the contents of my unbound field to the clipboard? How would I do that?

ErikSnoek
05-19-2008, 02:26 AM
That is great thank you. :)

I also want to try something else. Can I set up a command button that when clicked will store the contents of my unbound field to the clipboard? How would I do that?
Here's one way to do it:
Private Sub cmdTest_Click()
If Not IsNull(txtTest.Value) Then 'checking if the txtTest (textbox) is empty
txtTest.SetFocus
txtTest.SelLength = Len(txtTest.Value) 'selecting the text in the textbox
End If

DoCmd.RunCommand acCmdCopy 'copying the selected text to clipboard
End Sub

Otherwise you can use some API calls, as described here:
http://support.microsoft.com/?kbid=210216

davesmith202
05-19-2008, 02:38 AM
How would I modify that to copy the value in a string to the clipboard?

ErikSnoek
05-19-2008, 02:53 AM
How would I modify that to copy the value in a string to the clipboard?
You could store the string in an invisible textbox.. but I guess the best way is to just paste all this in a new module:
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) _
As Long
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) _
As Long
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, _
ByVal dwBytes As Long) As Long
Declare Function CloseClipboard Lib "User32" () As Long
Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) _
As Long
Declare Function EmptyClipboard Lib "User32" () As Long
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, _
ByVal lpString2 As Any) As Long
Declare Function SetClipboardData Lib "User32" (ByVal wFormat _
As Long, ByVal hMem As Long) As Long

Public Const GHND = &H42
Public Const CF_TEXT = 1
Public Const MAXSIZE = 4096

Function ClipBoard_SetData(MyString As String)
Dim hGlobalMemory As Long, lpGlobalMemory As Long
Dim hClipMemory As Long, X As Long

' Allocate moveable global memory.
'-------------------------------------------
hGlobalMemory = GlobalAlloc(GHND, Len(MyString) + 1)

' Lock the block to get a far pointer
' to this memory.
lpGlobalMemory = GlobalLock(hGlobalMemory)

' Copy the string to this global memory.
lpGlobalMemory = lstrcpy(lpGlobalMemory, MyString)

' Unlock the memory.
If GlobalUnlock(hGlobalMemory) <> 0 Then
MsgBox "Could not unlock memory location. Copy aborted."
GoTo OutOfHere2
End If

' Open the Clipboard to copy data to.
If OpenClipboard(0&) = 0 Then
MsgBox "Could not open the Clipboard. Copy aborted."
Exit Function
End If

' Clear the Clipboard.
X = EmptyClipboard()

' Copy the data to the Clipboard.
hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)

OutOfHere2:

If CloseClipboard() = 0 Then
MsgBox "Could not close Clipboard."
End If

End Function
Source: http://support.microsoft.com/?kbid=210216

And then:


Dim strTest as String
strTest = "To the clipboard!"
ClipBoard_SetData strTest

davesmith202
05-19-2008, 04:57 AM
Thank you!