tags:

views:

69

answers:

2

Hi,

my question is: What is the best way to call MSWord from C++

I never did this before. I always used VB6 and the Word-COM-Library:
C:\Program Files\Microsoft Office\OfficeXX\MSWORD.olb
resp:
"Microsoft Word XX.0 Object Library"
how should I use/call Word from C++ (VS2010) ?
should I use .NET ? (C++CLI)

do you have a good step by step explanation?
regards and many thanks in advance
Oops
P.S. a code example in VB

Option Explicit

Sub Main()
    Dim mWord    As New Word.Application
    mWord.ScreenUpdating = False
    Dim mMaxParagraph  As Long
    Dim aDoc As Word.Document
    Dim aFileName As String
    aFileName = "C:\mydoc.doc"
    Set aDoc = mWord.Documents.Open(aFileName)
    mMaxParagraph = mWord.ActiveDocument.Paragraphs.Count
    Debug.Print CStr(mMaxParagraph)
    aDoc.Close
    mWord.Quit
End Sub
A: 

Doing COM in native C++ is an exercise in pain. You might be best off using C++/CLI, which gives you all the features of C++ plus the ability to interact with .NET stuff easily.

Greg Hewgill
+2  A: 

Doing COM in C++ isn't that painful as long as you're using ATL smart COM pointers. You simply need to #import the type library and this will create a whole bunch of smart pointer classes that you can use in your application.

Rob
Thanks! that sounds good, could you please give a small example how this should look like?
Oops
IIRC `#import <C:\Program Files\Microsoft Office\OfficeXX\MSWORD.tlb>`. This will generate a number of temporary C++ files with declarations.
MSalters