Word Automation with PHP

31 Jul 2017

Windows Server setup

Make sure to set the correct COM permission to “This User”.

If it’s not working try to select “Interactive User”. Read more

PHP Setup

extension=php_com_dotnet.dll

Usage


try {
    $word = new COM("word.application");
} catch (Exception $ex) {
    echo $ex->getMessage();
    exit;
}

// Enable screenupdating (slower) but better pdf rendering results
$word->Screenupdating = true;

// Minimize window (0 - normal, 1 - fullscreen, 2 - minimized).
$word->WindowState = 2;

// Hide window
$word->Visible = 0;

// Disable language detection
$word->CheckLanguage = false;

// Disable overwrite mode
$word->Options->Overtype = false;

// Disable auto recovery
$word->Options->SaveInterval = 0;

// Disable the assistant
$word->Assistant->Visible = false;

// Disable alerts
$word->DisplayAlerts = false;

// Do fancy stuff...

// Close word
$word->Quit();
$word = null;