Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Filling bookmarks in a word document.

77 views
Skip to first unread message

Nicolae

unread,
Aug 26, 2010, 5:44:54 AM8/26/10
to
Hi All,
I try to make a function that opens a MS Word document and replaces
each bookmark with a specific value.
I am able to either list all bookmarks present in the document or to
replace any bookmark with a value.
What I try to achieve is to do both operations at the same time.
The code I present below somehow skips every other bookmark (if the
bookmarks are "bookmark1", "bookmark2", "bookmark3", it will only fill
in bookmark1 and bookmark3.

Another problem is that I can't operate with bookmarks that are
located in the header/footer.

Any help appreciated.
Nicolae

void __fastcall TForm1::btnFillBookmarksClick(TObject *Sender)
{
Variant vWApp;
Variant vWDocument;
Variant vBookmarks;
Variant vBookmark;
Variant vSelect;
Variant vOptions;

AnsiString sTemplatePath = "C:\\ProjectsBC\\WordOLEBookmarks";

if (vWApp.IsEmpty())
{
try {
vWApp=Variant::CreateObject("Word.Application");
}
catch(...)
{
ShowMessage("Could not find MS Word installed on this
computer");
return;
}
}
else
{
vWApp=GetActiveOleObject("Word.Application");
}

if (vWApp.IsEmpty())
{
ShowMessage("Unable to find Word application.");
return;
}

vWApp = CreateOleObject("Word.Application");
vWApp.OlePropertySet("Visible", (Variant) true);
vWApp.OleFunction("ChangeFileOpenDirectory", (Variant)
sTemplatePath);
vWDocument = vWApp.OlePropertyGet("Documents");
vWDocument.OleProcedure("Open", (Variant) "bookmarks.doc");
vWDocument = vWApp.OlePropertyGet("ActiveDocument");
vSelect = vWApp.OlePropertyGet("Selection");
vBookmarks = vWDocument.OlePropertyGet("Bookmarks");
int nCount = vBookmarks.OlePropertyGet("Count"); // doesn't matter
if it is uppercase or lowercase
AnsiString sBookName;
vBookmarks = vWDocument.OlePropertyGet("BookMarks");
nCount = vBookmarks.OlePropertyGet("Count");
AnsiString sBookValue;
int i;
for (i=1; i <= nCount; i++) {
vBookmark = vBookmarks.OleFunction("Item", (Variant)i);
sBookName = vBookmark.OlePropertyGet("Name");
// Memo1->Lines->Add(sBookName);

vBookmark = vBookmarks.Exec(Function("Item") << sBookName);
vBookmark.OleProcedure("Select");
sBookValue = "bookmark value " + IntToStr(i);
vSelect.Exec(Procedure("TypeText") << sBookValue);
}

// vWDocument.OleProcedure("SaveAs", (Variant) "C:\\AProjectsBC3\
\SML\\SML01_N_80012345.doc" );
// vWDocument.OleProcedure("Quit");
// vWApp.Exec(Procedure("Quit"));

vWDocument.OleProcedure("Close"); // closes the saved Word document
vWApp.OleProcedure( "Quit" ); // closes Word Application

vWApp = vWDocument = vBookmark = vBookmarks = vSelect = vOptions =
Unassigned;
Application->ProcessMessages();
}
//---------------------------------------------------------------------------

Nicolae

unread,
Aug 26, 2010, 8:33:35 AM8/26/10
to
I figured it out in the end.
The line "vBookmark = vBookmarks.OleFunction("Item", (Variant) i); "
should actually be:
vBookmark = vBookmarks.OleFunction("Item", (Variant) 1);
because after replacing the first bookmark, the next bookmark will
become the first bookmark in the document.
Hope this will help some of you.

Regards,
Nicolae

0 new messages