VBOffice

Open and Save Files

A sample for how to read and write the content of any file.

Last modified: 2006/01/20 | Accessed: 36.348  | #14
◀ Previous sample Next sample ▶
Reporter Reporter
VBOffice Reporter is an easy to use tool for data analysis and reporting in Outlook. A single click, for instance, allows you to see the number of hours planned for meetings the next month.

Many other samples use the Scripting Runtime for simple file operations. But it works also with much less overhead.


tip  How to add macros to Outlook
Public Function ReadFile(sPath As String) As String
  On Error GoTo AUSGANG
  Dim lFileNr As Long
  Dim sText As String

  lFileNr = FreeFile

  Open sPath For Binary As #lFileNr
  sText = Space$(LOF(lFileNr))
  Get #lFileNr, , sText

AUSGANG:
  If lFileNr Then Close #lFileNr
  If Err.Number Then Err.Raise Err.Number, Err.Source, Err.Description
  ReadFile = sText
End Function

Public Sub WriteFile(sPath As String, _
  sText As String, _
  Optional ByVal bAppend As Boolean _
)
  On Error GoTo AUSGANG
  Dim lFileNr As Long

  lFileNr = FreeFile

  Select Case bAppend
  Case False
    Open sPath For Output As #lFileNr
  Case Else
    Open sPath For Append As #lFileNr
  End Select
  Print #lFileNr, sText;

AUSGANG:
  If lFileNr Then Close #lFileNr
  If Err.Number Then Err.Raise Err.Number, Err.Source, Err.Description
End Sub
OLKeeper OLKeeper
OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or e-mails.
email  Send a message