Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to Select a variable range in Word
Message
From
22/03/2004 20:35:47
Peter Wagner
Point Informática Ltda.
Limeira, Brazil
 
 
To
22/03/2004 04:58:38
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
00888137
Message ID:
00888720
Views:
49
>>>>Im building many different strings in VFP (VFP_VAR1,VFP_VAR2,... ) and replacing it in Word.
>>>>The string looks something like this:
>>>>
>>>>"a) Entrada de R$ %2.816,52 (Dois Mil Oitocentos e Dezesseis Reais e
>>>>Cinqüenta e Dois Centavos )%% em %2 (Duas )%% parcelas %NÃO REAJUSTÁVEIS e SEM
>>>>JUROS%%, sendo  a  primeira no valor de R$ %1.408,26%% no dia %15/03/2004%%, e a
>>>>segunda no valor de R$ %1.408,26%% no dia %15/04/2004%%"
>>>>
>>>>Im replacing the text in Word this way..
>>>>
>>>>LOCAL oWord AS "Word.Application"
>>>>IF TYPE("oWord") <> "O"
>>>>	oWord = CREATEOBJECT("Word.Application")
>>>>ENDIF
>>>>
>>>>oWord.VISIBLE = .F.
>>>>loSelection = oWord.ActiveWindow.ActivePane.SELECTION
>>>>loSelection.FIND.ClearFormatting()
>>>>
>>>>WITH loSelection.FIND
>>>>	.TEXT              = "@@TEXT1"
>>>>	.Replacement.TEXT  = ""
>>>>	.Forward           = .T.
>>>>	.WRAP              = wdFindContinue
>>>>	.FORMAT            = .F.
>>>>	.MatchCase         = .F.
>>>>	.MatchWholeWord    = .F.
>>>>	.MatchWildcards    = .F.
>>>>	.MatchSoundsLike   = .F.
>>>>	.MatchAllWordForms = .F.
>>>>ENDWITH
>>>>
>>>>loSelection.FIND.TEXT  = "@@TEXT1"
>>>>loSelection.FIND.Execute()
>>>>loSelection.TypeText(TRIM(VFP_VAR1))
>>>>loSelection.GOTO(3,1,1)
>>>>
>>>>
>>>>So far its Ok, but after replacing those strings in Word I need to select ranges in those string where it starts with "%" and ends with "%%" to Format the text between "%" and "%%" to BOLD, and later remove "%" and "%%" from the document.
>>>>
>>>>Does someone knows how to select variable ranges in Word to do this ?
>>>>
>>>>Thanks in Advance
>>>>
>>>>Peter
>>>
>>>Peter,
>>>Why don't you use document variables or bookmarks instead.
>>>Cetin
>>
>>Cetin,
>>The text of the variable in the own variable can change, and its faster to build a string from many variables and strings in VFP and replace the whole as one into Word.
>>And if I have to replace "sub-variables" instead of a bigger variable, it would be necessary to keep more than 200 Word models of documents stored to just replace all the possibel combinations of text in the final Word text.
>>I just dont know how to start a range beginning at "%" and ending at "%%" to format this range to Bold.
>>I know how to find a word, or character in the document but not a range the way a need.
>>I was reading Office Automation with VFP but didnt came to something that solves the problem.
>>
>>[],s
>>Peter
>
>Peter,
>Probably you didn't understand what I'm saying. I say create a single template with named vars in it (or bookmarks). Then instead of conducting a search and replace, either use :
>
>oWord.ActiveDocument.Variables(tcVarName).value = cValue
>
>or :
>
>with oWord.ActiveDocument
> .Range(.Bookmarks(tcBookmarkName).Range.Start, ;
>        .Bookmarks(tcBookmarkName).Range.End).Text = m.tcText
>endwith
>
>
>Sample with bookmarks. Here first a template with bookmarks is created. Next a new file is created based on that template and text marked with some bookmarks are replaced with new text (however be carefull I was lazy to preserve the bookmark itself, that's for the replaced ones bookmarks are gone.)
>
>#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
>
>oWord = Createobject('Word.Application')
>With oWord
>  .Documents.Add(m.lcFileName) && New file based on template file
>  For ix=1 To Alen(aBookmarks,1)
>    With .Activedocument.Bookmarks(aBookmarks[m.ix,1]).Range
>      .Text = aBookmarks[m.ix,2]
>      .Font.Color = Rgb(255,0,0)
>      .Font.Bold = .T.
>    Endwith
>  Endfor
>  .Visible = .T.
>  .Activate
>Endwith
>**** Set the LocaleId to the previous value
>=Sys(3006,Val(nlLocaleId))
>
>PS: If you still want to use find/replace approach :
>
>.Text = "%*%%"
>.MatchWildCards = .t.
>
>Cetin

Cetin,
Thanks a lot for your advice and help with the code, it opened my mind to other ways to do what I need.

[],s
Peter
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform