Winword and OLE
-
- Posts: 310
- Joined: Mon Oct 10, 2005 5:10 am
Winword and OLE
Hi All
I have office 2010 and would lilke to open a word document and replace some text using
OLE
Can anyone provide a small sample.
Regards
Colin
I have office 2010 and would lilke to open a word document and replace some text using
OLE
Can anyone provide a small sample.
Regards
Colin
- Richard Chidiak
- Posts: 946
- Joined: Thu Oct 06, 2005 7:05 pm
- Location: France
- Contact:
Re: Winword and OLE
Colin
This is a sample, the code was originated by Enrico, i adapted it to my needs
HTH
Richard
This is a sample, the code was originated by Enrico, i adapted it to my needs
HTH
Richard
Code: Select all
TRY
oWORD := CREATEOBJECT( "word.Application" )
CATCH
MSGSTOP("L'Application Microsoft WORD n'est pas installée sur cet Ordinateur !" )
RETURN NIL
END
oDoc = oWord:Documents:Open(CFILE )
oDoc:Select()
oSel = oWord:Selection
WORDREPLACE( oSel, "#nom#", ALLTRIM(DNOM))
oWord:ActivePrinter = CNAME
oDoc:PrintOut()
oDoc:Close( 0 )
oWord:Quit()
STATIC FUNCTION WORDREPLACE( oSel, cSrc, cRpl )
oSel:Start = 0
oSel:End = -1
WHILE oSel:Find:Execute( cSrc )
oSel:Range:Text = cRpl
ENDDO
RETURN NIL
-
- Posts: 310
- Joined: Mon Oct 10, 2005 5:10 am
Re: Winword and OLE
Hi Richard
Thanks for your reply - I get the following error
Error description: Error word.Application:DOCUMENTS/9 'Item' is not a property.: OPEN
Stack Calls
===========
Called from: C:\xHarbour\source\rtl\win32ole.prg => TOLEAUTO:OPEN(0)
All the samples I have found use the same code as you provided.
Regards
Colin
Thanks for your reply - I get the following error
Error description: Error word.Application:DOCUMENTS/9 'Item' is not a property.: OPEN
Stack Calls
===========
Called from: C:\xHarbour\source\rtl\win32ole.prg => TOLEAUTO:OPEN(0)
All the samples I have found use the same code as you provided.
Regards
Colin
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Winword and OLE
This is an improved version that I'm using currently:
EMG
Code: Select all
FUNCTION WORDREPLACEALL( oSel, cSrc, cRpl )
LOCAL oRng := oSel:Document:Content
IF AT( cSrc, oRng:Text ) = 0; RETURN .F.; ENDIF
WHILE oRng:Find:Execute( cSrc )
oRng:Text = cRpl
oRng:Collapse( wdCollapseEnd )
ENDDO
RETURN .T.
-
- Posts: 310
- Joined: Mon Oct 10, 2005 5:10 am
Re: Winword and OLE
Hi All
The document I was trying to open was a docx - I tried a another doc document and
it openned.
Cheers
Colin
The document I was trying to open was a docx - I tried a another doc document and
it openned.
Cheers
Colin
Re: Winword and OLE
To me this is a very interesting topic.
But I have 2 more questions.
1. (To Enrico) I tried your example of WordReplace(), but I got an error that the variable WDCOLLAPSEND doesn't exist. Any idea ? The original code, proposed in this topic, is working just fine.
2. How can I search for a character, a word or a sentence, using this idea ?
3. Is there any difference between : DO WHILE ..... ENDDO and WHILE ..... ENDDO ?
Thank you very much in advance.
But I have 2 more questions.
1. (To Enrico) I tried your example of WordReplace(), but I got an error that the variable WDCOLLAPSEND doesn't exist. Any idea ? The original code, proposed in this topic, is working just fine.
2. How can I search for a character, a word or a sentence, using this idea ?
3. Is there any difference between : DO WHILE ..... ENDDO and WHILE ..... ENDDO ?
Thank you very much in advance.
Regards,
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Winword and OLE
driessen wrote:I got an error that the variable WDCOLLAPSEND doesn't exist.
Code: Select all
#define wdCollapseEnd 0
driessen wrote:How can I search for a character, a word or a sentence, using this idea ?
Code: Select all
IF oRng:Find:Execute( cString )
// Found!
ENDIF
No.driessen wrote:Is there any difference between : DO WHILE ..... ENDDO and WHILE ..... ENDDO ?
EMG
Re: Winword and OLE
Enrico,
Thank you very much for your answer.
Thank you very much for your answer.
Regards,
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
Re: Winword and OLE
Enrico,
Maybe just another question.
I noticed that the text is not replaced in de header and footer. Any idea how that can be solved ?
Thanks a lot in advance.
Maybe just another question.
I noticed that the text is not replaced in de header and footer. Any idea how that can be solved ?
Thanks a lot in advance.
Regards,
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Winword and OLE
Code: Select all
FUNCTION WORDREPLACERNGALL( oDoc, cSrc, cRpl )
LOCAL lOk := .F.
LOCAL oRng
TRY
oRng = oDoc:StoryRanges[ 7 ]
IF AT( cSrc, oRng:Text ) > 0
WHILE oRng:Find:Execute( cSrc )
oRng:Text = cRpl
oRng = oDoc:StoryRanges[ 7 ]
ENDDO
lOk = .T.
ENDIF
CATCH
END
RETURN lOk
Re: Winword and OLE
Enrico,
Thanks a lot.
I'll try it out this weekend.
Thanks a lot.
I'll try it out this weekend.
Regards,
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
Re: Winword and OLE
Enrico,
I tried you last example. Unfortunately I got an error : Error Word.Application:SELECTION/0 S_OK:STORYRANGES.
Does STORYRANGES need to be defined ? If yes, how ?
Thank you in advance.
I tried you last example. Unfortunately I got an error : Error Word.Application:SELECTION/0 S_OK:STORYRANGES.
Does STORYRANGES need to be defined ? If yes, how ?
Thank you in advance.
Regards,
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Winword and OLE
You can't have errors using my sample because the access to StoryRange is enclosed in a TRY/CATCH/END structure.
EMG
EMG
Re: Winword and OLE
Enrico,
You are right. But I tried your example first and noticed that my Word-document wasn't changed at all.
So I did remark the TRY ... CATCH ... END lines. And then that error occurred. To my opinion the reason why my document wasn't changed ?
You are right. But I tried your example first and noticed that my Word-document wasn't changed at all.
So I did remark the TRY ... CATCH ... END lines. And then that error occurred. To my opinion the reason why my document wasn't changed ?
Regards,
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 21.01 - Harbour 3.2.0 (October 2020) - xHarbour Builder (January 2020) - Bcc7
- Enrico Maria Giordano
- Posts: 7355
- Joined: Thu Oct 06, 2005 8:17 pm
- Location: Roma - Italia
- Contact:
Re: Winword and OLE
Please show a reduced sample of the problem so I can try to fix it.
EMG
EMG