hexagon logo

Scripting Help for auto Save-as

Trying to get going on some scripting and automation. Very new to this so any and all help most appreciated. The following code gives this error :

---------------------------
PC-DMIS Basic Scripting Engine
---------------------------
Error on line: 9 - OLE Automation object does not have a default value
---------------------------
OK
---------------------------

Can someone help me get this simple script running?

Sub Main ()
Dim App As Object
Dim Part As Object
Set App = CreateObject ("PCDLRN.Application")
Set Part = App.ActivePartProgram
Dim Serial As Object
Set Serial = Part.GetVariableValue("V1")
Dim NewName$
NewName = Part.Path & Part.PartName & "_" & Serial & ".PRG"
retval = Part.SaveAs(NewName)
' Cleanup
Set Part = Nothing
Set App = Nothing
End Sub


I believe this code was originally posted in a different (and probably much better) form by VPT.se. I'm trying to learn how to do this sort of thing by modifying it.

Thanks for any help.
  • I'm going to answer my own question now that I've done some more work on this:

    This problem described above is because the reference to "Serial" in line 9 does not explicitly define the type of value to use from "Serial". It seems that the value type for a referenced object like that needs to be expicitly defined. The allowed types are .type, .commandvalue, .doublevalue, .stringvalue, .longvalue, .pointvalue. Each of these will yield a different value and will be appropriate in different circumstances. The above code should work if I substitute "Serial.StringValue" in place of "Serial" in line 9. I haven't actually tried it because I ended up writing some different code combining some other ideas found on this forum.

    Here is what I ended up with. This seems to work very well for my needs and hopefully will be helpful to someone else as well:

    Sub Main
    
    ' This Script will Append the serial number entered In the C1 Input comment In the PC-DMIS program
    ' To the part program Name And save the file into a folder named "RESULTS" that resides
    ' In the same directory As the part program. It will Then resave As the original part program Name In
    ' the original directory, ready To be executed again.
    '---------------------------------------------------------------
    ' Create a Folder In the same directory As the Part Program called "RESULTS"
    
    ' Insert this code at beginning of PC-DMIS program:
    
    ' C1 =COMMENT/Input, NO, FULL SCREEN=NO,
    '        Enter Part Serial Number
            
    '        ASSIGN/SER_NUM=(C1.Input)
    '-------------------------------------------------------------
    
    
    Dim App As Object
    Dim Part As Object
    Dim Serial As Object
    Dim strCrntName As String
    Dim strPath As String
    Dim strPrgName As String
    Dim strNewName As String
    Dim strNoExt As String
    Dim bolPassFail As Boolean
    Dim FindDot
    
    Set App = CreateObject ("PCDLRN.Application")
    Set Part = App.ActivePartProgram
        Set Serial = Part.GetVariableValue ("SER_NUM")
            strCrntName = Part.FullName
            strPath = Part.Path
            strPrgName = Part.Name
                FindDot = InStr(1, strPrgName, ".")
            strNoExt = Left(strPrgName, FindDot - 1)
            strNewName = strPath & "RESULTS\" & strNoExt & "_" & Serial.StringValue & ".prg"
    
    bolPassFail = Part.SaveAs (strNewName)
    bolPassFail = Part.SaveAs (strCrntName)
    
    End Sub


    The code here is an adaptation of code written and posted to this forum by vpt.se and Craiger_NY.
  • DaSalo, you've written about the problem, and you also provided the solution. The only thing you didn't do was to thank you(rself) for it, so I will.

    Thank you for providing something that I am sure others would find useful.
  • There is one minor annoyance with this script that seems to be caused by the new auto-backup feature introduced in 2011 MR1. I get the following error after the script executes:

    Everything still works as intended, you just need to click OK on this error dialogue box. Anyone have a simple solution for making that go away? I would prefer not to turn the function off as I do find it useful most of the time.

    Also, I don't full understand why the .saveas method requires the "Boolean =" syntax. It works this way, and doesn't work without it, so obviously it is supposed to be like that but I don't quite get the logic behind it. I couldn't find any documentation of this method in PC-DMIS. Does anyone have any deeper information about how this works and any pointers on where to look stuff like this up in the future?

    Finally, what is the easiest way to make a script like this into an executable? I assume I need to write it in a different environment that is intended for that purpose. Any recommendations on something that is both simple and, ideally, free? Can the visual basic editor in the Excel or Word compile as an .exe?
  • DaSalo, you've written about the problem, and you also provided the solution. The only thing you didn't do was to thank you(rself) for it, so I will.

    Thank you for providing something that I am sure others would find useful.


    vpt.se and craiger_ny - Thanks guys for providing code so others can learn and evolve.

  • Finally, what is the easiest way to make a script like this into an executable? I assume I need to write it in a different environment that is intended for that purpose. Any recommendations on something that is both simple and, ideally, free? Can the visual basic editor in the Excel or Word compile as an .exe?


    no, the built in VB(A) with Word and Excel cannot make an executable file, at least not as far as I know.
  • The basic code in pcdmis is not the same as visual basic.
    You can use the basic editor in pcdmis to compile your script but not make an .exe.
  • The basic code in pcdmis is not the same as visual basic.
    You can use the basic editor in pcdmis to compile your script but not make an .exe.


    So it sounds like I would need to either find either a freeware, purpose built basic compiler or, more likely, find some commercial software to do this. The reason I ask about this is that one of our online seats is a "PRO" seat and I don't believe you can execute basic scripts with a "PRO" seat. The documentation says you need at least "CAD". I am pretty sure I can execute an external function with the "PRO" seat so I could still use something like this on that CMM if it was in the form of an .exe that I could call externally.
  • I have done this many times with Visual Basic 6 (not VBA, which is what comes with Excel, Word). You can make executables and call them from PC-DMIS, but its not free. I am not myself familiar with any freeware that will let you do this.
  • Hi,

    Yeah you'd need VB6 - don't know if you can get it any more.

    The other option would be to turn to .net, you can get the express versions free, but it's a bit more tricky to get your head round -if you do venture down this road there's not a good package & deployment solution in there (as there was with vb6), instead get "Inno Setup5" which is both free and excellent.


    In answer to your question about "Boolean =" I think you can instead of

    retval = part.saveas(path)

    simply say..

    part.saveas path

    Note you drop the parenthesis()
    ..or at least in in VB6 you can.
  • Just one final note in case anyone references this in the future: You can not add a basic script to a program or edit an existing basic script using a "Pro" seat but you can execute a program that already contains a basic script reference and it will execute just fine. So as long as you have something better than "Pro" to do your programming with you can execute your program in-full on a "Pro" seat.