/* MW_InsertTextFromTxtFile.prg Sample to instert text from txt file with MS Word file. Using HB32, Windows10, MS Word 2007. Compile with -lhbwin -lhbmisc This code is a basic sample for tests. Copy the constants files from my web site. web : http://bernard.mouille.free.fr/mso-hb32/MW_InsertTextFromTxtFile.txt Last change : 2023-06-03 */ // Parameter. #define BM_FILE_WORD hb_dirbase() + "_Result_Word.docx" // File to create with his path. #define BM_FILE_TXT hb_dirbase() + "_Result_Txt.txt" // File to insert in the document. #include "mw_WordConstants.h" // http://bernard.mouille.free.fr/mso-hb32/mw_WordConstants.h #include "bh_bgrColors.h" // http://bernard.mouille.free.fr/mso-hb32/bh_bgrColors.h procedure Main local i // Count number. local oWord // Word object. local oDoc // Document object. local oSele // Selection object. local aLines // Text array lines to insert setmode( 25, 80 ) setcolor( "GR+/B" ) @ 0, 0, maxrow(), maxcol() box space( 9 ) ? "Sample to instert text from txt file with MS Word file." ferase( BM_FILE_WORD ) CreateTestTxtFile( BM_FILE_TXT ) oWord := win_olecreateobject( "Word.Application" ) oWord:Visible := .T. oDoc := oWord:documents:add() oWord:ActiveDocument:Select() // Option : Page number at the end of page. oDoc:Sections( 1 ):Footers( wdHeaderFooterPrimary ):PageNumbers:Add() // Option : Margins. oDoc:PageSetup:LeftMargin := oWord:Application:CentimetersToPoints( 1 ) oDoc:PageSetup:RightMargin := oWord:Application:CentimetersToPoints( 1 ) oDoc:PageSetup:TopMargin := oWord:Application:CentimetersToPoints( 1 ) oDoc:PageSetup:BottomMargin := oWord:Application:CentimetersToPoints( 1 ) oSele := oWord:Selection oWord:Selection:typetext( "Instert text from txt file with MS Word file" ) oWord:Selection:InsertBreak( wdLineBreak ) oSele:TypeParagraph() oSele:typetext( "The blue text below is inserted from :" ) oWord:Selection:InsertBreak( wdLineBreak ) oSele:typetext( BM_FILE_TXT ) oWord:Selection:InsertBreak( wdLineBreak ) // Insert text. oSele:TypeParagraph() oWord:Selection:Font:Color := bgrBlue aLines := bh_FileToArray( BM_FILE_TXT ) for i := 1 to len( aLines ) oSele:typetext( aLines[ i ] ) if i < len( aLines ) oWord:Selection:InsertBreak( wdLineBreak ) endif endfor oWord:Selection:InsertBreak( wdLineBreak ) oSele:TypeParagraph() oWord:Selection:Font:Color := bgrBlack oSele:typetext( "End of instertion." ) oDoc:SaveAs( BM_FILE_WORD ) // oDoc:Close( .T. ) // oWord:Quit() oWord := nil return procedure CreateTestTxtFile( cFile ) local cLine := "" if file( cFile ) return endif cLine += "This is the first line." + hb_eol() cLine += "Read at : https://en.wikipedia.org/wiki/History_of_Microsoft_Word" + hb_eol() cLine += "The first version of Microsoft Word was developed by Charles Simonyi and Richard Brodie, former Xerox programmers hired by Bill Gates and Paul Allen in 1981." + hb_eol() cLine += "This is the last line." hb_memowrit( cFile, cLine ) return // Transform a file to array ( compile with -lhbmisc ). function bh_FileToArray( cFile ) local aArray := {} local cLine if .not. file( cFile ) return aArray endif cLine := hb_memoread( cFile ) // For unix end of line. if hb_at( chr( 10 ), cLine ) > 0 .and. hb_at( chr( 13 ), cLine ) == 0 cLine := strtran( cLine, chr( 10 ), chr( 13 ) + chr( 10 ) ) hb_memowrit( cFile, cLine ) endif cLine := nil hb_fuse( cFile ) do while .not. hb_feof() aadd( aArray, hb_freadln() ) hb_fskip() enddo hb_fuse() return aArray