Solved

Word Automation with MFC: Bookmarks

Posted on 2003-11-26
6
1,004 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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…

630 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