Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
I need help quick (this weekend!) on word automation!
Message
From
26/05/2001 08:32:01
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
25/05/2001 19:45:52
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
00511736
Message ID:
00511781
Views:
22
Hi David,
Replies are scattered where relevant in italic.

>I am making my first stab at word automation. I have a deadline of Tuesday AM. I am hoping to get some help over the weekend, and, if necessary, pay for consulting time, if someone can provide me with extensive help. Of course if 'quick help' will do the trick that is even better.
>
>Here is what I am trying to accomplish:
>
>1) My first stab at Word automation.
>2) I have a word doc that has mostly fixed text. I need to replace it with some data.

For replacement options are doc. Variables, Bookmarks, mailmerge fields.


>(I have attachd my first cuts -prgs and docs)
>3) I have studied the vb help on the word objects/methods. (I know about Della Martin's book - but it doesn't look like I could get it on short notice - unless you know of a magical way) But many things are not obvious in the docs. Any many things don't work as I expect them to.
>
>This is my strategy so far:
>
>1) I make a copy of a 'template doc' to another 'temp' doc.

If you have a template.doc you don't need a copy. Create new doc based on template :
oWord.Documents.Add(lcTemplateDocPathAndName)
>2) I open the doc via automation.
>
>1) Some of the data is in word tables. I think I've got the hang of replacing this data.

Instead of replacing, writing text afresh and converting to table is more appropriate IMHO. Word table is formatted thereafter.

>2) Other data is not in tables. For this data I am putting strings like 'COVER_PAGE_PRINT_DATE, in what I call the template doc. Then I am executing find/replace via the find object. If you look at the prg below - testword3.prg - there you will see what is perhaps the klugiest code of all time - don't laugh at me!

Find/Replace would work. But as said before doc. Variables, Bookmarks, mailmerge fields sound more feasible. - Nobody has right to laugh. As a beginner you're well IMHO. When I started first I think I wrote code worse than yours. No books, no object model and little doc it was more of a trial and error those days :)

>
>Here are some of the things that are confusing to me:
>1) Since vfp does not accept named parameters, it appears that you need to list all the parameters for every method you call, as placeholders.
>2) It is not obvious from the documentation what the defaults are for each method,and what will happen if you just call the method, without any parameters (e.g. odoc.close - appears to close and save by default ??/)
>3) In testword3.prg the only way I could get multiple finds/replaces to happen would be to: 1) do one 2) kill the odoc object and kill word 3) recreate word application instance, etc., and do the find/replac again. When I tried to do a find/replace after doing one already, the find fails. if I set wrap to true, I get an out of range error when it executes the find.execute
>4)I assume that after a find, the cursor is positioned in the doc, at the end if no find, or at the place in the doc where it found the text?? If i can't get the 'next' find to 'wrap', how can I get the cursor back to the top of the document?
>5) Most of the example code given in the documentation shows setting properties of the find object, and then calling execute with only a couple of parameters. Do the parameters set in the find object control what happens in the execute? For some reason I could not get things to work right by setting properties in the find object and calling execute with no parameters.
>Since vfp does not accept named parameters, it seems as if you must specify all parameters or none??, or use commas to signify position?? Most of the example code does not look like mine!
>
>5) Are there other more efficient ways to do what I am trying to accomplish?
>
>
>6) In addition to just replacing existing 'variables' with short strings, I also need to replace big chunks of text (a few pages) the pages are carefully formatted, and have gifs embedded in them. I have each 'chunk' in its own file. I want to be able to:
>a) open a 'chunk' and copy it to the clipboard b) paste the chunk at the appropriate place in my document. c) repeat the process as necessary. How can I do this?
>



1) Not truely. Yes you need to send positional parameters and yes you need to keep placeholders up to the last parameter you want to pass. I explained this before in detail here. If I can find it I'll paste message number.
You have to provide positional parameters but you can omit default ones. ie:
* Replace all w2k with windows 2000
.ActiveDocument.Range.Find.Execute(;
  "w2k",,,,,,,,,"Windows 2000",wdReplaceAll)
Here if I need only to find first occurence of w2k (no replacement) call would just be :
.ActiveDocument.Range.Find.Execute("w2k")
2) In doc generally defaults are told. ie: Close w/o params would cause a dialog to popup asking save changes (not sure docs say this).
3) :) Worst case scenario. My thumb of rule call VBA side code as less as possible. VBA is slow even if your code was pure correct. See above 'find.Execute' for doing it at once.

4) If no find then cursor stays at position where it was before. However you should never trust (at least I don't) where cursor is left. Move it explictly when you need it. This is somewhat complicated in word. You have many options to move letter, word, page... or dirctly to start-end of a range. Ranges could be a variety of things. Also there are built-in bookmarks like StartOfDoc, EndOfDoc etc.

5) (1) Answers this I think.


5.1 Yes. More than one. What to choose should be clear only to indiviual who knows his data, what to represent, how to represent.

6) No need to copy to clipboard it sounds. Just insert file at desired location.


Looking to code never set visible = .t. at top - slows down considerably
However OK when debugging-developing, OTOH some VBA code doesn't execute the same way when visible=.t. and .f.
I don't know why many using style (I'm not critizing your coding just trying to protect you from future failures - been there done that) :

oDoc=oWord.Documents.Open(lcfilename)
and using oDoc thereafter. Do it like this IMHO :
oWord = create...
*..
with oWord.Activedocument
I can't show you the difference promptly but you'd encounter yourself as you proceed with word.

A reducted version of code that would work correctly (assumng you mean replace all occurences of Company with prestige and all occurences of date with long format datetime :
lcTemplateName = 'c:\myTemplates\template.doc'
lcFileName = 'c:\mySavedDocs\myTest.doc'
#INCLUDE 'wdconst.h'
oWord=CREATEobject("Word.Application")

With oWord
  .Documents.Add(lcTemplateName)
  With .ActiveDocument.Range.Find
    * Replace company
    .Execute("COVER_PAGE_CLIENT_COMPANY",,,,,,,,,"Prestige",wdReplaceAll) 
    * Replace datetime
    .Execute("COVER_PAGE_PRINT_DATE",,,,,,,,,transform(;
       datetime(),'@YL'),wdReplaceAll)
  Endwith
  *	.Visible= .t.
  *	.activate
  .ActiveDocument.SaveAs(lcFileName) && Give a name and save path to new doc
  .Quit(wdSaveChanges) && Quit saving
Endwith
PS : Do you really have word2000 constants ? If so I'd like to get them, I still have w97 ones.
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