vb6 opening a file in a certain program

Posted on 2004-11-10
Last Modified: 2010-04-17
I've just fineshed writing my imaging software, and wanted to open certain files in that program.

say for instance that I wanted to open a jpg by dragging that file over my program's icon. How?

I have changed the "open with" settings so that it opens my program but how do I get the picture in my program.

Question by:Rickardc
    LVL 85

    Accepted Solution

    The two processes you have described are completely different animals.  The first involves drag and drop while the second involves command line parameters.

    In the "open with" settings, you should have the "%1" parameter tacked onto the end of your program name as in the example below:
        "C:\Program Files\Common Files\Microsoft Shared\PhotoEd\PHOTOED.EXE" "%1"

    This will cause the file name being opened to be passed into your program as a command line parameter.  You can get to this information in your program using code like this:

        Private fileName As String

        Private Sub Form_Load()
            fileName = Command()
        End Sub

    As for the drag and drop part, you can use code like this to determine when a file has been dropped on your application.  The following will cause the dropped filenames to be displayed in Text1 if they are dropped on the Form or Text1:

    Option Explicit

    Private Sub Form_Load()
        Text1.OLEDropMode = 1 ' Manual
        Me.OLEDropMode = 1 ' Manual
    End Sub

    Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Data.GetFormat(vbCFFiles) Then
            Dim sText As String
            Dim i As Integer
            For i = 1 To Data.Files.Count
                sText = sText & Data.Files(i) & vbCrLf
            Text1.SelText = sText
        End If
    End Sub

    Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
        Call Form_OLEDragDrop(Data, Effect, Button, Shift, X, Y)
    End Sub

    LVL 23

    Expert Comment

    I dont know if this is possible but instead of the command line parameters , isnt there a way to see what file has been dragged over the application in question and then use the shell command to open it ? I am new to programming and it was just a thought I had when I was reading through this.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    The OLEDragDrop code is for determining what file(s) have been dropped onto your application.  By inspecting the extension of those files, you can decide if your application should attempt to process them or just ignore them.

    If you associate an extension with your application, double clicking a file with that extension in explorer will cause your app to be opened and the filename that was double clicked to be passed in as a command-line parameter.

    The Shell() command is used to execute an external EXE, COM or BAT file from within a VB application.  You would use it if you need to launch another app from yours.  It wouldn't be used to process a file associated with your app unless you needed to launch a new instance of your application for each file to be opened.

    LVL 1

    Author Comment

    Sorry Bout that!

    Thanx Idle mind!

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    haveThree challenge 22 91
    thread-safe code in c++ 2 59
    Replace a tag with sed 2 32
    array11 challenge 16 40
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now