Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

¿ Why lost the format ?

Status
Not open for further replies.

capri1966

Programmer
May 13, 2010
57
ES
Hi, i've an app with mailing option for my customers and it's run ok with Word 2010 or below, but when it run with Word 2013 show an error.
The code is the following:

*----------------------------------------------------------------------------------------------------------------
Parameters lcBuscar, lcReemplazo, lReemplazo

#Define wdReplaceOne 1
#Define wdFindContinue 1
#Define wdStory 6
#Define wdPasteDefault 0

oWord.Selection.HomeKey(wdStory) && Inicio del Documento
oWord.Selection.Find.ClearFormatting
With oWord.Selection.Find
oWord.Selection.HomeKey(wdStory) && Inicio del Documento
oWord.Selection.Find.ClearFormatting
With oWord.Selection.Find
.Text = Alltrim(lcBuscar)
If Len(lcReemplazo)<=254
.Replacement.Text = lcReemplazo
Endif
.Forward = .T.
.Wrap = wdFindContinue
.Format = .F.
.MatchCase = .F.
.MatchWholeWord = .F.
.MatchWildcards = .F.
.MatchSoundsLike = .F.
.MatchAllWordForms = .F.
Endwith

.Forward = .T.
.Wrap = wdFindContinue
.Format = .F.
.MatchCase = .F.
.MatchWholeWord = .F.
.MatchWildcards = .F.
.MatchSoundsLike = .F.
.MatchAllWordForms = .F.

If Len(lcReemplazo)>254
If lReemplazo
_Cliptext = lcReemplazo && Copio los datos en memoria de Windows para pegarlo en Word
If oWord.Selection.Find.Execute
oWord.Selection.Paste
Endif
Else
Return .T.
Endif
Else
If oWord.Selection.Find.Execute
If lReemplazo
#Define wdReplaceAll 2
oWord.Selection.Find.Execute ( , , , , , , , , , , wdReplaceAll)
Else
Return .T.
Endif
Else
Return .F.
Endif
Endif
Endwith
*------------------------------------------------------------------------------------------------
Is the 'wdReplaceAll' where error is shown.


On the other hand i've tried to other thing, is change the text with next code:

local cMyFile
cMyFile=''
cMyFile=getfile("doc;rtf","Procesar","Abrir",0,"Procesar documento")
if !empty(cMyFile)
oWord = CreateObject("Word.Application")
oDoc = oWord.Documents.Open(cMyFile)
oDoc.Content.FormattedText=Strtran(oDoc.Content.text,"cambiar","reemplazamos")
oWord.visible=.T.
endif

And this code change the text fine, but the text format is lost.
How i can change the text without lost the format

 
You don't seem to have a constant defined for wdReplaceAll

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
No, that's just right by the line using it:

capri1966 said:
[highlight #FCE94F]#Define wdReplaceAll 2[/highlight]
oWord.Selection.Find.Execute ( , , , , , , , , , , wdReplaceAll)

What error is shown? I fail to see where you mention the error. It's the most important message to investigate on what's going wrong, isn't it? Very generally speaking.

Bye, Olaf.

 
Well spotted Olaf!

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
It's no wonder you don't look for it elsewhere, if there already are a few #defines at the top where they all belong. And #define will be valid throughtout all the code (unless you #undef something), even over class barriers, in a PRG, so it's natural to gather all of them at the top.

I'm still curious about what error which line throws.

I wonder why you have this restriction:
Code:
If Len(lcReemplazo)<=254
.Replacement.Text = lcReemplazo
Endif

Why should a word object property be limited to store more than 254 characters. 254 characters is a limit for VFP string literals, for char fields, but not for other processes classes object properties. You might have gotten this wrong idea once you didn't used a variable lcReemplazo but a literal string. But that is not the problem of the .Replacement.Text property.

What's correct is using the Find.Execute method. Let's see how it is defined:
Code:
 .Execute(FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, 
Wrap, Format, ReplaceWith, [highlight #FCE94F]Replace[/highlight], MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl)

YXou want to use the Replace parameter, which can be any wdReplace constant, eg wdReplaceAll. To have nothing in all other parameters the FindText and ReplaceWith parameters are taken from Find.Text and Find.Replacement.Text, that's also fine, so you have ... [tt].Execute(FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, wdReplaceAll)[/tt] which in short is [tt].Execute(, , , , , , , , , , wdReplaceAll)[/tt]. 10 commas, as you have it already. all right.

I don't know what got wrong, unless you have the error message and where it occurs.

Recording a macro, what is finally recorded is [tt]Selection.Find.Execute Replace:=wdReplaceAll[/tt], which is just anther way of wrting this in VB style with named parameters, a concept VFP lacks and thus has to skip all previous parameters. And that macro works, it doesn't change formats. I tried with a bold word replaced with another one and the new word stays bold. The replacement text is merely the text without any format modifiers. The only way to override format could be using the other case in your code:
Code:
_Cliptext = lcReemplazo && Copio los datos en memoria de Windows para pegarlo en Word
If oWord.Selection.Find.Execute
oWord.Selection.Paste
Endif

Using _Cliptext you have a formatless clipboard usage on the one side, but on the other side that's perhaps making words Selection.Paste paste in the text with standard formats instead of current Selection format. Not sure, I don't have Word2013 here right now and am too lazy to go to another machine where I have it.

Bye, Olaf.
 
I've just run both versions of your code.

With the second version, I saw the same results that you did. The replacement worked perfectly, but all the formatting was lost. To be more exact, all the styles in the document reverted to the default style.

But the first version worked perfectly. So we come back to the questions that the others have asked: What was your error message? And in which line of code did it occur?

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
The error code that is shown is 1429, OLE Dispatch 0, this command is unavailable. This happens when it run with Word 2013 or above but with Word 2010 or below, it's run fine
 
Every OLE class throwing an error results in VFP Error 1427 or 1429, that in itself doesn't contain much info.
VFP Help Topic AERROR said:
When OLE errors numbered 1427 or 1429 occur, the array contains one row. The following table describes the contents of each element.
The elements 3 and 7 of the array created by AERROR contain the OLE error message and OLE exception number, which are of major interest in case of OLE errors.

So you better implement a better error handling taking care of specific error number, there also is other info for ODBC errors, you can get much better information than "OLE Dispatch", because that again just states you have some OLE exception in the typical OLE automation server classes Microsofts X.Application classes are.

I guess you don't even have any error handling and only let the native system error messagebox appear in your EXE, that's a bad decision to get a better idea about what goes wrong at runtime, not only in regard of OLE errors.

Bye, Olaf.
 
The 1429 error occurs when 'oWord.Selection.Find.Execute ( , , , , , , , , , , wdReplaceAll)' is excecuted.
I don't know why this happens but in Word 2010 runs fine
 
Well, the more precise OLE error message will give a better hint.

You wrote:
capri1966 said:
this command is unavailable

Are you sure Word2013 is automated on that computer? What is oWord.Version?

I can't believe this command not being available is the problem. The reference on the Find.Execute() call is from the online reference of Word 2013, once again:

findexecute_jzzmdq.png


You might simply need to reinstall Word2013 to fix something missing but it's more likely, you have two word versions installed and automate an older version or the error is indeed in another line of code. A more decent error handler can show this. Note that Lineno() and Lineno(1) have a difference, especially in a PRG, the one is a the absolute line no, the other the line no within a PROC or FUNC.

Bye, Olaf.
 
I agree. It would be very surprising if any command or function was dropped between Word 2010 and 2013, especially a fundamental one like Find.Execute(). It's more likely that there is something wrong with your 2013 installation.

In fact, Find.Execute() has been in the language going at least as far back as Word 2000.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I would also suggest you simply log what's going on via SET COVERAGE TO somefile.txt and then kill the process, when the error shows up, you'll see which last lines excuted before triggering your error handling from the logfile, then.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top