I need a RTF viewer.

I am looking for VB code that will allow me to view small RTF files created with MS Wordpad. This code only needs to open a specific hard coded file name and have one button to close the screen. It should be able to scroll and view embedded graphics.

How might this be accomplished in VB?  

Keeping in mind that I am new to VB and Windows programming but I am an experienced DOS based programmer.

I am guessing the tools are already built into VB by using the RichTextBox control but I still offer 400 pts for a complete solution, instructions suitable for a beginner that will help lead to a working program, or a web link to ready source code.

I will offer more points if this is not trivial, especially if RichTextBox does not support graphics. Perhaps this ocx might be required? http://vbaccelerator.com/codelib/richedit/richedit.htm

Thank you for your help,
Doug
LVL 4
S ConnellyTechnical WriterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
Actually, the richtext control allows graphics and scrolling.
Check out the LoadFile function from the control to load a file directly in 1 line of code...
codeconquerorCommented:
Add a reference to the "Microsoft Rich Textbox Control 6.0" to your project (usually it's C:\Windows\System32\RICHTX32.ocx).  Then do the following:

Add a RichTextbox to your form, then use the following code:

<CODE SNIPPET>
RichTextBox1.LoadFile "C:\Document.rtf"
</CODE SNIPPET>
codeconquerorCommented:
If you have a file that's larger than the display area, set the ScrollBars property to meet your needs.  :)  Hope that helps.  

No need for the vbAccelerator control, although most of the stuff at vbAccelerator is great, I love that site, one of my personal favorites.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

S ConnellyTechnical WriterAuthor Commented:
Thanks for the tip.  I created a simple program based on RichTextBox1.LoadFile "C:\Document.rtf", added a scroll bar... presto! It worked!
That was too easy.  

Okay, now as stupid as this may appear how do I create an END/EXIT button.

I drew a command button and labeled it. Now what do I put in the source code?  Yikes! I am not used to being a newbie.

Doug
S ConnellyTechnical WriterAuthor Commented:
I just knew this was too easy.

It seems that I am compiling the RTF text into the EXE. That is not what I want, rather... I need the program to load in the RTF file everytime it is executed.

Thank you for your help.
Doug
codeconquerorCommented:
To make an End/Exit button, draw a Command Button on your form, set the Name property of the button to cmdExit, set the caption property to read E&xit or some other text.  Then double click it, it will bring you to code that looks like this:

<CODE SNIPPET>
Private Sub cmdExit_Click()

End Sub
</CODE SNIPPET>

Make it look like this by adding the extra lines of code:

<CODE SNIPPET>
Private Sub cmdExit_Click()
Dim frmForm As Form '**** Declare a form object
If Forms.Count > 1 Then '**** If there is more than just this form loaded
    For Each frmForm In Forms '**** Cycle through each of the loaded forms
         If frmForm <> Me Then '**** If the form is not this form
                Unload frmForm '**** Unload it
                Set frmForm = Nothing '**** Make sure it's destroyed and it's resources freed
         End If
    Next
End If
Unload Me '**** Unload this form
End Sub
</CODE SNIPPET>
codeconquerorCommented:
<QUOTE>
It seems that I am compiling the RTF text into the EXE. That is not what I want, rather... I need the program to load in the RTF file everytime it is executed.
</QUOTE>

So you mean you need some code to open a browse dialog box and load a selected file?  Or you want the program to automatically load a specific file each time it is run?
codeconquerorCommented:
If you need to browse to a selectable file, you can use this (slightly modified from another post):

Add a Command Button to your form, set the name property to cmdBrowse, and the caption property to B&rowse.  Double click it and insert the code from below.

<CODE SNIPPET>
Private Sub cmdBrowse_Click()  '**** This line should already be there
   Dim objShell As Object
   Dim objFItem As Object
   Dim objFolder As Object
   Dim sDialogTitle As String
   Dim sInitDir As String
   sInitDir = "C:\" '**** Set the starting directory for the browse window
   sDialogTitle = "Select the RTF document you want to open" '***** Set the caption for the browse window
   Set objShell = CreateObject("Shell.Application") '**** Create the shell instance
   Set objFolder = objShell.BrowseForFolder(Me.hWnd, sDialogTitle, BIF_BROWSEINCLUDEFILES, sInitDIR) '**** Open the dialog window
   If Not objFolder Is Nothing Then '**** If a file was selected
        Set objFItem = objFolder.Items.Item '**** Get the file object
        RichTextBox1.LoadFile objFItem.Path '**** Load the file into the text box
   End If
   Set objFItem = Nothing '**** Destroy the file object and free the resources
   Set objFolder = Nothing '**** Destroy the folder object and free the resources
   Set objShell = Nothing '**** Destroy the shell application object and free the resources
 End Sub '**** This line should already be there
</CODE SNIPPET>
codeconquerorCommented:
If you mean that you have added the RTF document to your project, then just right click on it from the Project Explorer and click Remove Document.rtf where Document.rtf is the name of the file in the project.  If this is not what you mean, then please explain further.  :)
S ConnellyTechnical WriterAuthor Commented:
Hello codeconqueror:

Thank you so much for this valuable code. It isn't what I am looking for but it is useful to digest for later projects. Thank you!

>Or you want the program to automatically load a specific file each time it is run?

Yes, that is what I am looking for but I believe you have already told me how to do that.

You had earlier suggested, "RichTextBox1.LoadFile "C:\Document.rtf" but what I had done was create a rich text box and hardcode the FILENAME in the properties. In hind sight I see that is where I might have gone wrong. But I am unsure where to place, "RichTextBox1.LoadFile "C:\Document.rtf""

Obviously, I am missing certain fundamentals of VB programming.  I hope to remedy that this weekend with a beginners course on how things are done in VB. :-)  Once I learn more about how it's done in VB, I shall be in a better position to help myself.

Tell me, I am considering an enhancement to this viewer in order to allow animated gifs as well as RTF. Can you recommend an add-on library?
 
Thank you again,
Doug
jimbobmcgeeCommented:
If you want the same file to load each time it is run, then you can put in your Form code

    Private Sub Form_Load()
        RichTextBox1.LoadFile "x:\pathto\file.rtf"
    End Sub

If you want to load a file that is in the same folder as the application, try:

    Private Sub Form_Load()
        RichTextBox1.LoadFile Replace(App.Path & "\file.rtf", "\\", "\")
    End Sub

If you want to specify which file loads, in the command line, try:

    Private Sub Form_Load()
        RichTextBox1.LoadFile Command$
    End Sub

If you want a combination, try:

    Private Sub Form_Load()

        If Command$ <> "" Then
            RichTextBox1.LoadFile Command$
        Else
            RichTextBox1.LoadFile Replace(App.Path & "\file.rtf", "\\", "\")
        End If
     
    End Sub

HTH

J.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jimbobmcgeeCommented:
You may also want to check that a file exists before you try to load it (particularly with user input).  Try:

    Public Sub LoadRichTextFile(ByVal szFileName As String)

        Dim oFSO As Object
        Set oFSO = CreateObject("Scripting.FileSystemObject")

        If oFSO.FileExists(szFileName) = False Then
            MsgBox "The file could not be found.", vbExclamation, "Error"
        Else
            RichTextBox1.LoadFile szFileName
        End If

    End Sub

Now, instead of calling RichTextBox1.LoadFile directly, you call that routine instead:

    Private Sub Form_Load()

        If Command$ <> "" Then
            LoadRichTextFile Command$
        Else
            LoadRichTextFile Replace(App.Path & "\file.rtf", "\\", "\")
        End If
     
    End Sub

HTH

J.
codeconquerorCommented:
Good luck with your beginner's course.  VB is really easy once you get the basics down.  :)  I too used to be a DOS programmer (GWBasic, QuickBasic, and Turbo Pascal), VB was my first Windows language and was really easy to pick up.

jimbobmcgee is correct in his code.  Do you understand everything about what he is showing you?  If so, then I guess you're all set.  Happy coding.
S ConnellyTechnical WriterAuthor Commented:
Hey thanks folks!

I'll go through this later today.... but yes codeconqueror, I understand the code.  I am/was having trouble understanding where code needed to be placed and where forms fitted into the picture.

Final question, I am now considering using HTML instead of RTF because I would like to include the ability to view animated GIFs.

I believe VB has a webbrowser control... How might I impliment that feature? Or is their another method I should consider?
codeconquerorCommented:
You can easily use the webbrowser control, I'll work up some sample code and post it in a few mins.  :)
codeconquerorCommented:
Well, I got the webbrowser control to load the RTF file, but it asks each time if it's okay to open it, then once opened it's in Word mode so it doesn't support animated gifs.  You will need to switch from using an RTF document to using an HTM, HTML, ASP, or other such page that can be openeing in IE.

Then, add the component to your project called: Microsoft Internet Controls
Then draw the object on your form.  You can call it using: WebBrowser1.Navigate "C:\MyFile.html"
S ConnellyTechnical WriterAuthor Commented:
Thank you codeconqueror: I might just use HTML instead of RTF. Any known caveats using WebBrowser1.Navigate "C:\MyFile.html" instead of RichTextBox1.LoadFile "C:\Document.rtf"? Specifically, I am assuming it uses IE... Is it slower to load... I suppose I can find this out for myself.  Other than making a simple viewer.... the only criteria that I have is that it must be a small footprint... or at least load quickly.

Thank you again...
codeconquerorCommented:
No problem.  It should load quite quickly, it doesn't have a very big footprint.  :)
S ConnellyTechnical WriterAuthor Commented:
I cannot get over how easy that is! Virtually no lines of code to do this!

What is the best way to ensure that the VB runtime app is installed... before this problem runs?  It must be as simple as possible in fact, it is preferred if the end user doesn't even know that the runtime is being installed.
codeconquerorCommented:
If you use any normal installation creator like Wise it will install them with your application.  It would be pretty rare now adays to find a system without the vb runtimes installed though.

You can download the install for the runtimes here:

http://www.microsoft.com/downloads/thankyou.aspx?familyId=bf9a24f9-b5c5-48f4-8edd-cdf2d29a79d5&displayLang=en

Although, I'm not sure how to make it run silently other than to use an installation program.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.