0

I'm new to programming and so I have come here for some help.

I need to copy a text field from excel and paste it in a notepad and then save the notepad with the specific name to a specific location. These things should be done with the help of Macro.

Any help would be highly appreciated

I was able to copy the text from excel and paste in Notepad, not sure how to save it in a new location

sub Macro2()

    Range("A5").Select
    Selection.Copy
    Shell "notepad.exe", vbMaximizedFocus
    SendKeys "^V"

End Sub
pnuts
  • 58,317
  • 11
  • 87
  • 139
narayanan
  • 3
  • 1
  • 2
  • 5
  • Saving is better than copying using sendkeys. Save it to temporary location and open it again. I would also suggest you to read about clipboard class. – Sangram Nandkhile Oct 15 '13 at 09:44

1 Answers1

4

Do you really need notepad?

Why don't you just save text file and open it? SendKeys is somewhat unpredictable...

Sub Macro2()
    Dim f As Integer
    'get a free file handle
    f = FreeFile
    'open test.txt in temp dir for writing
    Open Environ("TEMP") & "\test.txt" For Output As f
    'write text from cell A5
    Print #f, Range("A5").Text
    'close file handle
    Close #f
    'open file with notepad
    Shell "NOTEPAD.EXE " & Environ("TEMP") & "\test.txt"
End Sub
LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46