1

Here we are using php COM(word.application) for converting the html to docx. But COM("word.application")->ActiveDocument->SaveAs($htmlfilepath, 16) function is giving the below mentioned error. Uncaught com_exception: Parameter 0: Type mismatch.

// WdSaveFormat: http://msdn.microsoft.com/en-us/library/office/bb238158%28v=office.12%29.aspx
static $wdSaveFormats = array(
    'docx' => 16,
    'html' => 10,
    'rtf'  => 6,
    'txt'  => 2,
    'doc'  => 0,
    'pdf'  => 17,
);
$newSaveFormat = 16;
if (preg_match('@\.(.{3,4})$@', $new, $arr)) {
    if (isset($wdSaveFormats[$arr[1]])) {
        $newSaveFormat = $wdSaveFormats[$arr[1]];
    }
}

$rethrow = NULL;
try {
    // Document.Enable long file name https://msdn.microsoft.com/en-us/vba/word-vba/articles/weboptions-uselongfilenames-property-word
    $word->ActiveDocument->WebOptions->UseLongFileNames = true;

    // Document.SaveAs: http://msdn.microsoft.com/en-us/library/office/bb221597%28v=office.12%29.aspx
    // $new = 'Z:\data\reportPdf/1636634123sampl_test_2019programreport.docx';
    **$word->ActiveDocument->SaveAs($new, $newSaveFormat);**
} catch (Exception $rethrow) {
    // We still want to close the document.
}

Thanks in advance!

  • Are you sure that $newSaveFormat is valid type? – silentwasd Nov 15 '21 at 10:15
  • @МихаилТихонов Yes its 16 as per the documentation. http://msdn.microsoft.com/en-us/library/office/bb238158%28v=office.12%29.aspx – Abhra Sarkar Nov 15 '21 at 10:16
  • I mean preg_match can change it. So are you sure, that after preg_match $newSaveFormat is 16? – silentwasd Nov 15 '21 at 10:16
  • i tried with only 16 directly. Still the same – Abhra Sarkar Nov 16 '21 at 06:40
  • There is [a same question](https://stackoverflow.com/questions/39482937/how-to-solve-exception-com-exception-with-message-parameter-0-type-mismatch) since 2016 but no one can answered. I'm currently stuck with this problem too. I think MS Word COM object is completely broke and unusable with PHP. – vee Feb 25 '22 at 04:11

0 Answers0