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
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
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
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:
Yuri
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