Copy paste text to notepad vba
- User
Copy paste text to notepad vba
- Assistant
You can use the following VBA code to copy the selected text in a Microsoft Word document and save it to a new Notepad file. The code will use the Windows Script Host Object Model, which you must enable in the VBA editor. To do this, go to the VBA editor (press ALT + F11), click on Tools > References, and check 'Microsoft Scripting Runtime' and 'Windows Script Host Object Model'. Then, paste the following code into a new module.
Option Explicit Sub CopyTextToNotepad() Dim objFSO As FileSystemObject Dim objTextFile As TextStream Dim myString As String Dim notepadPath As String ' Check if any text is selected If Selection.Type <> wdSelectionNormal Then MsgBox "Please select some text first." Exit Sub End If ' Create FileSystemObject and TextStream objects Set objFSO = CreateObject("Scripting.FileSystemObject") notepadPath = Environ$("USERPROFILE") & "\Desktop\NotepadText.txt" ' Check if file exists, if not, create new file If objFSO.FileExists(notepadPath) Then Set objTextFile = objFSO.OpenTextFile(notepadPath, ForAppending) Else Set objTextFile = objFSO.CreateTextFile(notepadPath) End If ' Get the selected text myString = Selection.Text ' Write selected text to Notepad objTextFile.Write myString ' Close the text file objTextFile.Close ' Clean up Set objTextFile = Nothing Set objFSO = Nothing ' Inform the user MsgBox "Selected text has been saved to NotepadText.txt on your desktop." End Sub - User
Copy from textbox paste to notepad vba
- Assistant
In this example, I'll assume you have a UserForm with a TextBox named TextBox1 and a CommandButton named CommandButton1. To copy the text from the TextBox in the UserForm to a Notepad file, you can use the following VBA code:
- Open the VBA editor (press ALT + F11).
- Insert a UserForm into the project.
- Add TextBox1 and CommandButton1 to the UserForm.
- Double-click on CommandButton1 to open the code editor.
- Paste the following code into the code editor for the CommandButton_Click event:
Private Sub CommandButton1_Click() Dim objShell As Object Dim objFSO As Object Dim objTextFile As Object Dim notepadPath As String ' Create FileSystemObject Set objFSO = CreateObject("Scripting.FileSystemObject") notepadPath = Environ$("USERPROFILE") & "\Desktop\NotepadText.txt" ' Check if file exists, if not, create new file If objFSO.FileExists(notepadPath) Then Set objTextFile = objFSO.OpenTextFile(notepadPath, ForAppending) Else Set objTextFile = objFSO.CreateTextFile(notepadPath) End If ' Write text from TextBox to Notepad objTextFile.Write TextBox1.Text ' Close the text file objTextFile.Close ' Clean up Set objTextFile = Nothing Set objFSO = Nothing ' Open Notepad with the saved text Set objShell = CreateObject("WScript.Shell") objShell.Run "notepad.exe " & notepadPath Set objShell = Nothing ' Inform the user MsgBox "The text from TextBox1 has been saved to NotepadText.txt on your desktop and opened in Notepad." End Sub
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.