Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
GoTo Bookmark in Word...
Message
From
18/01/2006 10:39:11
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP SP2
Network:
Novell 6.x
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01087963
Message ID:
01087997
Views:
8
>okay when I try this I now get error. OLE Idispatch exception code 0 from word...cannot find requested bookmark. It passes the exists test but crashes on the goto portion? Any ideas

Bookmark might be out of page range (ie: header, footer section). Then instead of GoTo you might directly use one of other ways.
#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 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)
* Type one - replaces text totally, removes bookmark
*!*	    With .Activedocument.Bookmarks(aBookmarks[m.ix,1]).Range
*!*	      .Text = aBookmarks[m.ix,2]
*!*	      .Font.Color = Rgb(255,0,0)
*!*	      .Font.Bold = .T.
*!*	    Endwith


* Type two - preserves bookmark, inserts text at start
*!*	    .Activedocument.Bookmarks(aBookmarks[m.ix,1]).Select
*!*	    With .Selection
*!*	      .Collapse(wdCollapseStart)
*!*	      .Font.Color = Rgb(255,0,0)
*!*	      .Font.Bold = .T.
*!*	      .TypeText( aBookmarks[m.ix,2] )
*!*	    EndWith

* Type three - 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

* Type four - preserves bookmark, inserts text AFTER bookmark, outside bookmark
*!*		    .Activedocument.Bookmarks(aBookmarks[m.ix,1]).Range.InsertAfter(aBookmarks[m.ix,2])
 
   
  EndFor
  
  .Visible = .T.
  .Activate
Endwith
**** Set the LocaleId to the previous value
=Sys(3006,Val(nlLocaleId))
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