// Find objects with constants /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ This DXL search text in an OLE in the Object Text This program ignores objects with multiple OLEs. It assumes that the OLE is first order (i.e., does not itself contain an embedded OLE). Excel does not provide a method for searching a chart, so such OLEs are added to an error string in this example Static and unregistered OLEs cannot be activated and searched, so they are added to an error string in this example This example only searches in OLEs from Word or Excel. It is possible to expand its capabilities to other applications. It is written for Office 2003, but should still work for Office 2007. ------------------------------------------------------------------------------*/ const int xlWorksheet = -4167 const int xlFormulas = -4123 Module m Object o string sName string sErr bool bIsActivated bool bFoundString bool bIsStatic bool bIsUnregistered bool bNextrequired int iObjMax, iCount, iType OleAutoObj objDoc OleAutoObj objApp OleAutoObj objSelection, objSelection2 OleAutoObj objFind OleAutoObj objReplacement OleAutoArgs objArgs = create string sError = "" string sSearchText = "" string sReplaceText = "" int iNumberOfOles string sObjId string getUserInput(string Title) { DB db DBE input string s void closeCB(DB db) { release(db) } db = create("User interaction", styleCentered) input = field(db, Title, "", 30, false) apply(db, "OK", closeCB) close(db, false, closeCB) block(db) s = get(input) destroy(db) return(s) } m = current if (null m) { errorBox "This function must be run from within a module" halt } sSearchText = getUserInput("Word to be found in OLE") sReplaceText = getUserInput("Word to replaced with in OLE") iObjMax = 0 iCount = 0 bNextrequired = false for o in m do iObjMax++ progressStart(dbExplorer, "Find Constants in OLEs", "Something", iObjMax) for o in m do { iCount++ progressStep iCount sObjId = identifier o "" progressMessage "Processing " iCount " of " iObjMax " (" sObjId ")" iNumberOfOles = oleCount(o."Object Text") if (iNumberOfOles > 1) { sError = sError "Object " sObjId " has more than one OLE\n" continue } if (iNumberOfOles == 0) continue // only one OLE, so let's process it. bIsStatic = containsStaticOle(o."Object Text") bIsUnregistered = containsUnregisteredOle(o."Object Text") if (bIsStatic) { bIsActivated = false } else if (bIsUnregistered) { bIsActivated = false } else bIsActivated = oleActivate(o) if (!bIsActivated) { sError = sError "Unable to activate OLE for object " sObjId ": " if (bIsStatic) sError = sError "It is Static\n" else if (bIsUnregistered) sError = sError "It is Unregistered\n" else sError = sError "Reason unknown\n" continue } objDoc = oleGetAutoObject(o) if (null objDoc) { sError = sError "Unable to get document handle for OLE object in " sObjId "\n" if bIsActivated then oleDeactivate(o) oleCloseAutoObject objDoc continue } sErr = oleGet(objDoc, "Application", objApp) if (!null sErr) { sError = sError "Cannot get application object for OLE in " sObjId "\n" oleDeactivate(o) continue } if (null objApp) { sError = sError "Unable to get application for activated document in " sObjId "\n" if bIsActivated then oleDeactivate(o) continue } oleGet(objApp, "Name", sName) if (matches("Word", sName)) { bFoundString = false oleGet(objApp, "Selection", objSelection) oleGet(objSelection, "Find", objFind) oleMethod(objFind, "ClearFormatting") oleGet(objFind, "Replacement", objReplacement) oleMethod(objReplacement, "ClearFormatting") olePut(objFind, "Replacement.Text", sReplaceText) olePut(objFind, "Wrap", "wdFindContinue") olePut(objFind, "Text", sSearchText) olePut(objFind, "MatchWholeWord", true) olePut(objFind, "Format", false) clear objArgs put(objArgs, "Replace", "wdReplaceAll") oleMethod(objFind, "Execute", objArgs) } else { sError = sError "Application " sName " not currently supported\n" } if bIsActivated then oleDeactivate(o) } progressStop if (sError != "") warningBox sError delete objArgs oleCloseAutoObject(objDoc) oleCloseAutoObject(objApp) oleCloseAutoObject(objSelection) oleCloseAutoObject(objFind) oleCloseAutoObject(objReplacement)