>I have a problem importing a Microsoft Office type libraries into my
>application. I am using Visual Studio 2005. I tried it with both
>#import directive and wizard->AddClass->FromTypeLib. Nothing seems to
>work. Lately I found this article: http://support.microsoft.com/kb/307473.
>...\debug\excel.tlh(1219) : error C2371: 'FontPtr' : redefinition;
>different basic types
<cut>
I've tried the KB article step-by-step, and it works fine in VS2003.
(I don't have VS2005 and can't try with it.)
Moreover, I don't have this "excel.tlh" file... what is this file?
MrAsm
I don't know if this will help you much, but I have encountered the same
problem and do not know the cause. I did find that the example works if
built in VC++ version 6, and then the project can be moved to VC++ 2005
without the #import directive.
I have found that the ATL approach works much better than the MFC
approach for Office COM interfaces. Here's a sample:
http://msdn2.microsoft.com/en-us/library/e85w507k(VS.80).aspx
--
Scott McPhillips [MVP VC++]
>I have found that the ATL approach works much better than the MFC
>approach for Office COM interfaces. Here's a sample:
>http://msdn2.microsoft.com/en-us/library/e85w507k(VS.80).aspx
Yes, ATL is more COM-oriented than MFC.
Maybe, would it be easier for the OP to use VS2005 C++/CLI extensions
and COM Interop to automate Excel?
i.e. create the interop DLL from Excel typelib, and then use the
interop DLL using C++/CLI.
Maybe the COM Interop with C++/CLI extensions is easier than pure C++
and ATL?
MrAsm
Well, let's be real clear: What would really be easier in this case
would be if Microsoft samples would compile with VC++ 2005. I've been
climbing the same learning curve as the OP and it has been very
depressing due to cruddy samples, cruddy documentation and
incompatibilities with older sample programs (from codeguru, codeproject
and two books I bought). It's a bloody minefield.
As for adding interop plus CLI extensions on top of Office on top of
COM, ... Sheesh! Way too much bloat for me.
>Well, let's be real clear: What would really be easier in this case
>would be if Microsoft samples would compile with VC++ 2005.
I do agree.
>As for adding interop plus CLI extensions on top of Office on top of
>COM, ... Sheesh! Way too much bloat for me.
This is because you can manage ATL, so you can do direct Office COM
automation programming using ATL. But ATL is not trivial, and maybe
the OP could find easier to add some layers of architecture (even if
this is of course less efficient and more "bloat"), and manage Office
using the C++/CLI.
MrAsm
// Prepare a new mail message
_MailItemPtr olMail(spApp->CreateItem(olMailItem));
BSTR to = reinterpret_cast<BSTR>("t...@gmail.com");
olMail->put_To(to);
BSTR sbj = reinterpret_cast<BSTR>("Subject");
olMail->put_Subject(sbj);
BSTR bdy = reinterpret_cast<BSTR>("Hi James,\n\n"
"\tI'll see you in two minutes for our meeting!\n\n"
"Btw: I've added you to my contact list!");
olMail->put_Body(bdy);
// Send the message!
olMail->Send();
I get error in Send() : Unhandled exception at 0x7c57e592 in Auto.exe:
Microsoft C++ exception: _com_error at memory location 0x0012f3c0..
When I press BREAK it leads me to msoutl.tli file, Send() method.
Can someone please help me out here.
Thanks in advance.
> Hi, guys!
> Thanks for all your comments.
> I did manage to compile the project.
> Now I try to run the following code:
> _ApplicationPtr spApp("Outlook.Application");
>
> // Prepare a new mail message
> _MailItemPtr olMail(spApp->CreateItem(olMailItem));
> BSTR to = reinterpret_cast<BSTR>("t...@gmail.com");
> olMail->put_To(to);
> BSTR sbj = reinterpret_cast<BSTR>("Subject");
> olMail->put_Subject(sbj);
> BSTR bdy = reinterpret_cast<BSTR>("Hi James,\n\n"
> "\tI'll see you in two minutes for our meeting!\n\n"
> "Btw: I've added you to my contact list!");
> olMail->put_Body(bdy);
>
> // Send the message!
> olMail->Send();
>
> I get error in Send() : Unhandled exception at 0x7c57e592 in Auto.exe:
When you use the #import wrappers created by default you will need to catch
_com_exception& as this is the way the wrappers signals HRESULTs with the
failure bit set. Debug into any method like put_To or Send and see that it
checks for FAILED(hr) and does a _com_issue_error if FAILED(hr) is true.
Second, try to avoid casting as much as you can, try to avoid
reinterpret_cast even harder.
You cannot create valid BSTRs the way you do. A C style string is layed out
differently in memory as a BSTR. BSTRs are created with SysAlloc-functions
and must be freed with SysFreeString. Use CComBSTR or _bstr_t to manage raw
BSTRs like so:
_bstr_t to(L"t...@gmail.com");
olMail->put_To(to);
--
SvenC
Please tell us how you got it to compile.
Have you used the debugger to look at the BSTR's? The
reinterpret_cast's you are using look like a bad way to try and set a
BSTR. Try BSTR to = _T("t...@gmail.com");
Please not!
This is broken in the same way as the OPs code: the char array part looks
okay but a BSTR has its length prepended, so that null termination is not
mandatory. So when you do BSTR b = _T("hello world") and someone calls
SysStringLen(b) you are in undefined behaviour land. The same goes for
SysAllocFree. You must use a helper class like CComBSTR or _bstr_t if you
want automatic conversion with C strings.
--
SvenC