Solved

Word Automation with MFC: Bookmarks

Posted on 2003-11-26
6
999 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 250 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Is IHttpActionResult a promise pattern? 2 82
Capture logon name 13 119
Windows shortcut location resolution on double click open 4 78
Please explain purpose of GZIP 4 83
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…

751 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