I need a bit of help with creating a batch file and pasteing a certain range from excel into it BUT keeping the text as it is displayed in excel.
Exemple: (this is the content of a cell I want to paste into a batch file)
"pushd N:\contracte\CONTRACTE NEVOI PERSONALE\Contracte nevoi personale 102501N - 105000N\
for /f ""delims="" %%a in ('dir /b /s ^| find ""104020""') do (
cd ..
xcopy ""%%a"" ""C:\Users\agrigoriu\Desktop\COPIERE\"" /E /D /Y )
@ECHO ---"
Problem 1: all the content will be displayed in one single line like this:
pushd N:\contracte\CONTRACTE NEVOI PERSONALE\Contracte nevoi personale 102501N – 105000N\ for /f “”delims=”” %%a in (‘dir /b /s ^| find “”104020″”‘) do ( cd .. xcopy “”%%a”” “”C:\Users\agrigoriu\Desktop\COPIERE\”” /E /D /Y ) @ECHO —“
Problem 2: it doubles every comma there is in the original text
this is the code I have so far and it only pastes the information from the range into a notepad (with the problems stated above):
Sub test()
'The range that contains the values
Range("D:D").Copy
'Start Notepad And let it recieve focus
Shell "notepad.exe", vbNormalFocus
'Send the keys CTRL+V To Notepad (i.e the window that has focus)
SendKeys "^V"
End Sub
Ideea: One “solution” woud be to paste it first into Word and then copy it again and paste it into notepad but this must work with all standard computers and I observed that not that many have the Word app on vba turned on by default.
Its better to write the text to a file then open that file in Notepad, that way you have control of formatting and you don’t trash whatever the user may have in their clipboard.
Dim hF As Integer: hF = FreeFile()
'// get temp file name
Dim path As String: path = Environ$("TEMP") & "\DUMP.TXT"
Open path For Output As #hF
Print #hF, Replace$(Range("D1").Text, vbLf, vbCrLf)
Close #hF
Shell "NOTEPAD.EXE " & path, vbNormalFocus
(This replaces \n new lines that exist within the cell with \r\n which is the cause of everything appearing on one line)
Answer:
Based on Alex K answer, this works with a range:
Sub test()
Dim hF As Integer: hF = FreeFile()
Dim output As String
'// get temp file name
Dim path As String: path = Environ$("TEMP") & "\DUMP.TXT"
Range("D2:D2000").Select
Open path For Output As #hF
For Each c In Selection
Print #hF, Replace(c.Value, vbLf, vbCrLf)
Next c
Close #hF
Shell "NOTEPAD.EXE " & path, vbNormalFocus
End Sub
Tags: excel-vbaexcel, vba