Re: MFC GoTo Bookmark Error
On Sun, 17 Aug 2008 23:37:00 -0700, Landon
<Landon@discussions.microsoft.com> wrote:
COleVariant m_True( ( short )TRUE ), m_False( ( short )FALSE ),
m_Long( ( long )DISP_E_PARAMNOTFOUND, VT_ERROR );
The m_True and m_False definitions are suspect, as VARIANT_TRUE != TRUE.
You probably need to write m_True(VARIANT_BOOL, VT_BOOL) and similarly for
m_False.
That should have been m_True(VARIANT_TRUE, VT_BOOL). Again, VARIANT_TRUE !=
TRUE, and casting to short and not specifying the VT_xxx type in the second
argument causes the variants to hold VT_I2 values, not VT_BOOL values. This
latter consideration affects m_False as well.
What do you mean? I don't really understand your explanation about the
m_True and m_False. Can you please give explanation by fix my code as
necessary?
What I'm saying is that you should double-check the types these COleVariant
objects are supposed to represent. I can't fix your code because I don't
know for a fact that it's incorrect; that's why I said the definitions were
"suspect". I don't know that they're wrong, but they do look strange,
because they are apparently intended to represent boolean values, and COM
defines VARIANT_TRUE and VARIANT_FALSE for that. Even though VARIANT_BOOL
is a typedef for short, VT_BOOL and VT_I2 have different values, so you may
not be able to substitute a variant holding a VT_I2 value for one that is
supposed to hold a VT_BOOL value.
<snip>
COleVariant vtBookmark( ( short ) -1 ),
vtNext ( ( short ) 2 ),
vtName ( ( CString ) "support" ),
vtLoopVar ( ( short ) 1 );
<snip>
selObj.GoTo( vtBookmark, vtNext, COleVariant( vtLoopVar ), COleVariant(
sBookmarkName ) );
I also think all these but vtName are initialized to the wrong variant
type. You should be using the relevant enums to initialize vtBookmark and
vtNext. Where did you get the idea to cast them to short (VT_I2)? When I
use #import to create the .tlh/.tli files from MSWORD.OLB, the compiler
emits their types as simple enums, whose sizeof == 4. Thus, I would think
they should be VT_I4. Have you tried casting them to long? I would think
that vtLoopVar should be long as well. Hope this helps...
I'm sorry I don't understand your explanation as I am trying to fix it now.
Please give at least one example by fixing my code.
Assuming -1 is the right value for vtBookmark, try vtBookmark(-1L) above.
The documentation you linked to discusses the enum values, and if you're
using #import, they are available to you. I think you would need to cast
them to long to resolve ambiguity in the constructors, though, as in
vtBookmark((long) Word::wdGoToBookmark).
Then again, maybe you know what you're doing, as you didn't answer my
question, "Where did you get the idea to cast them to short (VT_I2)?" I
don't know that your code is wrong. I've never programmed the Word object
model, so all I can do is point out things that look strange to me based on
my experience with, for example, the Visual Studio object model.
You might find some people familiar with Word programming in this group:
http://groups.google.com/group/microsoft.public.word.programming/topics?lnk
or in one of these:
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.word.vba%2C&
--
Doug Harrison
Visual C++ MVP