Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Vfp to msword, how?
Message
From
08/03/2000 10:21:37
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
08/03/2000 06:40:01
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00342824
Message ID:
00343260
Views:
28
>Hi Cetin,
>
>and thanks! I am getting closer, but still are a little confused when trying to translate Word/vba code into VFP equivalents. Here is what I am trying to do now, this works in a VBA macro, but how can I create corresponding code in VFP:
>
>.Application.Browser.Target = wdBrowseSection
>.Application.Browser.Next
>.Selection.MoveUp Unit:=wdLine, Count:= 1
>.Selection.Movedown Unit:=wdLine, Count:= 2, Extend:=wdExtend
>.Selection.Delete Unit:=wdCharacter, Count:=1
>
>By the way, this code is used to remove the dividing line that Word inserts
>at the end of a document created by mailmerge from a vfp data record, and i wonder if it is possible to tell Word not to insert this divider at all?
>
>With regard to (wdPromptToSaveChanges = false), that is on fact what I find
>in my VBA macros, so I assume somehow the syntax is correct. I have tried
>the variations you suggest, but did not have much success. But that seems no longer to be any problem anyway.
>
>Regards, Rolf


Hi Rolf,
Converting VBA macros is not hard. You should pay attention to "Applies". Now let's convert the problematic ones :
You're using "Selection" object which "Applies" to application, pane and window objects (either check "Applies" item in help if there is one or click the parent object shape to see list). Generally Application is the one you need (here it's a reference to cursor position). Therefore in VFP it becomes :
.Application.Selection

Next you're using "MoveUp" method of "Selection". In help it gives syntax as :


expression.MoveUp(Unit, Count, Extend)

expression Required. An expression that returns a Selection object.

Unit Optional Variant. The unit by which to move the selection. Can be one of the following WdUnits constants: wdLine, wdParagraph, wdWindow or wdScreen. The default value is wdLine.

Count Optional Variant. The number of units the selection is to be moved. The default value is 1.

Extend Optional Variant. Can be either wdMove or wdExtend. If wdMove is used, the selection is collapsed to the end point and moved up. If wdExtend is used, the selection is extended up. The default value is wdMove.


expression Required and an object which returns a "selection" object. We have already satisfied it with :
.Application.Selection

Syntax :

.MoveUp Unit:=wdLine, Count:= 1

is valid "named argument" style that you could use in VB (arguments are named). Note that you could say :

.MoveUp Count:= 1, Unit:=wdLine

too. Order of parameters don't matter because they're "named". In VFP, however, we use "positional arguments". That's each argument is identified by its absolute position in parameter list. Fortunately in w97 and up that position of arguments are exactly the same as in help syntax. So applying this to VFP you need to say :

.Application.Selection.MoveUp(UnitParameter, CountParameter, ExtendParameter)

Help identifies what could parameters be and optional or not ("optional variant" translate to VFP as "optional variable"). If a parameter is optional then it has a default value too. Here for "MoveUp" all 3 parameters are optional and "Unit" defaults to wdLine, Count to 1 and Extend to wdMove.
Now say we want to move 3 (Count) lines (Unit) up and do not want to extend the selection (that's shift is not pressed while moving) (Extend). Parameters would be :

.Application.Selection.MoveUp(wdLine, 3, wdMove)

Now since all three parameters are optional we could omit those we want with "default" values (Defaults for Unit and Extend are same as what we want):

.Application.Selection.MoveUp(, 3,) && Commas serve as placeholder for omitted

Since we don't use any nondefault parameter after 3 (Count) we could write it shorter :
.Application.Selection.MoveUp(, 3)

MoveDown is almost same. So :
.Selection.Movedown Unit:=wdLine, Count:= 2, Extend:=wdExtend

translates as :
.Application.Selecton.MoveDown(,2,wdExtend)

In :
.Selection.Delete Unit:=wdCharacter, Count:=1
you use syntax 3 which translates as :
.Application.Selection.Delete() && Parameters omitted
* Because we used default values for both Unit and Count.

wdExtend, wdLine etc are VBA constants. I assume you include a header file having those constants. It's good to do so. Alternatively you could define only those used or directly use their values. ie:

#define wdExtend 1
.Application.Selecton.MoveDown(,2,wdExtend)

.Application.Selecton.MoveDown(,2,1)

#include "wdconstants.h"
.Application.Selecton.MoveDown(,2,wdExtend)

are all identical provided "wdconstants.h" is a valid header containing those constants. In any case if you can't find constants header file (which is also here on files section AFAIK) you could :
*!* -Open Excel
*!* -Select tools\macro\VB editor
*!* -Press F2 to bring up "Object browser"
*!* -Find needed xlConstant type ie:xlChartType
*!* -On right window click needed Constant ie: xl3DArea
*!* -Below the window constant value is displayed
(I took the shorcut paste for Excel info. But it's eaxctly same for other Office apps)

In VFP you could save an object reference to a variable and use it :
oControl = thisform.myContainer.myControl
or
oControl = thisform.PageFrame1.pages(1).controls(4)

also in VFP you could use with...endwith :
with thisform.PageFrame1.pages(1).controls(4)
.value = "Hello"
.fontbold = .t.
.refresh
endwith

also you could use for each ... endfor with collections :
for each oPage in thisform.pgf1.pages
oPage.Blahblah
endfor

Good it's just the same in OLEAutomation to word, excel etc. ie: These are totally valid :
for each oTable in oWord.Tables
oTable.blahblah
endfor

with .Application.Selection
...
endwith

oSelection = oWord.Tables(4).Select && Returns a selection object
oSelection.Delete

Finally you whole macro code translated as :
with .Application
 with .Browser
    .Target = wdBrowseSection
    .Next
 endwith
 with .Selection
    .MoveUp(,1)
    .Movedown(,2,wdExtend)
    .Delete()
 endwith
endwith
As in VFP there are many ways to skin a cat :) As I understand from code you clear the "section breaks" that merge placed (divider = section break ?). If I understood it right then another way is to "replace all" section breaks with something else (here with nothing - just ""). Section break, paragraph marks and alike has special replacement codes like "^b", "^p". Below code replaces all sectionbreaks with 2 newline + [End of Merge]+newline :
.ActiveDocument.MailMerge.Execute && Ready to merge - execute
.ActiveDocument.Range.Find.Execute("^b",,,,,,,,,"^p^p[End of Merge]^p", wdReplaceAll)
endwith
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