PDA

View Full Version : Set noteApp = CreateObject("notepad.Application") , failed



uktous
09-15-2012, 10:25 PM
Hi,

I want to use excel vba to open notepad.

However, the following code does not work.

I got this error message:
Run-time error 429
ActiveX component can’t create object

I guess I should choose a reference specific for notepad.

Anyone can help?




Dim noteApp As Object
Set noteApp = CreateObject("notepad.Application")


Thanks

GTO
09-15-2012, 11:32 PM
Greetings uktous,

In plain English, what is it, that you are wanting to do?

Mark

uktous
09-15-2012, 11:40 PM
Greetings uktous,

In plain English, what is it, that you are wanting to do?

Mark

I want to use excel to open notepad.
Then, I copy data to excel then paste to notepad.

I know that using shell will be easier, but I want to learn using create object method.

GTO
09-16-2012, 12:06 AM
I want to use excel to open notepad.
Then, I copy data to excel then paste to notepad.

I know that using shell will be easier, but I want to learn using create object method.

Greetings and Saluttions,

Notepad is an Application, specifically, a text editor. I am about 'toast, but for whoever gets to help, please explain the part in red.

snb
09-16-2012, 05:14 AM
you can do that in one line:


sub snb()
with createobject("scripting.filesystemobject")
.createtextfile(G:\OF\result.txt").write .opentextfile("G:\OF\example.txt").readall
end with
end sub


what can be done more simply using:

sub snb()
name "G:\OF\example.txt" as "G:\OF\result.txt"
end sub


The nice thing of VBA is, that you don't need to open notepad to handle text files.
The userinterface doesn't have such a facility.
So trying to open notepad in VBA is returning to the deficiencies of the userinterface.

Aflatoon
09-17-2012, 01:12 AM
Also since Notepad does not work as an automation server, using Createobject will not work.

uktous
09-17-2012, 05:55 AM
Also since Notepad does not work as an automation server, using Createobject will not work.

thank you for all replies
very helpful

one more question

i search ""CreateObject("notepad.Application")"" via google.
I got 176000 result

my question is that if it does not work, why so many people write such code to web :eek:

Aflatoon
09-17-2012, 06:14 AM
Perhaps for the same reason you did. ;)

snb
09-17-2012, 06:40 AM
By asking this question you added some more hits ...

Kenneth Hobs
09-17-2012, 07:21 AM
Why don't you just use MSWord? It allows automation.

Here are some example routines if you want to stick with notepad.
Const NPMsg As String = "Notepad ID = "

Private Const GW_CHILD = 5
Private Const WM_SETTEXT = &HC
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Const WM_CLOSE = &H10
Private Const EM_REPLACESEL = &HC2
Private Const EM_SETSEL = &HB1
Private Const EM_SETMODIFY = &HB9
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hWnd As Long, lpdwProcessId As Long) As Long

Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String) As Long

Private Declare Function GetWindow Lib "user32" _
(ByVal hWnd As Long, ByVal wCmd As Long) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long

Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Any) As Long

Private Declare Function MoveWindow Lib "user32" _
(ByVal hWnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _
ByVal nHeight As Long, ByVal bRepaint As Long) As Long

Private Declare Function SetWindowPos Lib "user32" _
(ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, _
ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

''To update the cell contents from Notepad.
Public Function GetNote(Ra As Range, hWnd As Long) As Boolean
GetNote = False
If hWnd = 0 Then Exit Function
Ra.FormulaLocal = ReadNotepad(hWnd)
GetNote = True
End Function

'Passing the cell contents in Notepad.
'HWnd new start time is omitted, it returns the hWnd.
Public Function PutNote(Ra As Range, Optional hWnd As Long = 0) As Long
Dim strText As String
If hWnd = 0 Then hWnd = OpenNotepad()
PutNote = hWnd
If hWnd = 0 Then Exit Function
strText = Ra.FormulaLocal
strText = Replace(strText, vbLf, vbCrLf)
WriteNotepad hWnd, strText
End Function

'HWnd unsaved to clear the flag specified in Notepad.
Public Function SetSavedNotepad(hWnd As Long) As Long
Dim i As Long
i = GetWindow(hWnd, GW_CHILD)
SendMessage i, EM_SETMODIFY, 0, 0
SetSavedNotepad = i
End Function

'HWnd Close Notepad specified.
Public Sub CloseNotepad(hWnd As Long)
SetSavedNotepad hWnd
SendMessage hWnd, WM_CLOSE, 0, 0
End Sub

'Start a new notepad, hWnd returned.
Public Function OpenNotepad(Optional iWindowState As Long = vbNormalFocus) As Long
Dim hWnd As Long
Dim ProcID As Long, ThreadID As Long
Dim i As Long, j As Long, k As Long
On Error GoTo Err1
i = Shell("notepad.exe", iWindowState)
If i = 0 Then GoTo Err1
hWnd = 0
Do
'hWnd = FindWindowEx(0, hWnd, "Notepad", "?? - ???")
hWnd = FindWindowEx(0, hWnd, "Notepad", vbNullString)
If hWnd = 0 Then GoTo Err1
ThreadID = GetWindowThreadProcessId(hWnd, ProcID)
Loop Until i = ProcID
i = SetWindowText(hWnd, NPMsg & ProcID)
'MoveWindow hWnd, 0, 50, 300, 200, 1
'Z-order if you change the SetWindowPos
SetWindowPos hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
OpenNotepad = hWnd
Exit Function
Err1:
MsgBox "Error", , NPMsg
OpenNotepad = 0
End Function

'HWnd content specified in Notepad, replace the specified character.
Public Function WriteNotepad(hWnd As Long, strTextAll As String) As Boolean
Dim i As Long
i = GetWindow(hWnd, GW_CHILD)
WriteNotepad = (0 <> SendMessageStr(i, WM_SETTEXT, 0, strTextAll))
End Function

'HWnd specified in Notepad, add the specified character. ????? With a new line.
'IPos = 0: the current cursor position
'-1: Top
'1: last
Public Function WriteLineNotepad(hWnd As Long, strText As String, Optional iPos As Long = 0) As Boolean
WriteLineNotepad = WriteTextNotepad(hWnd, strText & vbNewLine, iPos)
End Function

'HWnd specified in Notepad, add the specified character. No newline.
'IPos = 0: the current cursor position
'-1: Top
'1: last
Public Function WriteTextNotepad(hWnd As Long, strText As String, _
Optional iPos As Long = 0) As Boolean
Dim i As Long
i = GetWindow(hWnd, GW_CHILD)
Select Case iPos
Case -1
SendMessage i, EM_SETSEL, 0, 0
Case 1
SendMessage i, EM_SETSEL, 0, -1 'select all
SendMessage i, EM_SETSEL, -1, 0 'Deselect (move the cursor to the end of the selected area)
End Select
WriteTextNotepad = (0 <> SendMessageStr(i, EM_REPLACESEL, 0, strText))
End Function

'HWnd specified the contents of the Notepad, you get a letter.
Public Function ReadNotepad(hWnd As Long) As String
Dim i As Long
Dim j As Long
Dim x As String
i = GetWindow(hWnd, GW_CHILD)
j = 1 + SendMessage(i, WM_GETTEXTLENGTH, 0, 0)
x = String(j, Chr(0))
SendMessageStr i, WM_GETTEXT, j, x
ReadNotepad = x
End Function



Option Explicit
Public Sub Example1()
PutNote ActiveCell
End Sub

Public Sub Example2()
Dim Cell As Range
Dim hWnd As Long
hWnd = OpenNotepad()
If hWnd <> 0 Then
For Each Cell In ActiveSheet.UsedRange
WriteLineNotepad hWnd, Cell.Address(False, False) & ":" & Cell.Value
Next
End If
End Sub

snb
09-17-2012, 07:30 AM
@KH

Is this meant to be encouraging or discouraging ? :)

Kenneth Hobs
09-17-2012, 08:38 AM
I am always here to help the user. Some may not interpret it that way. I provide solutions. It is up to the user to decide if that helps or not. It is hard for me to read minds so I just make a best effort. :)

GTO
02-26-2013, 12:39 AM
Neat examples Ken!

I just remembered this and got to play a tiny bit, and wanted to say Thank You :-)

Mark