Unable to run a power point macro from perl - perl

I have a macro written in powerpoint and i need to call it from my perl script, it is possible to call a macro from Excel using $Excel->Run("MYMACRONAMEHERE"); but using "Run" with powerpoint is giving the below error:
OLE exception from "Microsoft PowerPoint 2010":
Application.Run : Invalid request. Sub or function not defined.
Win32::OLE(0.1709) error 0x80048240 in METHOD/PROPERTYGET "Run"
below is the perl i am usign to call the macro from powerpoint:
my $filename = "<path>";
my $PptApp = Win32::OLE->GetActiveObject('PowerPoint.Application')|| Win32::OLE->new('PowerPoint.Application', 'Quit');
$PptApp->{Visible} = 1;
my $Presentation = $PptApp->Presentations->Open({FileName=>"$filename",ReadOnly=>0});
$PptApp->Run("macro_name");

You're using the power point Application object to run an Excel macro. That's not going to work. You'll need to get an instance of Excel and use its Application object to run the Excel macro. Alternatively, you could copy the VBA code into a PowerPoint module.

The macro must be defined and, I'm pretty sure, must be declared as Public.
And, from MSDN (http://msdn.microsoft.com/en-us/library/office/ff744221(v=office.15).aspx):
The name of the procedure to be run. The string can contain the following: a loaded presentation or add-in file name followed by an exclamation point (!), a valid module name followed by a period (.), and the procedure name. For example, the following is a valid MacroName value: "MyPres.ppt!Module1.Test."
However, if the procedure is declared Public and is part of a loaded add-in, you should only need to supply the procedure name.

Related

How to pass arguments from UFT to command line

I am trying to run tests in UFT by running a .vbs file. I am also passing arguments through command line. .vbs file reads the arguments and sets the environment variable of UFT. Hence, I can read them inside UFT.
qtApp.Test.Environment.Value("First_Argument") = WScript.Arguments.Item(0)
qtApp.Test.Environment.Value("Second_Argument") = WScript.Arguments.Item(1)
After that, I want to get a number as an output from UFT because I will use that output to pass it to the next command in command line.
The Test Parameters Object can be a way , more detailed in the Automation Object Documentation
You will have to define the TestParameters of the TestCase from the UFT IDE(manually) there is no way to define them automatically. If you declare them as in and out type, and change their value as a part of a Test Case, you would be able to read it afterwards from the vbs (Do not open a new Test Case until you did not read out the preferred values)
Although this is a working (and standard) way for exchanging parameters between the driver script and the TA Robot(UFT) I would advise you to use a simple file based way of doing this - managing test parameters can be very time consuming.
Tell the script via an Environment variable the path of the xml / json or simple text file where you expect the results to be written and when the test is done, read the content of the file (assuming the test will write into that file)
The plain old file way should not be underestimated especially in such circumstances.

How to read a file during simulation in Specman

I need to read a file with input parameters for my test. However I dont want to hardcode the name of the file into the code.
How can I specify the name of the file from the command line for compiled e code?
Is there another way to do it for loaded e code? Why wont this work for compiled code?
The generic solution would be to use the new sn_plus mechanism.
From command line add something like +my_file=filename
From your code you can access the argument with special functions sn_plusargs_exist to check if there is such argument, and read its value with sn_plus_value.
Another solution is passing filename as define from the command line, with -c flag, and inside your code read the file named with that define.
However, it doesn't work with compiled e code, since the defines are already calculated at compiling time.
You can use the sn_plusargs_value() and sn_plusargs_exist() in your code.
Now you can pass your arguments file via command line with no need to re-compile your e code.
Alternatively you can set an environment variable and retrieve its value in the e-code using
var filename := get_symbol("<VAR>")

Is there a way to attach a file (.txt) in calc open office programmatically (using macro)?

I have a .txt file that I need to attach in a column of my sheet, and i have the path to this file.
So I need to read this path and attach the file in another column programmatically. Is there a way to do it?
thanks in advance.
Indeed there is! And using by using macros it is quite easy to do.
Enabling macros
Go to the Tools > Options menu and click on the Security section under OpenOffice.org. Once there, click the Macro Security button. Now on the Security Level Tab, make sure that your settings will allow you to run Macros.
My settings are on low because I'm the author of all the macros I run, if you are not sure that this will be your case you might want to use a higher setting.
Note: Be careful, if you are unlucky or live in the 90's an evil macro can cause serious damage!
Creating a new macro
Now that you can run them, you must create a new macro. OpenOffice accepts a wide range of languages including Python, but since you didn't specified any I'll use OO's version of basic here.
Go to Tools > Macros > Organize Macros > OpenOffice.org Basic, and once there add a new module under your file's tree. Give it a meaningful name.
The actual macro
Once you create a new module the editor screen will pop up, write this code below:
Sub DataFromFile
Dim FileNo As Integer
Dim CurrentLine As String
Dim File As String
Dim Msg as String
Dim I as Integer
' Get the filename from the cell, in this case B1.
currentSheet=ThisComponent.CurrentController.ActiveSheet
fileName = currentSheet.getCellRangeByName("B1").getString
' Create a new file handler and open it for reading
FileNo = FreeFile
Open fileName For Input As #FileNo
I = 0
' Read file until EOF is reached
Do While not eof(FileNo)
' Read line
Line Input #FileNo, CurrentLine
' Define the range to put the data in as A4:A999 '
curentCell = currentSheet.getCellRangeByName("A4:A999").getCellByPosition(0,I)
' Select the I-th cell on the defined range and put a line of the file there
curentCell.String = CurrentLine
'Increase I by one
I = I + 1
Loop
Close #FileNo
End Sub
To test it, just create a text file and put something in it, then put the path to it on cell B1 and run the macro. You can run the macro in many ways, for test purposes just use the Run button on the same window that you used to create the module. This is the expected result:
Note: If you are unfamiliar with linux, don't be intimidated by that file path, it's just how they are on linux. This would just work the same with windows and it's file path structure.
Further improving the macro
I wrote the code above with the goal of making it as easy to understand as possible, therefore the macro have plenty of room for improvement, such as:
Being able to show the data retrieved on multiple columns/A single column/Something else
Once you have retrieved the data from the file, you can display it on your spreadsheet in nearly anyway you want it. Let me know if the way you initially intended was not addressed and I will edit the answer.
Having to re-run the macro every time you want the data updated.
This is easily fixed. There are many ways to automatize the macro execution, the one I'm most familiar with consists on making it run on a loop in conjunction with a delay of, say, 5 seconds and making it start as soon as the file loads.
Sub Main
Do While True
DataFromFile()
Wait(5000)
Loop
End Sub
And from now on you should call the Main sub instead of the DataFromFile.
To make the macro run at start-up go to Tools > Customize on the Events tab and select Open Document from the list then click on the Macro button. On the dialog to select the macro, pick Main. Now close the document, reopen it, and voila!
Using Cell Ranges
It's easier to keep your code and make changes to it if you name the cell ranges and use their names instead of their absolute address. To name a range (or a single cell) you must first select it then click on Data > Define Range to give it a name, for example B1 could be called 'FilePath' and A4:A999 could be called 'DataRange'. This way if you ever need to change them, you don't have to change the macro, just the defined range name.
Don't forget to update the code to look for the range instead of the address, for example, this bit of code:
getCellRangeByName("A4:A999")
would be rewritten to
getCellRangeByName("DataRange")
Error checking
It is a good idea to check and deal with error or unexpected events. What if the file doesn't exists? What if it is bigger than the defined range?
Further reading
Official reference regarding files for OpenOffice Basic macros.
A guide on different ways to run a macro
A great introduction to macro programming

how to run VBA macro from perl code

I am new to perl & i am trying to write a module which would run a excel macro on a already open excel sheet. there is a code sniplet that describes how to run a macro from another excel sheet but i want the macro code as a subroutine in the same file. How to implement that? Can any one help?
use strict;
use warnings;
use Win32::OLE;
my $excel= Win32::OLE->new('Excel.Application')or die "Could not create Excel.Application!\n;
$excel->Workbooks->open( 'C:\Users\Me\Documents\Book1.xlsx' );
$excel->run( 'Book1!Macro1' );
# Here i want that Macro1 as sub in this file itself & not from book1
$excel->quit;
I don't really think you can do this. You would need to access
$workbook->VBProject->VBComponents
but then the typical way to pass create a macro on the fly is to call the VBComponents collection's AddFile, AddFromTemplate or Import method which all require paths to files that Excel will read itself. It's not like it's an extension of Perl and will accept an open file stream as well.
Of course, you can always write the machinery to take a in-script string, dump it out to a temporary file and send that file name to Excel. However, since Microsoft has greatly stepped up its paranoia, I wonder how many security hurdles you will need to clear to get Excel to run a macro from a temp file directory.
After you get this loaded it's simply a matter of $xl->run( 'Bookname!Macro' ). But I think the protections against attacks are bound to hinder your doing this.
Update:
Yeah, I just tried something along these lines and got "Programmatic access to Visual Basic Project is not trusted". Like I said, expect a lot of hurdles, if not complete failure.
However, you can work around that with this advice.
Actually, it turns out I was wrong, the code below allows you to add behavior to a code module.
my $prj = $wb->VBProject;
my $mod = $prj->VBComponents->Item( 'ThisWorkbook' )->CodeModule;
$mod->addFromString( <<"END_VB" );
Public Sub Doodad
MsgBox( "I am Doodad! Hear me roar!" )
End Sub
END_VB
However when I did this:
$excel->Run( $wb->Name . '!Doodad' );
I got this:
Cannot run the macro 'Book1!Doodad'. The macro may not be available in this
workbook or all macros may be disabled.

How to import a macro file (previously exported as .bas file) to Microsoft Word using command line?

I'm writing a command-line program that has a step in which I need to replace text in a Word file. The replacing task is accomplished using Word macro.
What I need to do now is to call this macro from command-line. At the moment we can do this by using the /mMacroName parameter of 'winword.exe', i.e. <path-to-msoffice>\winword.exe /mMacroName. But this needs the macro to be already available as a global macro.
Since I need to run the program on another computer, I need to import the above replacing macro programmatically... and I don't know how to do this.
Adding the macro using VBScript would be an option. You can find a sample to get started in the following related question:
Remove MS Word macro using VBScript