Link to home
Start Free TrialLog in
Avatar of postman25
postman25

asked on

VB6 - Trying to load a word doc that's selected from a file list box into a textbox without using a common dialog.

' I'm trying to load the file that is selected from the file list box into the textbox
' without using a common dialog.
' The error that I'm getting is "Input past end of file."
' I've tried - LoadTextBox = Input(LOF(#intFileNumber), #intFileNumber) but it VB freaks out.

Option Explicit
Private AppPath As String
Private SelectedFile As String
Private FileName As String
Private intFileNumber As Integer
Private LoadTextBox As String



Private Sub cmdCancel_Click()
    Unload frmReliusExplorer
End Sub

Private Sub Dir1_Change()
    Call LOADFILESINLISTBOX
    File1.Path = Dir1.Path
End Sub

Private Sub File1_Click()
   ' File1.Path = Dir1.Path
   ' FPath = File1.Path
   
   'MsgBox "Selected:" & File1.FileName
   FileName = File1.FileName
   'MsgBox "Selected:" & File1.ListIndex
   
 '  SelectedFile = File1.ListIndex
   SelectedFile = File1.Path + "\" + File1.FileName
  ' MsgBox SelectedFile
   
   Call LoadFile(SelectedFile, FileName)
 
   
End Sub

Private Sub Form_Load()
    AppPath = App.Path
   ' MsgBox "path" & AppPath
   
    Dir1.Path = AppPath
  '  File1.Path = Dir1.Path
    Call LOADFILESINLISTBOX
   
End Sub

Private Sub LOADFILESINLISTBOX()

Dim sFile As String
Dim sDirectory As String

List1.Clear

sDirectory = Dir1.Path & "\"
sFile = Dir(sDirectory, vbDirectory)

Do While sFile <> ""

'MsgBox sFile
    If sFile <> "." And sFile <> ".." Then
        If (GetAttr(sDirectory & sFile) And vbDirectory) <> vbDirectory Then
            List1.AddItem sFile
        End If
    End If
   
 '   MsgBox Dir
    sFile = Dir
   
Loop

End Sub

Private Sub List1_Click()
    'MsgBox "Selected:" & List1.Text
    MsgBox "Selected:" & List1.ListIndex
   
End Sub

Private Sub LoadFile(SelectedFile, FileName)

'    MsgBox SelectedFile
'    MsgBox FileName
   
    intFileNumber = FreeFile
    Open SelectedFile For Input As #intFileNumber
   
'    Text1.Text = Input(LOF(intFileNumber), intFileNumber)
    LoadTextBox = Input(LOF(intFileNumber), intFileNumber)
    Text1.Text = LoadTextBox  'This is where the error occurs "message: Input past end of file"
    frmReliusExplorer.Caption = "Maintain " + FileName
   

End Sub

Avatar of MichaelOS
MichaelOS

Hi,

What is LoadTextBox? Is this a function or subroutine? You say that "Text1.Text = LoadTextBox" works but LoadTextBox = Input(LOF(intFileNumber), intFileNumber) does not. I thing that the load file routine is being called twice and that the second call is failing because you're at the end of the file after the first read and probably haven't closed the file before the second read.

You could just say:
Text1.Text = = Input(LOF(intFileNumber), intFileNumber)
   
     
   

Sorry, meant:
Text1.Text = Input(LOF(intFileNumber), intFileNumber)
 
   
 

Avatar of postman25

ASKER

I changed the:
 Text1.Text = Input(LOF(intFileNumber), intFileNumber)inside the LOADTEXTFILE subroutine.  This loads a text file, but not a word document.  I tried to change the file extension:

SelectedFile = File1.Path + "\" + File1.FileName
SelectedFile = Replace(SelectedFile, ".doc", ".txt")

Then I saved it to disk b/c an error displayed "File cannot be found."

 Dim appWord As Word.Application
 Dim wrdDoc As Word.Document

 Set appWord = New Word.Application
 Set wrdDoc = appWord.Documents.Open(SelectedFile)

   SelectedFile = Replace(SelectedFile, ".doc", ".txt")
   wrdDoc.SaveAs SelectedFile
   wrdDoc.Close
   appWord.Quit
   
   Set appWord = Nothing
   Set wrdDoc = Nothing

 I tried to save the file after changing the extension but before opening it up:
Set wrdDoc = appWord.Documents.Save(SelectedFile)

An error display about expected function.

On my form I have a directory list and a file list box.  My goal is to allow the user to navigate through the directories and then the file list box will populate with the files inside the selected directory.  The user can select the word documents and have the textbox (or richtextbox or whatever) populate with that selected word document.  From there, the user can modify that file and save the changes.

My dilemma is getting the word document to load in the textbox.  I have been able to get a textbox to load.  I think the Word doc doesn't load b/c textbox or richtextbox doesn't have the ability of loading the larger file.  There is something between the word doc and the textbox that I'm missing. (some conversion?) Or I'm way off.....who knows.

Any ideas?
I'm basically trying to mimic Windows Explorer.  I just ran across a MiningTreeViewer control.  Will this control perform like the tree in windows explorer?  That would be great.
ASKER CERTIFIED SOLUTION
Avatar of StewartJ
StewartJ

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry it took me so long to respond.  
Thanks for the help.