Working with variables

There's something about the macro language I don't understand. I want make a macro that saves the results in a txt file with the same name as the image. The start of my macro looks like this:

    Public Function FD008() As SimpleScript
        FD008 = New SimpleScript
        Dim doc1, image1, doc2
        Dim docList1 = New List(1), doc3, image2
        Dim myFile As String
        Dim StubNo As Integer
        Dim ImageNo As Integer

        With Application.RibbonCommands.SelectRibbonTab(FD008)
            .TabName = "Measure"
            .Run()
        End With

        Measure.Data.CollectorCommands.Clear(FD008).Run()

        With Application.RibbonCommands.SelectRibbonTab(FD008)
            .TabName = "CountSize"
            .Run()
        End With

        With Application.DocumentCommands.Active(FD008)
            .Run(doc1)
        End With

        With Measure.MeasurementsCommands.Options(FD008)
            .ActiveClass = 1
            .Run(doc1)
        End With

        myFile =  doc1.Name


why doesn't that last bit work? I want to use the filename for savings later on:


        With Measure.Data.Collector.TableCommands.SaveAsText(FD008)
            .FileName = "C:\Users\torbe\Dropbox\Torben Dokumenter\Mandrup Software\Kunder\RW\Ny SEM\Pyhton Code\" & Left(myFile,Len(myFile)-4) & ".txt"
            .UseStatistics = False
            .Run()
        End With

Please help me:-))

 - Torben

Answers

  • 2016-11-23-130013

    Torben --

    I have never tried to use

        DOC1.NAME

    in the way that you have.

    The way that I accomplish what you are looking for uses

        ThisApplication.ActiveDocument.FileName

    Please see the CODE and SCREEN CAPTURE below.

    I hope this information is helpful.

    -- Matt

    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

    Public Sub DEMO123()
    
        'Show the OUTPUT WINDOW
        ThisApplication.Output.show
    
        ThisApplication.Output.PrintMessage("The DISPLAYNAME of the current image is" & ThisApplication.ActiveDocument.DisplayName)
    
        ThisApplication.Output.PrintMessage("The FILENAME of the current image is" & ThisApplication.ActiveDocument.FileName)
    
    End Sub
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-



  • Torben,

    Your macro has "SimpleScript" type, which requires special handling, this type of macros are executed on Load and when they Run, which is different from normal Sub, which Matt used as example. On Load doc1 is Nothing, which will throw an error.

    If you want to use your own code in SimpleScript you have to put it inside CodeCommand (that can be dropped to the macro from the toolbox in designer view. Your code command should look like this:

            'my variable
            Dim myFile As String
            With Automate.ScriptingCommands.CodeCommand(FD008)
                If .Run() Then
                    ' User Code Here
                    myFile =  doc1.Name
                    'or with full file path
                    myFile=doc1.FileName
                    'check it
                    Debug.Print myFile
                End If
            End With
    

    Yuri 
  • Thanks Yuri - But then I guess this one should also be wrapped somehow:

            With Measure.Data.Collector.TableCommands.SaveAsText(FD007)
                .FileName = "C:\Users\torbe\Dropbox\Torben Dokumenter\Mandrup Software\Kunder\RW\Ny SEM\Pyhton Code\" & Left(myFile,Len(myFile)-4) & ".txt"
                .UseStatistics = False
                .Run()
            End With
    
  • Torben --

    Try modifying your code as shown below.

    But . . .

    A "feature" of WINDOWS is that the FILE EXTENSION will not be shown in the IMAGE TITLE BAR or in the DISPLAY NAME unless the OPTION of

        WINDOWS + EXPLORER + TOOLS + FOLDER OPTIONS + VIEW +
        HIDE EXTENSIONS FOR KNOWN FILE TYPES
        (see screen capture below)

    is turned OFF.

    If you want the TXT FILE to go into the same FOLDER as the IMAGE FILE, then you can use

        .FILENAME

    and replace the EXTENSION as you have done in your code.

    Also . . .

    It is just a "bit" dangerous to assume that your FILE EXTENSIONS will always have 3 CHARACTERS.  Your program will be more robust if it does a reverse search for the last "." and then replaces all of the characters after the "." with "txt"

    I hope this information is helpful.

    -- Matt

            MyFile = ThisApplication.ActiveDocument.DisplayName

    With Measure.Data.Collector.TableCommands.SaveAsText(FD007) .FileName = "C:\Users\torbe\Dropbox\Torben Dokumenter\Mandrup Software\Kunder\RW\Ny SEM\Pyhton Code\" & Left(myFile,Len(myFile)-4) & ".txt" .UseStatistics = False .Run() End With




  • edited November 2016
    Torben,
    The command TableCommands.SaveAsText doesn't have to be wrapped to CodeCommand as it doesn't use any objects that can be undefined during Load stage.

    Regarding creation of new name: it depends what you put into myFile, in case of doc1.FileName - it will be full path, in case of doc1.Name - it will be just name. You can simply add ".txt" extension to it (without even trying to remove old extension), like this:
     .FileName = "C:\Users\torbe\Dropbox\Torben Dokumenter\Mandrup Software\Kunder\RW\Ny SEM\Pyhton Code\" & myFile & ".txt"
    Yuri
Sign In or Register to comment.