2

We have developed an app that can import DOC/XLS files as images using LibreOffice. However, for PCs where Office is already installed, we would like to be able to use the installed MS Office. Most people are on MS Office version 2007-2010. I know that MS provide some sort of interoperability DLLs for this purpose but I haven;t seen any example of this being used with Qt.

Update - this answer looks interesting, can the same be done in Qt?

Convert Word file pages to jpg images using C#

phyatt
  • 18,472
  • 5
  • 61
  • 80
TenG
  • 3,843
  • 2
  • 25
  • 42

1 Answers1

2

PowerShell

It looks like you can do it with Powershell on the commandline pretty easily.

Open a QProcess, and run:

http://technet.microsoft.com/en-us/library/hh847736.aspx

powershell -Command "Import-Module MSOnline";"C:\Myscript.ps1"

(Download this script or convert it to C++ com calls.)

https://gallery.technet.microsoft.com/office/Script-to-convert-Word-f702844d

powershell -Command "Import-Module ConvertWordPocumentToPDF.psm1";"ConvertTo-OSCPDF -Path C:\path\to\document.docx"

So the end code for Qt probably could be something like:

QProcess * process = new QProcess();

// put code to pipe stdout and stderr from the QProcess to this exe's stdout, or to a TextEdit.

process->start("powershell.exe -Command \"Import-Module ConvertWordPocumentToPDF.psm1\";\"ConvertTo-OSCPDF -Path C:\\path\\to\\document.docx\"");

Com Object Access

DocumentBase.ExportAsFixedFormat Method

http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.documentbase.exportasfixedformat.aspx

public void ExportAsFixedFormat(
    string outputFileName,
    WdExportFormat exportFormat,
    bool openAfterExport,
    WdExportOptimizeFor optimizeFor,
    WdExportRange range,
    int from,
    int to,
    WdExportItem item,
    bool includeDocProps,
    bool keepIRM,
    WdExportCreateBookmarks createBookmarks,
    bool docStructureTags,
    bool bitmapMissingFonts,
    bool useISO19005_1,
    ref Object fixedFormatExtClassPtr
)

First get an instance of a Word.Application

then get an instance of a document using wdApplication.Documents.Open

then call wdDocument.ExportAsFixedFormat, with the parameters of:

$wdExportFormat = [Microsoft.Office.Interop.Word.WdExportFormat]::wdExportFormatPDF
$OpenAfterExport = $false
$wdExportOptimizeFor = [Microsoft.Office.Interop.Word.WdExportOptimizeFor]::wdExportOptimizeForOnScreen
$wdExportItem = [Microsoft.Office.Interop.Word.WdExportItem]::wdExportDocumentContent
$IncludeDocProps = $true
$KeepIRM = $true
$wdExportCreateBookmarks = [Microsoft.Office.Interop.Word.WdExportCreateBookmarks]::wdExportCreateWordBookmarks
$DocStructureTags = $true
$BitmapMissingFonts = $true
$UseISO19005_1 = $false
$wdStartPage = 0
$wdEndPage = 0

(but formatted for c++, not powershell)

then when it is completed, release the com objects.

http://msdn.microsoft.com/en-us/library/kw65a0we.aspx

I haven't tested either method, but I have ran Com objects from a Qt project before. Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • Thanks I will try that. I assume powershell is standard with Windows 7 Pro – TenG Dec 05 '14 at 01:31
  • http://en.wikipedia.org/wiki/Windows_PowerShell#Versions Included in XP SP3 and above, as far as I can tell. – phyatt Dec 05 '14 at 01:38