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

postman25
postman25 used Ask the Experts™
on
' 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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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)
 
   
 

Author

Commented:
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?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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.
Commented:
You cannot display a Word document directly in a text control as it is not text.  Use an OLEContainer control instead.  If you just want the unformatted text of the main story, you could use wrdDoc.Content.Text

Author

Commented:
Sorry it took me so long to respond.  
Thanks for the help.

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