Hello Silvester,
A basic sample below my signature.
My codes arent basic samples in Harbour 32.
Modify that it do not works if you use another tool that Harbour 32.
Regards,
Bernard.
// Begin code.
/*
MW_ReplaceImagesWithText.prg
Sample to replace images with text in Word file.
Using HB32, Windows10, MS Word 2007.
Compile with -lhbwin
This code is a basic sample for tests.
Copy the constants files from my web site.
local oShape // Shape and InlineShape object.
local Word_File // File to extract images.
local oSentence // Sentence object.
local aStentences // Array with the sentences positions.
local nShape // Shape while number.
local nSentence // Sentences while number.
setmode( 25, 80 )
setcolor( "GR+/B" )
@ 0, 0, maxrow(), maxcol() box space( 9 )
ferase( BM_FILE_LOG )
ferase( BM_FILE_WORD )
set( _SET_EOF , .F. )
set( _SET_ALTFILE , BM_FILE_LOG )
set( _SET_ALTERNATE, .T. )
? "Sample to replace images with text in Word file."
?
Word_File := win_GetOpenFileName( , "Select a Word file",,, { "Word files ; *.docx" } )
if .not. file( Word_File )
wapi_MessageBox( 0, "Word file not found, abort." )
return
endif
hb_fcopy( Word_File, BM_FILE_WORD )
oWord := Win_OleCreateObject( "Word.Application" )
oWord:Visible := .T.
oDoc := oWord:documents:open( BM_FILE_WORD )
? "Puts all the document sentences to an array for the shapes."
?
aStentences := {}
for each oSentence in oDoc:Sentences
aadd( aStentences, { oSentence:information( wdActiveEndAdjustedPageNumber ) ;
, oSentence:information( wdVerticalPositionRelativeToPage ) ;
, oSentence:information( wdHorizontalPositionRelativeToPage ) } )
? oSentence:information( wdActiveEndAdjustedPageNumber ), " "
?? oSentence:information( wdVerticalPositionRelativeToPage ), " "
?? oSentence:information( wdHorizontalPositionRelativeToPage )
endfor
?
? "Replace Shapes with text."
?
for nShape := oDoc:Shapes:Count to 1 step - 1
oShape := oDoc:Shapes( nShape )
if oShape:Type == msoLinkedPicture .or. oShape:Type == msoPicture
oShape:Select()
for nSentence := 1 to len( aStentences )
if aStentences[ nSentence, 1 ] == oWord:Selection:information( wdActiveEndAdjustedPageNumber ) .and. ;
aStentences[ nSentence, 2 ] == oWord:Selection:information( wdVerticalPositionRelativeToPage ) .and. ;
aStentences[ nSentence, 3 ] == oWord:Selection:information( wdHorizontalPositionRelativeToPage )
? oWord:Selection:information( wdActiveEndAdjustedPageNumber ), " "
?? oWord:Selection:information( wdVerticalPositionRelativeToPage ), " "
?? oWord:Selection:information( wdHorizontalPositionRelativeToPage )
oDoc:Sentences( nSentence ):Text := "*** Replaced Shape image number " + hb_ntos( nShape ) + " *** " + chr( 10 )
exit
endif
endfor
endif
endfor
endif
?
if oDoc:InlineShapes:Count > 0
? "Puts all the document sentences to an array for the InlineShapes."
?
aStentences := {}
for each oSentence in oDoc:Sentences
aadd( aStentences, { oSentence:information( wdActiveEndAdjustedPageNumber ) ;
, oSentence:information( wdVerticalPositionRelativeToPage ) ;
, oSentence:information( wdHorizontalPositionRelativeToPage ) } )
? oSentence:information( wdActiveEndAdjustedPageNumber ), " "
?? oSentence:information( wdVerticalPositionRelativeToPage ), " "
?? oSentence:information( wdHorizontalPositionRelativeToPage )
endfor
?
? "Replace InlineShapes with text"
?
for nShape := oDoc:InlineShapes:Count to 1 step - 1
oShape := oDoc:InlineShapes( nShape )
if oShape:Type == wdInlineShapePicture
for nSentence := 1 to len( aStentences )
if aStentences[ nSentence, 1 ] == oShape:Range:information( wdActiveEndAdjustedPageNumber ) .and. ;
aStentences[ nSentence, 2 ] == oShape:Range:information( wdVerticalPositionRelativeToPage ) .and. ;
aStentences[ nSentence, 3 ] == oShape:Range:information( wdHorizontalPositionRelativeToPage )
? oShape:Range:information( wdActiveEndAdjustedPageNumber ), " "
?? oShape:Range:information( wdVerticalPositionRelativeToPage ), " "
?? oShape:Range:information( wdHorizontalPositionRelativeToPage )
oDoc:Sentences( nSentence ):Text := "*** Replaced InlineShape image number " + hb_ntos( nShape ) + " *** " + chr( 10 )
exit
endif
endfor
endif
endfor
endif
oDoc:Save()
// oDoc:Close( wdDoNotSaveChanges )
// oWord:Quit()
oWord := nil
?
set( _SET_ALTERNATE, .F. )
set( _SET_ALTFILE , "" )
return
// End code.