Solved

VBA copy from Excel to Notepad

Posted on 2013-05-21
9
5,434 Views
Last Modified: 2013-05-23
Need some code to copy a range of cells from Excel,open up notepad and paste into it.
Anyway to have notepad save to a given csv file and save into a given folder using VBA?
0
Comment
Question by:cebu1014
9 Comments
 
LVL 39

Expert Comment

by:nutsch
ID: 39184760
It's all possible, but it's not necessarily clean. Why don't you save the csv directly without going through notepad?

Thomas
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39184909
Yes, I would suggest using VBA to simply create a new worksheet, paste the desired data into that sheet, then Save-As the sheet using CSV into the desired folder.
0
 

Author Comment

by:cebu1014
ID: 39185044
I would like to at least no how to open up notepad and to paste into it using VBA just to have the know how. There is some issues I am having otherwise when using double quotes in fields .
0
 
LVL 80

Expert Comment

by:byundt
ID: 39185078
If you want to learn how to open Notepad from Excel, paste data and save the resulting file, take a look at the code in this Mr. Excel thread: http://www.mrexcel.com/forum/excel-questions/46163-open-notepad-form-visual-basic-applications.html
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 22

Expert Comment

by:rspahitz
ID: 39185139
A simple way to get started is this, but it won't save the file:

Sub CopyToNotepad()
    Shell "Notepad", vbNormalFocus
    AppActivate "Untitled - Notepad"
    SendKeys "123"
End Sub

Open in new window

0
 
LVL 22

Accepted Solution

by:
rspahitz earned 500 total points
ID: 39185189
Or another way that will create the file then open it, insert data, then save:
Sub CopyToNotepad()
    Dim iFileNumber As Integer
    
    Selection.Copy' copy the current Excel selection

    iFileNumber = FreeFile()' find the next free file identifier

   ' create the file
    Open "C:\Users\{username}\Documents\abc.txt" For Output As #iFileNumber
    Close #iFileNumber

    ' launch the file in notepad
    Shell "Notepad C:\Users\{username}\Documents\abc.txt", vbNormalFocus

   ' activate the window so we can talk to it
    AppActivate "abc.txt"

   ' send a Ctrl+V (paste) and Ctrl+S (save) to notepad...could also send an Alt-FX to exit if desired
    SendKeys "^v^s"
End Sub

Open in new window


Note that SendKeys is considered occasionally flaky and not always reliable.
0
 

Author Comment

by:cebu1014
ID: 39189909
I got it to work using your above routine as a guide. I removed some of the lines though and added  TRUE at end of sendkeys command in order to get it to work.

Sub CopyToNotepad()
   
   
     Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy

 
    Shell "Notepad C:\Users\mw1\my Documents\abc.csv", vbNormalFocus


   
    SendKeys "^a", True
    SendKeys "{ENTER}", True
SendKeys ("^{HOME}"), True
SendKeys "^v", True
End Sub
0
 

Author Closing Comment

by:cebu1014
ID: 39189912
Good Job
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 39191225
Glad it got you in the right direction.  Seems that you learned a few new things along the way too.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

760 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

21 Experts available now in Live!

Get 1:1 Help Now