Page 1 of 1

Programming: Using Object variables (Updated)

Posted: Thu Oct 06, 2005 8:36 pm
by Onetoomanysodas
This tutorial will focus on targetting and contacting other applications to preform designated processes pre-defined by their own extentions.

Some of you may know the standard shell function:

Code: Select all

Shell ("C:\Games\Halo\Halo.exe")
This launches the specified path given and if you want to specify the open method of size and focus. But some of you may be wondering how to manipulate a program and it's actions. For this tutorial, I will not use Halo because it's not an application that is used as a tool such as... Photoshop. If you don't have Photoshop no worries, the same concept applies to all/most applications.

To start off we're going to create a new project. Go ahead and stretch the form out to a comfortable length. Now this is the important part. Go to Project>References... Scroll down to Adobe Photoshop 9.0 (or your version number) Object Library and select the checkbox then hit Ok. Now Ctrl+S and save your form as "Main.frm" or whatever name you prefer. Then go to File>Save Project and save your project as "Photoshop.vpb" or again, as whatever you want but remember, organization is essential.

Now gee Blake, what the hell did I just do? Adding a reference to your project will have visual studio add an autocomplete list after typing in strings that can be filled further. For example, when you type in "Command1.", it shows the possible completions. It will now do the same for the reference you added. References are a library type, such as ".dll, .olb, .tlb" that define an application's external/internal actions.

So know if you type in "Photoshop." then you should see everything possible with this application. Now, we're not going to use 'Photoshop.' as our code statements because when our program compiles with that, it will refer to the reference to locate the application, which could be different depending on where someone has installed it. So create a command button and enter the following code for it:

Code: Select all

On Error Resume Next
Set PhotApp = GetObject(, "Photoshop.Application")
Ok, the first statement is to bypass the errors you might get from Active X when performing this operation. This will just tell the program that if there is an error, continue executing code. The next statement is critical. This is what defines our target application as a variable. "Set PhotApp" - This is what the variable's name is going to be for our block of code. ' GetObject(,"Photoshop.Application") ' - Do not forget that comma, it tells visual basic to skip the first property which asks for the application's physical location. For me, that would be C:\Program Files\Adobe\Adobe Photoshop CS2\Photoshop.exe, but possibly different for other people. Photoshop.Application is the class ID of Photoshop and will only work if the application is currently running. This may differ depending on the application you are trying to access, Class ID's can differ from an application's name.

So now that "PhotApp" is set to the Class ID of Photoshop, you might be wondering why I made you set a reference earlier. Well that is for your own experimenting. For example, let's start off with this, type in "Photoshop." and view the list that appears, select "Application" and add another period for the next list to come up and this time select "Visible" then add a space and type in "= False". Your statement should look like this:

Code: Select all

Photoshop.Application.Visible = False
And your command block for your command button should look like this:

Code: Select all

Private Sub Command1_Click()
Set PhotApp = GetObject(, "Photoshop.Application")
Photoshop.Application.Visible = False
End Sub
Ok so now, since we want our program to work on other machines as well, we're going to change 'Photoshop' in the "Photoshop.Application.Visible" string to 'PhotApp' so the line should now read:

Code: Select all

PhotApp.Application.Visible = False
That means it'll read this statement from the class ID specified earlier when we defined 'PhotApp'.

Now go back to your visual form and add another command button, label them accordingly. This one is going to show/load the Application. By doing this, we will instead use the "CreateObject" statement. Since CreateObject will start a new instance of the program if it is not running, we must also add a statement. And by the way, there's no comma for the CreateObject statement because the ClassName is the first property. So you're whole code should look like this

Code: Select all

Private Sub Command1_Click()
On Error Resume Next
Set PhotApp = GetObject(, "Photoshop.Application")
Photoshop.Application.Visible = False
End Sub

Private Sub Command2_Click()
On Error Resume Next
Set PhotApp = CreateObject("Photoshop.Application")
Photoshop.Application.Visible = True
End Sub
If it does, then run the program (F5).
Click the show/load button, whichever you labeled accordingly. Photoshop should load. Go ahead and click the other command button. Boom! Photoshop is hidden, even from Window's taskbar. Now click the show/load button again, the application should reappear. Cool, eh?

The rest of things work the same as this and testing your own ways of doing things. For a good statement that will load a file in Photoshop, I wrote:

Code: Select all

Private Sub Openfile_Click()

On Error GoTo Error1

cmdlg.Flags = cdlOFNHideReadOnly
  With cmdlg
        .DialogTitle = "Open"
        .CancelError = False
        .Filter = "All Files (*.*)|*.*|"
                                          .ShowOpen
        If Len(.FileName) = 0 Then
            Exit Sub
        End If
    End With
     Load.Text = cmdlg.FileName

Photoshop.Application.Load (Load)

If Photoshop.ActiveDocument.FullName = Load Then
MsgBox "Sucessfully opened " + Load, , "Complete"
Exit Sub
End If

Error1:
MsgBox "Not a valid Photoshop format", vbCritical, "Error"
End Sub
If you would like me to expand or explain something in more detail please ask so, here is the very simple program I made for Photoshop:
Download from Navy Leaf

EDIT: P.S. Please give feedback too :D

Posted: Fri Oct 07, 2005 5:47 pm
by Pacifica
jawesome = jaw droppingly awesome

Posted: Sat Oct 08, 2005 10:19 pm
by Onetoomanysodas
*bump*

~prays for comments~

Posted: Sun Oct 09, 2005 9:47 am
by xxANTMANxx
umm

cool

Posted: Thu Oct 13, 2005 5:41 pm
by xxANTMANxx
is there any way u can upgrade this to vb.net?

that would be very usefull

Posted: Thu Oct 13, 2005 6:39 pm
by Onetoomanysodas
:D

We talked, but for other people, just add

Code: Select all

Dim PhotApp as Object
before the first line of code for each command and maybe some other little things, i'm not sure I actually didn't convert it but I think Antman might post that soon.