Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste into notepad cell content from excel?

Tags:

excel

vba

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.

like image 654
MisterA Avatar asked Jun 22 '26 23:06

MisterA


1 Answers

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)

like image 51
Alex K. Avatar answered Jun 28 '26 08:06

Alex K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!