Noticed another feature (word 2007).
Created this app:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <ActiveQt/QAxObject>
#include <ActiveQt/qaxbase.h>
#include <QDir>
QAxObject *word, *objVBComp, *objCodeMod;
QString VbaScriptLine = "Sub MacroCreatedByUser() : ActiveDocument.VBProject.References.AddFromGuid \"{00020802-0000-0000-C000-000000000046}\", 1, 5 : End Sub";
QString OpenFilePath = "input.doc";
QString SaveFilePath = "output.doc";
QString MacroName = "Test macros";
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_pressed()
{
word = new QAxObject( "Word.Application" );
word->setProperty("DisplayAlerts", - 1);
word->setProperty("Visible", true);
QString PathTmp = QDir::toNativeSeparators(QDir::currentPath() + "\\" + OpenFilePath);
word->querySubObject( "Documents()" )->dynamicCall( "Open(FileName, ConfirmConversions, ReadOnly )", QDir::toNativeSeparators(QDir::currentPath() + "\\" + OpenFilePath), false, false );
objCodeMod = word->querySubObject( "ActiveDocument" )->querySubObject( "VBProject" )->querySubObject( "VBComponents" )->querySubObject("Item(Index)", 1)->querySubObject( "CodeModule" );
objCodeMod->dynamicCall( "AddFromString(QString)", VbaScriptLine );
word->querySubObject( "Application" )->dynamicCall( "Run(MacroName)", MacroName );
word->querySubObject( "ActiveDocument" )->dynamicCall( "SaveAs( FileName, FileFormat )", QDir::toNativeSeparators(QDir::currentPath() + "\\" + SaveFilePath), 0 );
word->querySubObject( "ActiveDocument" )->dynamicCall( "Close(SaveChanges)", 0 );
word->dynamicCall( "Quit()" );
delete word;
}
While executing the line: «objCodeMod->dynamicCall( "AddFromString(QString)", VbaScriptLine );» Windows OS fixes an error in the WINWORD.EXE application:
win_error.png
Despite the error, the macro is added and called as expected.