1 '/*************************************************************************
3 ' * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 ' * Copyright 2008 by Sun Microsystems, Inc.
7 ' * OpenOffice.org - a multi-platform office productivity suite
9 ' * $RCSfile: DocAnalysisRunMacro.vbs,v $
10 ' * $Revision: 1.2.140.1 $
12 ' * This file is part of OpenOffice.org.
14 ' * OpenOffice.org is free software: you can redistribute it and/or modify
15 ' * it under the terms of the GNU Lesser General Public License version 3
16 ' * only, as published by the Free Software Foundation.
18 ' * OpenOffice.org is distributed in the hope that it will be useful,
19 ' * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ' * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ' * GNU Lesser General Public License version 3 for more details
22 ' * (a copy is included in the LICENSE file that accompanied this code).
24 ' * You should have received a copy of the GNU Lesser General Public License
25 ' * version 3 along with OpenOffice.org. If not, see
26 ' * <http://www.openoffice.org/license.html>
27 ' * for a copy of the LGPLv3 License.
29 ' ************************************************************************/
30 '### Support Module for running macros in Word. Excel and Powerpoint
33 CONST CDA_TITLE
= "Document Analysis Run Macro"
34 CONST CDA_ANALYSIS_INI
= "analysis.ini"
35 Const CDA_ERR_STD_DELAY
= 10
36 Const CDA_APPNAME_WORD
= "Word"
37 Const CDA_APPNAME_EXCEL
= "Excel"
38 Const CDA_APPNAME_POWERPOINT
= "Powerpoint"
52 '# Setup Scripting objects
53 set daFso
= WScript
.CreateObject("Scripting.FileSystemObject")
54 set daWshShell
= Wscript
.CreateObject("Wscript.Shell")
57 '##### Run Macro FUNCTIONS ######
59 '######################
60 Sub DASetTitle(newTitle
)
64 '######################
69 Set daWrd
= wscript
.CreateObject("Word.Application")
70 If Err
.Number
<> 0 Then
71 DAErrMsg
"Failed to create Word Automation server: " & vbLf
& vbLf
& "Error: " _
72 & CStr(Err
.Number
) & " " & Err
.Description
, CDA_ERR_STD_DELAY
78 '######################
79 Sub DAOpenWrdDriver(driver
)
80 Dim sWordDriverDocPath
86 sWordDriverDocPath
= daFso
.GetAbsolutePathName(driver
)
87 'DAdiagMsg "sWordDriverDocPath : " & sWordDriverDocPath , CDIAG_STD_DELAY
89 If Not daFso
.FileExists(sWordDriverDocPath
) Then
90 DAErrMsg
"Driver doc does not exist: " & sWordDriverDocPath
, CDA_ERR_STD_DELAY
94 Set daDoc
= daWrd
.Documents
.Open(sWordDriverDocPath
)
95 If Err
.Number
<> 0 Then
96 DAErrMsg
"Failed to open driver doc: " & vbLf
& sWordDriverDocPath
& vbLf
& vbLf
& "Error: " _
97 & CStr(Err
.Number
) & " " & Err
.Description
, CDA_ERR_STD_DELAY
103 '######################
104 Function DArunWrdDriver(driver
, macro
)
108 DArunWrdDriver
= True
109 daWrd
.Run ("AnalysisTool." & macro
)
110 If Err
.Number
<> 0 Then
111 DAErrMsg
"Failed to run macro: " & macro
& vbLf
& vbLf
& "Error: " _
112 & CStr(Err
.Number
) & " " & Err
.Description
, CDA_ERR_STD_DELAY
113 DArunWrdDriver
= False
118 '######################
119 Sub DAsaveWrdDriver(saveDriver
)
120 'DAdiagMsg "saveDriver : " & saveDriver , CDIAG_STD_DELAY
121 'DAdiagMsg "Abs(saveDriver) : " & daFso.GetAbsolutePathName( saveDriver) , CDIAG_STD_DELAY
122 daDoc
.SaveAs daFso
.GetAbsolutePathName( saveDriver
)
125 '######################
126 Sub DAsetupExcelServer
130 Set daXl
= wscript
.CreateObject("Excel.Application")
131 If Err
.Number
<> 0 Then
132 DAErrMsg
"Failed to create Excel Automation server: " & vbLf
& vbLf
& "Error: " _
133 & CStr(Err
.Number
) & " " & Err
.Description
, CDA_ERR_STD_DELAY
139 '######################
140 Sub DAOpenExcelDriver(driver
)
141 Dim sExcelDriverDocPath
147 sExcelDriverDocPath
= daFso
.GetAbsolutePathName(driver
)
148 If Not daFso
.FileExists(sExcelDriverDocPath
) Then
149 DAErrMsg
"Driver doc does not exist: " & sExcelDriverDocPath
, CDA_ERR_STD_DELAY
153 Set daWb
= daXl
.Workbooks
.Open(sExcelDriverDocPath
)
154 If Err
.Number
<> 0 Then
155 DAErrMsg
"Failed to open driver doc: " & vbLf
& sExcelDriverDocPath
& vbLf
& vbLf
& "Error: " _
156 & CStr(Err
.Number
) & " " & Err
.Description
, CDA_ERR_STD_DELAY
162 '######################
163 Function DArunExcelDriver(driver
, macro
)
167 DArunExcelDriver
= True
168 daXl
.Run ("AnalysisTool." & macro
)
169 If Err
.Number
<> 0 Then
170 DAErrMsg
"Failed to run macro: " & macro
& vbLf
& vbLf
& "Error: " _
171 & CStr(Err
.Number
) & " " & Err
.Description
, CDA_ERR_STD_DELAY
172 DArunExcelDriver
= False
177 '######################
178 Sub DAsaveExcelDriver(saveDriver
)
179 '# Not overwritting - Excel hangs, need to remove file first
180 if daFso
.FileExists(daFso
.GetAbsolutePathName(saveDriver
)) Then
181 daFso
.DeleteFile(daFso
.GetAbsolutePathName(saveDriver
))
183 daWb
.SaveAs daFso
.GetAbsolutePathName(saveDriver
)
186 '######################
191 Set daPP
= wscript
.CreateObject("PowerPoint.Application")
192 If Err
.Number
<> 0 Then
193 DAErrMsg
"Failed to create PowerPoint Automation server: " & vbLf
& vbLf
& "Error: " _
194 & CStr(Err
.Number
) & " " & Err
.Description
, CDA_ERR_STD_DELAY
200 '######################
201 Sub DAOpenPPDriver(driver
)
208 sPPDriverDocPath
= daFso
.GetAbsolutePathName(driver
)
209 If Not daFso
.FileExists(sPPDriverDocPath
) Then
210 DAErrMsg
"Driver doc does not exist: " & sPPDriverDocPath
, CDA_ERR_STD_DELAY
215 '## MS: KB Article 155073 ##
216 '# PPT7: OLE Automation Error Using Open Method
217 '# MUST show the PowerPoint application window at least once before calling the Application.Presentations.Open method
219 daPP
.WindowState
= 2 'Minimize PowerPoint
221 daPP
.Presentations
.Open sPPDriverDocPath
222 If Err
.Number
<> 0 Then
223 DAErrMsg
"Failed to open driver doc: " & vbLf
& sPPDriverDocPath
& vbLf
& vbLf
& "Error: " _
224 & CStr(Err
.Number
) & " " & Err
.Description
, CDA_ERR_STD_DELAY
228 set daPres
= daPP
.Presentations(1)
232 '######################
233 Function DArunPPDriver(driver
, macro
)
238 daPP
.Run (daFso
.GetFileName(driver
) & "!" & macro
)
239 If Err
.Number
<> 0 Then
240 DAErrMsg
"Failed to run macro: " & macro
& vbLf
& vbLf
& "Error: " _
241 & CStr(Err
.Number
) & " " & Err
.Description
, CDA_ERR_STD_DELAY
242 DArunPPDriver
= False
247 '######################
248 Sub DAsavePPDriver(saveDriver
)
249 daPres
.SaveAs daFso
.GetAbsolutePathName(saveDriver
)
253 '######################
258 If Not daWrd Is
Nothing Then
259 daDoc
.Close wdDoNotSaveChanges
262 If Not daXl Is
Nothing Then
266 If Not daPP Is
Nothing Then
280 '######################
289 Set daWshShell
= Nothing
293 '######################
294 Sub DAdiagMsg( msg
, delay
)
295 '# WSHShell.echo: Popup if run with Wscript.exe, command line output if run with Cscript.exe
298 'WSHShell.popup msg, delay, daTitle, 64
301 '######################
302 Sub DAErrMsg( msg
, delay
)
303 daWshShell
.Popup msg
, delay
, daTitle
, 16
308 '######################
309 Sub DAVerifyAnalysisIni()
310 if daFso
.FileExists(daFso
.GetAbsolutePathName(".\" & CDA_ANALYSIS_INI
)) Then Exit Sub
312 DAErrMsg CDA_ANALYSIS_INI
& " does not exist. " & vbLf
& vbLf
& _
313 "You need to create it manually or use the DocAnalysisWizard to create one for you." & vbLf
& _
314 "Once this is done you can rerun the Document Analysis command line.", CDA_ERR_STD_DELAY
318 '######################
319 Sub DAExportFile(fileName
, projectFile
, app_name
)
324 '# Setup App Specifc VB Project
325 Set myProject
= DAgetProject(fileName
, projectFile
, app_name
)
328 Set myComponent
= myProject
.VBComponents(projectFile
)
329 If Err
.Number
<> 0 Then
330 DAErrMsg
"Missing Project File [" & projectFile
& "] - Path:" & vbLf
& vbLf
& fileName
, CERR_STD_DELAY
331 Set myComponent
= Nothing
332 Set myProject
= Nothing
336 myProject
.VBComponents(projectFile
).Export fileName
337 If Err
.Number
<> 0 Then
338 DAErrMsg
"Error exporting Project File [" & projectFile
& "] - Path:" & vbLf
& vbLf
& fileName
, CERR_STD_DELAY
339 Set myComponent
= Nothing
340 Set myProject
= Nothing
344 Set myComponent
= Nothing
345 Set myProject
= Nothing
349 '######################
350 Sub DAImportFile(fileName
, projectFile
, app_name
)
355 '# Setup App Specifc VB Project
356 Set myProject
= DAgetProject(fileName
, projectFile
, app_name
)
358 '# Check if module already exists raise error
360 Set myComponent
= myProject
.VBComponents(projectFile
)
361 If Err
.Number
= 0 Then
362 DAErrMsg
"Duplicate Project File [" & projectFile
& "] - Path:" & vbLf
& vbLf
& fileName
, CERR_STD_DELAY
363 Set myComponent
= Nothing
364 Set myProject
= Nothing
368 '#If module not there need to clear out of index error
371 If Not daFso
.FileExists(fileName
) Then
372 DAErrMsg
"Missing File " & fileName
, CERR_STD_DELAY
373 Set myComponent
= Nothing
374 Set myProject
= Nothing
378 Call myProject
.VBComponents
.Import(fileName
)
380 If Err
.Number
<> 0 Then
381 DAErrMsg
"Error importing Project File [" & projectFile
& "] - Path:" & vbLf
& vbLf
& fileName
, CERR_STD_DELAY
382 Set myComponent
= Nothing
383 Set myProject
= Nothing
387 Set myComponent
= Nothing
388 Set myProject
= Nothing
393 Sub DARemoveModule(fileName
, projectFile
, app_name
)
398 '# Setup App Specifc VB Project
399 Set myProject
= DAgetProject(fileName
, projectFile
, app_name
)
401 '# Check if module already exists raise error
403 Set myComponent
= myProject
.VBComponents(projectFile
)
406 myProject
.VBComponents
.Remove myComponent
408 If Err
.Number
<> 0 Then
409 DAErrMsg
"Error removing Project File [" & projectFile
& "] - Path:" & vbLf
& vbLf
& fileName
, CERR_STD_DELAY
410 Set myComponent
= Nothing
411 Set myProject
= Nothing
415 Set myComponent
= Nothing
416 Set myProject
= Nothing
419 '######################
420 Function DAgetProject(fileName
, projectFile
, app_name
)
423 If app_name
= CDA_APPNAME_WORD
Then
424 Set DAgetProject
= daWrd
.ActiveDocument
.VBProject
426 ElseIf app_name
= CDA_APPNAME_EXCEL
Then
427 Set DAgetProject
= daXl
.ActiveWorkbook
.VBProject
429 ElseIf app_name
= CDA_APPNAME_POWERPOINT
Then
430 Set DAgetProject
= daPP
.ActivePresentation
.VBProject
433 If Err
.Number
<> 0 Then
434 DAErrMsg
"Cannot access VBProject for Project File [" & projectFile
& "] - Path:" & vbLf
& vbLf
& fileName
, _
436 Set DAgetProject
= Nothing