Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Word Automation with MFC: Bookmarks

Posted on 2003-11-26
6
Medium Priority
?
1,009 Views
Last Modified: 2013-11-25
Hi,
   I have scoured the internet for examples in MFC/VC++ to automate word and then:

1) Open a named word document with bookmarks ["Address", "Postcode", "Surname"]
2) Insert text from my MFC App/VC++ to these bookmarks
      InsertBookMark ("Address", "Buckingham Palace") for example.

I'm having major problems with step 2, i've tried the following code:

  long m_LoopVar;
  for(m_LoopVar=1;m_LoopVar<= m_Books.GetCount();m_LoopVar++)
 {
     startPos=endPos ;
    docObj.Activate();
   m_Book.AttachDispatch(m_Books.Item(COleVariant(m_LoopVar)));

  m_BookMarks.AddString (m_Book.GetName());

selObj = appObj.GetSelection ();
 rangeObj = selObj.GetRange ();

// Insert text into bookmark - does not work -
// first param is wdGoToBookMark
rangeObj.GoTo ( COleVariant ((short) -1), vtOptional, vtOptional, COleVariant (m_Book.GetName()));
rangeObj.InsertAfter ((const char*) m_Book.GetName());
}

Many thanks for any help

Dave
0
Comment
Question by:DMawer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 1000 total points
ID: 9830798
This should help

void CNewDocDlg::InsertBookmarkText(LPCSTR szBookmark, LPCSTR szText)
{
      TRACE1("Inserting Bookmark: %s\n", szBookmark);


      if(lstrcmp(szText, _T("-")) == 0)
            return;

      COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

      try
      {
            g_selWord.GoTo(COleVariant((long)wdGoToBookmark),
                                    covOptional,
                                    covOptional,
                                    COleVariant(szBookmark, VT_BSTR));

            g_selWord.InsertAfter(szText);
      }
      catch(COleDispatchException* e)
      {
            //Assume it is a missing bookmark
            TCHAR   szCause[255];
            e->GetErrorMessage(szCause, 255);

            TRACE1("COleDispatchException: %s\n", szCause);
            TRACE1("****** Bookmark: %s\n", szBookmark);
//            e->ReportError();
      }
      catch (_com_error &e)
      {
            PrintComError(e);
      }
      catch(CUserException* e)
      {
            TRACE("CUserException thrown\n");
      }
      catch(CException* e)
      {
            TRACE("CException thrown\n");
            e->ReportError();
      }
}
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9830809
also
enum WdGoToItem
{
    wdGoToBookmark = -1,

and

MSWord::Selection g_selWord;
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 11850391
My comment should provide the solution
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question