I am trying to open a file with notepad.
const strfilename = \"C:\\Users\\Desktop\\abc.txt\"
set OFS = myOFSO.OpenTextFile(strfilename)
I
Could just call a shell command to execute notepad.exe with the file path.
returnvalue = Shell("notepad.exe " & strfilename, vbNormalFocus)
Try this approach
Sub Test()
Dim strCont As String
strCont = LoadFileStr$(ThisWorkbook.Path & "\Sample.txt")
Range("A1").Value = strCont
End Sub
Function LoadFileStr$(FN$)
With CreateObject("Scripting.FileSystemObject")
LoadFileStr = .OpenTextFile(FN, 1).readall
End With
End Function
If you want to create a new text-file and want to write into it, you could use this approach:
Sub writeToTextFile()
Dim i As Long
Open ThisWorkbook.Path & "\newFile.txt" For Output As #1
Print #1, "This could be the first line"
Print #1, "this the second"
For i = 3 To 10
Print #1, "this is the " & i & ". line"
Next
Close #1
End Sub
CreateObject("Shell.Application").Open("c:\Users\Desktop\abc.txt")
If you want to use this method with a filetype that isn't yet associated with an application:
default
Below code will help you to open notepad from excel.
Dim fso As Object
Dim sfile As String
Set fso = CreateObject("shell.application")
sfile = "C:\Users\Universal\Desktop\test.txt"
fso.Open (sfile)