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
Solved

Word Automation with MFC: Bookmarks

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 163
ClickOnce Install - Shortcut Question 3 103
Not needed 13 119
Problem to open Excel file 15 178
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.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…

828 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