Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Word Bookmarks
Message
From
13/10/2005 06:48:54
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
ASP.NET
Category:
Other
Title:
Miscellaneous
Thread ID:
01058569
Message ID:
01058586
Views:
10
>Hello all, does anyone know if it is possible to enumerate the Bookmarks collection in a Word 97 ( or 2000 ) document ?, what I am trying to achieve is !, I have a class that creates a new Word document based on a specified template, I then populate some "known" bookmarks with data, print it out and exit, all this works fine. What I would like to do is: set the template property of my class, enumerate the bookmarks in the template ( get their names ) and populate them with data from a DataSet containing columns of the same name, as I say I don't know if this is possible

Pete,
This sample is in VFP but not hard to convert to .Net:
#include wdconstants.h
#Define NL Chr(13)+Chr(10)
*** set the LOCALEID to English
nlLocaleId=Sys(3004)		&& Save local id
=Sys(3006,1033)				&& We will be sending instructions in English

Use _samples+'data\employee'		&& test table
m.lcFileName = Sys(5)+Curdir()+'myBoookmarkTest.doc'
oWordDocument=Createobject("word.application")	&& Create word object
With oWordDocument
  .Documents.Add && Create a new doc
  Scan
    .Selection.TypeText(Transform(emp_id)+NL+First_Name-(' '+Last_Name)+NL)
    .Selection.Collapse(wdCollapseEnd)
    lnRangeStart = .Selection.Range.End
    .Selection.TypeText(Nvl(notes,'')+NL+NL)
    lnRangeEnd   = .Selection.Range.End
    .Activedocument.Bookmarks.Add('b'+Alltrim(Transform(emp_id)),.Activedocument.Range(lnRangeStart,lnRangeEnd))
  Endscan
  .Activedocument.SaveAs(m.lcFileName)
  .Activedocument.Saved = .T.
  .Quit
Endwith

* Now create a new file based on saved template
* and change text based on bookmarks
Select employee
lcEmployeeRecnos = '1,3,4,5,6'
lnEmployee = Alines(aEmpNo,lcEmployeeRecnos,.T.,',')
Dimension aBookmarks[lnEmployee,2] && Change text for only few employee
For ix=1 To lnEmployee
  Go Val(aEmpNo[m.ix])
  aBookmarks[m.ix,1] = 'b'+Alltrim(Transform(emp_id))
  aBookmarks[m.ix,2] = 'Notes for employee '+First_Name-(' '+Last_Name)+NL+NL
Endfor

* assumed typing replaces text option is set
oWord = Createobject('Word.Application')
With oWord
  .Documents.Add(m.lcFileName) && New file based on template file
  .Visible = .T.
  For ix=1 To Alen(aBookmarks,1)
    If .Activedocument.Bookmarks.Exists(aBookmarks[m.ix,1])
      * preserves bookmark, inserts text replacing old one
      .Selection.Goto(wdGotoBookmark,,,aBookmarks[m.ix,1])

      With .Selection
        .Font.Color = Rgb(255,0,0)
        .Font.Bold = .T.
        .TypeText( aBookmarks[m.ix,2] )
      Endwith
    Endif
  Endfor

  .Visible = .T.
  .Activate
Endwith
**** Set the LocaleId to the previous value
=Sys(3006,Val(nlLocaleId))
For .Net you can utilize "VS Tools for MS office system". Check 'How to: Update Bookmark Text' in MSDN help.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform