Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

Write To File:

Public Sub WriteToTextFile(WriteToFilePath As String, strContent As String)
 Open WriteToFilePath For Output As #1
 Print #1, strContent
 Close #1
End Sub
Sub Test()

Dim strContent As String, WriteToFilePath As String
WriteToFilePath = "C:\tmp\helloworld.txt"
strContent = "Hello World"
 
Call WriteToTextFile(WriteToFilePath, strContent)

End Sub

Read From File:

Public Function ReadFromTextFile(ReadFromFilePath As String) As String
Dim text As String, textline As String
Open ReadFromFilePath For Input As #1
Do Until EOF(1)
    Line Input #1, textline
    text = text & textline
Loop
ReadFromTextFile = text
Close #1
End Function
Sub Test()

Dim FilePath As String
FilePath = "C:\tmp\helloworld.txt"
result = ReadFromTextFile(FilePath)
Debug.Print result

End Sub

Does Exist?

Public Function IsFile(ByVal fName As String) As Boolean
 On Error Resume Next
 IsFile = ((GetAttr(fName) And vbDirectory) <> vbDirectory)
End Function
Public Function FileExists(ByVal FileToTest As String) As Boolean
   FileExists = (Dir(FileToTest) <> "")
End Function
If FileExists(FileToDelete) Then

GetFile/Folder Path Using Windows UI:

Public Function GetFolder() As String
' https://stackoverflow.com/a/26392703/4650297
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    GetFolder = sItem
    Set fldr = Nothing
End Function
Public Function GetFile() As String
' https://stackoverflow.com/a/26392703/4650297
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFilePicker)
    With fldr
        .Title = "Select a File"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    GetFile = sItem
    Set fldr = Nothing
End Function

  • No labels