Update text file with Excel

mjtwo3

New Member
Joined
Apr 4, 2012
Messages
4
Hello All,

I need to copy cells B2:C8, open an already saved text file (Test.txt), highlight everything in there, paste in the info from excel, and then save the text file.

Thanks in advance
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
What's the purpose of highlighting everything? Do you want to replace the exsiting contents?

Dom
 
Upvote 0
You will have to refer this thread for information on the object used:
DataObject

The code you will have to adjust to suit your text file path.
Code:
Public Sub WriteDataToTxtFile()
Dim objDaOb As Object
Dim rngCOPY As Range
Dim strPath As String

Set objDaOb = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set rngCOPY = Range("B2:C8")    'Set Range Reference
strPath = "C:\Test.txt"         'Set text file path

rngCOPY.Copy
objDaOb.GetFromClipboard
Open strPath For Output As #1
Print #1, objDaOb.GetText
Close #1
Application.CutCopyMode = False

End Sub
 
Upvote 0
Domski, yes i want to replace existing context.

taurean - that didn't seem to work. when i go to my txt file it is not updated with the same data that is in excel.
 
Upvote 0
taurean - that didn't seem to work. when i go to my txt file it is not updated with the same data that is in excel.
I had tested the code with false data and it did work so not sure what is different in your setup that fails it. Can you post the code as you have now?
 
Upvote 0

Forum statistics

Threads
1,219,161
Messages
6,146,657
Members
450,706
Latest member
LGVBPP

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top