Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,542 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,778 people online right now. Registration is fast and FREE... Join Now!




Automating Photoshop with VB2008

 
Reply to this topicStart new topic

Automating Photoshop with VB2008, Code that worked in VBA doesn't in VB2008

PeteyB
17 Aug, 2008 - 04:53 PM
Post #1

New D.I.C Head
*

Joined: 17 Aug, 2008
Posts: 3

I built a class module in Access 2007 using VBA for automating Photoshop. It worked so well that I decided to port it to a standalone application, and installed Visual Basic Express 2008. Come to find out VB2008 is not just a simple upgrade from from VB6 or VBA, they changed a lot with the new .NET framework. But I'm getting most of it to work, unfortunately the one procedure I use most:
CODE

Public Sub SelectArea(l As Integer, t As Integer, w As Integer, h As Integer)
Dim p(1 To 5) As Variant
    p(1) = Array(l, t)
    p(2) = Array(w + l, t)
    p(3) = Array(w + l, t + h)
    p(4) = Array(l, t + h)
    p(5) = Array(l, t)
    ps.ActiveDocument.Selection.Select p
End Sub

Didn't port over easily. The Photoshop dox are skimpy, but thie Select method expects an array of arrays and this code worked perfectly. The VB2008 code I wrote appears to be functionally identical:
CODE

    Public Sub SelectArea(ByVal l As Integer, ByVal t As Integer, ByVal w As Integer, ByVal h As Integer)
        Dim p1() = {2, 2}
        Dim p2() = {12, 2}
        Dim p3() = {12, 12}
        Dim p4() = {2, 12}
        Dim p5() = {2, 2}
        Dim p()() = {p1, p2, p3, p4, p5}
        PSapp.ActiveDocument.Selection.Select(p)
    End Sub

The values were plugged in to make it easier to debug. When I call this I get a
CODE

          [b]System.Runtime.InteropServices.SafeArrayTypeMismatchException[/b]

I've tried several variations on this theme, specifying other types, most of which won't even compile. I've tried to shut off the exception handling for this error, and handling the error myself but with no luck. I'm desperate at this point, only because this little app cuts my image processing time in half. I could just run it from Access, but that's a lot of baggage.
The Photoshop reference just lists the param type as "Array (Points:Array (Array(x,y),...)" which is intended for VB6 or earlier.
At this point I'll try just about anything.
User is offlineProfile CardPM
+Quote Post

PeteyB
RE: Automating Photoshop With VB2008
17 Aug, 2008 - 06:46 PM
Post #2

New D.I.C Head
*

Joined: 17 Aug, 2008
Posts: 3

This is my 1st time here, I usually turn to the experts exchange but they seldom help, and once again I've answered my own question... HERE'S WHAT WORKS:
CODE

        Dim p1() = {2, 2}
        Dim p2() = {12, 2}
        Dim p3() = {12, 12}
        Dim p4() = {2, 12}
        Dim p5() = {2, 2}
        Dim p() As Object = {p1, p2, p3, p4, p5}

The Select method accepted it with no problems, the immediate window was quieter than usual, (though I still got one SafeArrayTypeMismatchException and two COMexceptions from System.Runtime.InteropServices ) and most importantly It Worked. Enabling/Disabling the exception handler from Exceptions in the Debug menu had no effect (does this even do anything?). Now as far as WHY it worked, I have no clue. My guess would be ...
In the original VBA code the elements of the p() array were variants that just happened to be arrays which is mirrored with p() As Object. Using p()() defined the elements as arrays first and variants/objects second which isn't what the COM object wanted. I'd love to cut & paste the page and a half of variations I tried before I stumbled on this little bit of accidental brilliance, but if this site posted all the code that didn't work...
I wonder what the difference is internally between the two data structs? Ah who cares, Photoshop is now fully enslaved and all is right with the world
blink.gif
That's it for now, but I'm sure I'll be back.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:33PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month