How to load a string into the default text editor

Posted on 2004-11-18
Last Modified: 2010-05-02
I'l like to load a string straight into the users default text editor (or if necessary notepad)

I can do this by saving as a file but I'd like to by pass this and put the string straight into the users default editor.
They can then choose whether to save the file.
Question by:colindow
    LVL 2

    Expert Comment

    I'm not entirely sure what you mean, but would putting something into the users clipboard be acceptable?

        Clipboard.SetText "Default text would go in here."

    Your users then just need to paste this anywhere they wish.

    Author Comment

    As it happens this is what I'm doing at the moment. What I would like to do is present them with the default text editor with the text loaded.

    I do something similar where I create an instance of excel to give the user results. If they save the file they have a copy. If they don't there is nothing left on their disk.

    In this case I need to work out the app to use (i.e. the defualt text editor) then apply the text string to an empty document.
    LVL 2

    Expert Comment

    The registry entry [HKEY_CLASSES_ROOT\txtfile\shell\open\command] contains the default editor to open a textfile. Can you query this key to find the app, then use this to input the default text you require?
    LVL 2

    Expert Comment

    A little extra just to be aware.. if you google search that registry location, you'll find the first 50 entries or so solely talk about a virus/worm.. this might make searching for help on this subject a little tougher :(
    LVL 28

    Accepted Solution

    Try this:

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    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 SendMessageSTRING Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    Private Const WM_SETTEXT = &HC

    Private Sub Command1_Click()
    Dim lNotepadHwnd As Long, lNotepadEdit As Long
    Shell "Notepad.exe", vbNormalFocus
    lNotepadHwnd = FindWindow("Notepad", vbNullString)
    lNotepadEdit = FindWindowEx(lNotepadHwnd, 0&, "Edit", vbNullString)
    SendMessageSTRING lNotepadEdit, WM_SETTEXT, 256, "This is The Text I want to send"

    End Sub

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Join & Write a Comment

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now