Excel macro to open Notepad and save with Date/Time in filename

W

Willy

I've tried to create this macro using the Shell command which works for
opening Notepad but I can't get the part about putting the date and time into
the filename of the Notepad file. The date and time format could have blanks
between such as mm dd yyyy hh mm ss but if it has underscore or other
characters that is ok.
 
R

RB Smissaert

This is a different approach and maybe simpler:

Option Explicit

Sub StringToTextFile(ByVal txtFile As String, _
ByVal strString As String)

Dim hFile As Long

hFile = FreeFile

'Close before reopening in another mode.
'---------------------------------------
On Error Resume Next
Open txtFile For Input As #hFile
Close #hFile

Open txtFile For Output As #hFile
'this will make start- and end quotes
'Write #hFile, strString
Print #hFile, strString;
Close #hFile

End Sub


Sub test()

Dim strFolder As String
Dim strFileName As String
Dim strExtension As String

strFolder = "C:\"
strFileName = "File " & _
Format(Date, "dd_mmm_yyyy")
strExtension = ".txt"

StringToTextFile _
strFolder & strFileName & strExtension, _
"just testing"

End Sub


RBS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top