Associated File

bshoenhair used Ask the Experts™
I have a program with a RichTextBox. I would like to know how to load the text of an associated file into that RichTextBox when a user opens the associated file.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

How are you getting the filename from the user?  Common Dialog Control?  Try this:


if CommonDialog1.Filename <> vbNullString then
  open CommonDialog1.Filename for binary as #1
  RichText1.Text = input$(lof(1), #1)
  close #1
end if


My program saves a file from the RichTextBox.text using the savedialog in a directory choosen by the user. I can open the file from my program with no problem using the opendialog.

When a user double clicks on the associated file it opens my program but nothing appears in the RichTextBox. How can the file be displayed automatically in the RichTextBox when the user only double clicks on the associated file. Example: when you double click on an Excel Worksheet associated file it opens Excel with all the save data.
Assuming that Windows already has the association set up, add this to your start-up form:

Private Sub Form_Load()
  On Error GoTo Form_Err
  Dim strFilename As String
  Dim iFileNumber As Integer
  strFilename = Replace$(Command$, Chr$(34), vbNullString)
  If strFilename <> vbNullString Then
    If Dir$(strFilename) <> vbNullString Then
      iFileNumber = FreeFile()
      Open strFilename For Input As #iFileNumber
      RichText1.Text = Input$(LOF(iFileNumber), #iFileNumber)
      Close #iFileNumber
    End If
  End If
  Exit Sub
  MsgBox "Error " & Err.Number & Err.Description & vbNewLine & strFilename
End Sub


Works perfectly

Thanks for the help

Merry Christmas

You're welcome.  Thanks for the A-grade, and a Happy New Year, too!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial