Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open File With...

Status
Not open for further replies.

jbrimble

Programmer
May 13, 2002
10
AU
Hey guys! Just a quick question regarding opening files with your Application. Let's just say, for example, I was writing a version of Notepad. The file extension for my crude word processor's output would be *.JWP (Jason's Word Processor). How can i configure it so that all .JWP files on a users computer can be opened with my Word processor, and then the contents of the file to be placed into a text box for editing. If someone can help me with this i would be very appreciative.


Thanks :) Jason Brimblecombe

Webmaster of OCX Review
 
Thanks a lot for replying to my post, Jonathan. However, this has only answered half my question. I would like my program to detect the filename that is being opened and copy the contents of the file into a textbox for editing.

Can you help me out with this? Jason Brimblecombe

Webmaster of OCX Review
 
you could try something like this
note that a text box can only hold so many characters
you might want to consider using a Rich Text Box control instead

Code:
Option Explicit

Private Sub Form_Load()
  Dim sFile As String
  
  sFile = Command
  
  If sFile <> &quot;&quot; Then LoadFile sFile
End Sub

Private Sub LoadFile(sFile As String)
  Dim sCont As String
  Dim nFile As Integer
  
  sCont = Space$(FileLen(sFile))
  nFile = FreeFile
  Open sFile For Binary As nFile
  Get nFile, 1, sCont
  Close nFile
  
  Text1.Text = sCont
End Sub
 
What justin is saying is that your app can detect a file path using the code he has shown. You would copy the &quot;extention association&quot; code which you can find under the notepad &quot;txt&quot; or other extention. It then should be the same as if you were at a command line calling your app (just by double clicking the jwp file):

c>myapp.exe c:\temp\mydocument.jwp

I would advise first detecting if the file exisits to avoid a crash. Jonathan Galpin
 
Ken,

the RichTextBox control has a LoadFile Method

from MSDN:
LoadFile Method


Loads an .rtf file or text file into a RichTextBox control.

Syntax

object.LoadFile pathname, filetype

The LoadFile method syntax has these parts:

Part Description
object Required. Anobject expression that evaluates to an object in the Applies To list.
pathname Required. Astring expression defining the path and filename of the file to load into the control.
filetype Optional. An integer or constant that specifies the type of file loaded, as described in Settings.


Settings

The settings for filetype are:

Constant Value Description
rtfRTF 0 (Default) RTF. The file loaded must be a valid .rtf file.
rtfText 1 Text. The RichTextBox control loads any text file.


Remarks

When loading a file with the LoadFile method, the contents of the loaded file replaces the entire contents of the RichTextBox control. This will cause the values of the Text and RTFText properties to change.

You can also use the Input function in Visual Basic and the TextRTF and SelRTF properties of the RichTextBox control to read .rtf files. For example, you can load the contents of an .rtf file to the RichTextBox control as follows:

Open &quot;mytext.rtf&quot; For Input As 1

RichTextBox1.TextRTF = Strconv(InputB$(LOF(1), 1), vbUnicode)
 
Now i have tried but it still wont work.

My code.

Private Sub Form_Load()
Dim sfile As String
If sfile <> &quot;&quot; Then winload sfile
End Sub

Private Sub winload(sfile As String)
Open sfile For Input As 1
RichTextBox1.TextRTF = StrConv(InputB$(LOF(1), 1), vbUnicode)
End Sub.

What have I done wrong?
ken Christensen
Ken@Christensen.dk
Ken Christensen Software
 
this is confusing.

please make a code without any comments in it.
I have a RTF (Rich text format) control and it shall open the file when you dbl click at it in windows.

:) ken Christensen
Ken@Christensen.dk
Ken Christensen Software
 
Code:
Option Explicit

private const FILEFORMAT = 0 ' replace with 1 if you are opening a text file instead of an RTF file

Private Sub Form_Load()
  Dim sFile As String
  
  sFile = Command
  
  If sFile <> &quot;&quot; Then
    if dir(sfile) <> &quot;&quot; then
      RichTextBox1.LoadFile sFile, FILEFORMAT
    end if
  end if
End Sub
 
' try it without the 2nd argument

Option Explicit

Private Sub Form_Load()
Dim sFile As String

sFile = Command

If sFile <> &quot;&quot; Then
if dir(sfile) <> &quot;&quot; then
RichTextBox1.LoadFile sFile
end if
end if
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top