WORD 2007 docx: search/replace is working
Posted: Sun Nov 09, 2008 11:08 pm
I did some tests with the new Word Open XML format and have
"search replace" working.
This is what I did:
1. rename from docx to zip
2. unpack
3. Seek and replace in the document.xml for placeholders
4. Zip again the files and rename back to docx.
Walkthrough: Word 2007 XML Format
http://msdn.microsoft.com/en-us/library/ms771890.aspx
Regards,
Otto
"search replace" working.
This is what I did:
1. rename from docx to zip
2. unpack
3. Seek and replace in the document.xml for placeholders
4. Zip again the files and rename back to docx.
Walkthrough: Word 2007 XML Format
http://msdn.microsoft.com/en-us/library/ms771890.aspx
Regards,
Otto
Code: Select all
#include "FiveWin.ch"
#INCLUDE "directry.ch"
//----------------------------------------------------------------------------//
function Main()
local DCOM
local cTxtFile
local cRCFile
local cPath := "c:\soft" // is a tmp directry to expand the docx file
local aFiles
DELETE FILE ('archive.zip')
DELETE FILE ('temp.zip')
// clear the directory - sure there is a better way
do while .t.
aFiles := directory( cPath + '\*.*', 'D' )
if len(aFiles) > 2
dir_recurs( cPath )
else
exit
endif
enddo
MOVEFILE( "demo.docx" , "temp.zip" )
SYSREFRESH()
//extracts all *.* files from the archive archive.zip to c:\soft folder.
// 7Z.exe - thanks to Richard - maybe someone can help with the build in function
DCOM := '7Z.exe x ' + "temp.zip" + " -oc:\soft *.* -r"
WAITRUN(DCOM,0)
SYSREFRESH()
cRCFile := "c:\soft\word\document.xml"
cTxtFile = MemoRead( cRCFile )
SYSREFRESH()
cTxtFile := STRTRAN(cTxtFile, "[ANREDE]", "Herr")
cTxtFile := STRTRAN(cTxtFile, "[TITELVORNAMENAME]", "Dr. Mustermann Hans")
cTxtFile := STRTRAN(cTxtFile, "[BRIEFANRED]", "Lieber Hans")
cTxtFile := STRTRAN(cTxtFile, "[STRASSE]", "Bahnhofstrasse")
cTxtFile := STRTRAN(cTxtFile, "[ORT]", "Musterort" )
//xHarbours memowrit adds a chr(26) - therfore I linked the harbour function here
memowrit(cRCFile, cTxtFile,.f. )
SYSREFRESH()
//adds all files and subfolders from folder subdir to archive archive.zip.
DCOM := '7z a -tzip archive.zip -ir!c:\soft\*.*'
WAITRUN(DCOM,0)
SYSREFRESH()
MOVEFILE("archive.zip", "demo.docx" )
SYSREFRESH()
msginfo("Ende")
return nil
//----------------------------------------------------------------------------//
static function dir_recurs( cPath )
local x
local aFiles := directory( cPath + '\*.*', 'D' )
local nFilCount := len( aFiles )
dirremove(cPath)
for x := 1 to nFilCount
if aFiles[ X, F_NAME ] <> '..'
ferase (cPath + "\" + aFiles[ X, F_NAME ])
endif
if 'D' $ aFiles[ X, F_ATTR ]
if aFiles[ X, F_NAME ] <> '.'
dir_recurs( cPath + '\' + aFiles[ X, F_NAME ] )
endif
endif
next
return NIL
//----------------------------------------------------------------------------//
#pragma BEGINDUMP
#include <hbapi.h>
#include <hbapiitm.h>
#include "hbapifs.h"
HB_FUNC( MEMOWRIT )
{
PHB_ITEM pFileName = hb_param( 1, HB_IT_STRING );
PHB_ITEM pString = hb_param( 2, HB_IT_STRING );
BOOL bWriteEof = TRUE; /* write Eof !, by default is .T. */
BOOL bRetVal = FALSE;
if( hb_parinfo(0) == 3 && ISLOG( 3 ) )
bWriteEof = hb_parl( 3 );
if( pFileName && pString )
{
FHANDLE fhnd = hb_fsCreate( ( BYTE * ) hb_itemGetCPtr( pFileName ), FC_NORMAL );
if( fhnd != FS_ERROR )
{
ULONG ulSize = hb_itemGetCLen( pString );
bRetVal = ( hb_fsWriteLarge( fhnd, ( BYTE * ) hb_itemGetCPtr( pString ), ulSize ) == ulSize );
/* NOTE: CA-Clipper will add the EOF even if the write failed. [vszakats] */
/* NOTE: CA-Clipper will not return .F. when the EOF could not be written. [vszakats] */
#if ! defined(OS_UNIX_COMPATIBLE)
{
if( bWriteEof ) /* if true, then write EOF */
{
BYTE byEOF = HB_CHAR_EOF;
hb_fsWrite( fhnd, &byEOF, sizeof( BYTE ) );
}
}
#endif
hb_fsClose( fhnd );
}
}
hb_retl( bRetVal );
}
#pragma ENDDUMP
//----------------------------------------------------------------------------//