Solved

Word Automation with MFC: Bookmarks

Posted on 2003-11-26
6
978 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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 …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now