6 Replies Latest reply on Jan 27, 2012 4:11 PM by al_layson

    Opening last file


      I am trying to create a little script that will open the last file that was open. When Expedition is open I can use Alt+F and then 1 to open tha last file. But, how do I use that to put it in a script.


      I found the OpenDocument Method, but it hardcoded to the file name.


      thank you.

        • 1. Re: Opening last file

          Your best bet is to write the Document.FullName property to a text file when the Document.OnClosing event fires.  Then, when the script runs on startup, read the path from the text file and use the OpenDocument method.

          1 of 1 people found this helpful
          • 2. Re: Opening last file

            This is what I found and then wrote. But nothing happens. I had created another piece that when i click a button, it would write to the test file. I did that to make sure my code for writing to a file worked correct. However, when I added the, "Sub Document_OnClosing()" section, nothing happens. I can't seem to find much on this. Any help is appreciated.


            Sub Document_OnClosing()

            Dim objFSO, objTextFile, objFile

            Dim strDirectory, strFile, pcbfilename

            strFile = "c:\temp\test.txt"

            pcbfilename = ActiveDocument.FullName


            ' Create the File System Object

            Set objFSO = CreateObject("Scripting.FileSystemObject")


            ' OpenTextFile Method needs a Const value ForAppending = 8 ForReading = 1, ForWriting = 2

            Const ForAppending = 2


            Set objTextFile = objFSO.OpenTextFile (strFile, ForAppending, True)


            ' Writes pcbfilename every time you run this



            End Sub



            Thank you,


            • 3. Re: Opening last file

              I should add that I also found the perfect way to open the last pcb file. This is to use a function called "sendkeys". I can code what keys to push for whatever command. For instance, you can code in the alt+F to bring up the File pulldown and then code in 1, to open the last file. However Mentor scripting doesn't allow it even though it is a vbscript function.

              • 4. Re: Opening last file

                You're on the right track, but your event setup needs some tweaking.  Do something like this:


                Dim pcbApp

                Set pcbApp = Application

                Dim pcbDoc

                Set pcbDoc = pcbApp.ActiveDocument

                If (ValidateServer(pcbDoc)) Then

                    Call Scripting.AttachEvents(pcbDoc, "pcbDoc")

                    Scripting.DontExit = True


                    MsgBox "Error: Licensing failed"

                End If

                Function pcbDoc_OnClosing()

                    ''' Save to file here '''

                End Function

                Function ValidateServer(doc)

                    Dim keyInt

                    Dim licenseTokenInt

                    Dim licenseServer


                   'Ask Expedition's document for the key

                   keyInt = doc.Validate (0)

                   'Get license server

                   Set licenseServer = CreateObject ("MGCPCBAutomationLicensing.Application")

                   'Ask the license server for the license token

                   licenseTokenInt = licenseServer.GetToken (keyInt)

                   'Release license server

                   Set licenseServer = nothing

                   'Turn off error messages (validate may fail if the token is incorrect)

                   On Error Resume Next


                   'Ask the document to validate the license token


                   If Err Then

                      ValidateServer = 0


                      ValidateServer = 1

                   End If

                End Function

                • 5. Re: Opening last file

                  Yeah, I tried this approach (Application.Gui.ProcessKeyPressEvent) prior to posting my first answer.  I couldn't get it to work.  You need to use a tool like AutoIt/AutoHotKey that will send keys to any application.

                  • 6. Re: Opening last file

                    When Expedition Application is running, do this