Jump to content
Sign in to follow this  
jcaspar

[..] Word Modeles informations

Recommended Posts

jcaspar

Hello Autoit Friends

I'm working on a script  that should allow me to find informations about the modeles used by Word files in a directory

my script is partially  functional just i don't find how  to manage files in a directory in order to get a complete listing

Could you give me some advices to resolve my troubles

Thanks a lot for your advices !

 

[spoiler=]

#include <Debug.au3>

#include <Array.au3>

#include <FileConstants.au3>

#include <MsgBoxConstants.au3>

#include <Word.au3>

#include <File.au3>

#include <Array.au3>
 
 
 
Opt("MustDeclareVars", 0)
; List files in a directory
 
  Local $aFileList = _FileListToArray("p:\SG\INFOR", "*.docx")

    If @error = 1 Then

        MsgBox($MB_SYSTEMMODAL, "", "Chemin incorrect.")

        Exit

    EndIf

    If @error = 4 Then

        MsgBox($MB_SYSTEMMODAL, "", "Aucun fichier(s) n'ont été trouvés.")

        Exit

    EndIf
    _ArrayDisplay($aFileList, "Fichiers du dossier")


; List informations  about the array
 
;~  Local $iRows = UBound($aFileList, $UBOUND_ROWS) ; Nombre de rangées

;~     Local $iCols = UBound($aFileList, $UBOUND_COLUMNS) ; Nombres de colonnes

;~     Local $iDimension = UBound($aFileList, $UBOUND_DIMENSIONS) ; Dimensions du tableau
;~     MsgBox($MB_SYSTEMMODAL, "Structure tableau", "Le tableau comporte: " &@cr & $iDimension & " dimension(s)" & _

;~            @cr& $iRows & " rangée(s) & " &@cr & $iCols & " colonne(s).")


; List the files in the console
 
For $i = 1 To $aFileList[0]

    ConsoleWrite($aFileList[$i] & @CRLF)

 modeles()

Next
 
;List the files in the directory
 


func modeles ()
;~ $fichier = 'd:\reglementfunerai.doc'

 $fichier = $aFileList[$i]
 msgbox(64,"fichier",$fichier)

_DebugSetup('', False, 2)
Local $bVisible, $bForceNew

;~ Local $oWordApp = _Word_Create($bVisible=true,$bForceNew=true)

Local $oWordApp = _Word_Create()

$oWordApp2=$oWordApp.AttachedTemplate.FullName


;~ pourquoi cette ligne change elle  le fonctionnement du script ? Mystère et boulle de gomme !!!

;~  msgbox(48,"Info",$oWordApp2)
 
; If the creation doesn't function show an error
 
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocSave Example", _

        "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

Local $oDocument = _Word_DocOpen($oWordApp, $fichier)
 


If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocSave Example", _

        "Error opening " & $fichier & @CRLF & "error = " & @error & ", @extended = " & @extended)
 
 
; List the informations in an Array
 
Local $tableau[2][2]

$tableau[0][0]=$oDocument.AttachedTemplate.Name

;~ _DebugOut($oDocument.AttachedTemplate.Name)

$tableau[0][1]=$oDocument.AttachedTemplate.FullName

;~ _DebugOut($oDocument.AttachedTemplate.FullName)

_ArrayDisplay($tableau," Modeles présents dans le fichier")


If Not FileExists($oDocument.AttachedTemplate.FullName) Then

 $oDocument.AttachedTemplate =  "C:\Templates\Letter.dot"

EndIf
_DebugOut('')

_DebugReportVar('$oWordApp.Templates', $oWordApp.Templates)

For $template In $oWordApp.Templates

    _DebugOut($template.Name)

    _DebugOut($template.FullName)

   $test= _DebugOut($template.Name)

    If Not FileExists($fichier) then

    msgbox(48,"Fichier inexistant"," Fichier Inacessible")
 Else

  msgbox(48,"Le fichier est accessible",$test)

 EndIf

Next
 
 
_DebugReportVar('$oWordApp.AddIns', $oWordApp.AddIns)

For $addIns In $oWordApp.AddIns

    $Nom=_DebugOut($addIns.Name)

    $Chemin=_DebugOut($addIns.Path)

    _DebugOut($addIns.Installed)

    _DebugOut('')

 msgbox(48,"titre", _DebugReportVar('$oWordApp.AddIns', $oWordApp.AddIns))

    ; If Not FileExists(...

Next
;safe document
 
 _Word_DocSave($oDocument)

 If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocSave Example", _

    "Error saving the Word document." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Fermeture du document

 _Word_DocClose($fichier)

EndFunc


 

Share this post


Link to post
Share on other sites
water

How about this? I stripped it down a bit, moved _Word_Create out of the loop and removed hte test for $oWordApp.AttachedTemplate.FullName.

Can't test at the moment so there might be bugs ;-)

#include <Debug.au3>
#include <Array.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <Word.au3>
#include <File.au3>
#include <Array.au3>

Opt("MustDeclareVars", 0)
; List files in a directory
Global $aFileList = _FileListToArray("p:\SG\INFOR", "*.docx")
If @error = 1 Then Exit MsgBox($MB_SYSTEMMODAL, "", "Chemin incorrect.")
If @error = 4 Then Exit MsgBox($MB_SYSTEMMODAL, "", "Aucun fichier(s) n'ont été trouvés.")
_ArrayDisplay($aFileList, "Fichiers du dossier")
Global $oWordApp = _Word_Create()
; If the creation doesn't function show an error
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocSave Example", "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; List the files in the console
For $i = 1 To $aFileList[0]
    ConsoleWrite($aFileList[$i] & @CRLF)
    modeles($aFileList[$i])
Next
_Word_Quit($oWordApp) 
Func modeles($fichier)

    MsgBox(64, "fichier", $fichier)
    _DebugSetup('', False, 2)
    Local $bVisible, $bForceNew
    ;   $oWordApp2 = $oWordApp.AttachedTemplate.FullName  ==> Attachedtemplate property is only available for document objects not for apllication
    Local $oDocument = _Word_DocOpen($oWordApp, $fichier)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocSave Example", "Error opening " & $fichier & @CRLF & "error = " & @error & ", @extended = " & @extended)
    ; List the informations in an Array
    Local $tableau[2][2]
    $tableau[0][0] = $oDocument.AttachedTemplate.Name
    $tableau[0][1] = $oDocument.AttachedTemplate.FullName
    _ArrayDisplay($tableau, " Modeles présents dans le fichier")
    If Not FileExists($oDocument.AttachedTemplate.FullName) Then
        $oDocument.AttachedTemplate = "C:\Templates\Letter.dot"
    EndIf
    _DebugOut('')
    _DebugReportVar('$oWordApp.Templates', $oWordApp.Templates)
    For $template In $oWordApp.Templates
        _DebugOut($template.Name)
        _DebugOut($template.FullName)
        $test = _DebugOut($template.Name)
        If Not FileExists($fichier) Then
            MsgBox(48, "Fichier inexistant", " Fichier Inacessible")
        Else
            MsgBox(48, "Le fichier est accessible", $test)
        EndIf
    Next
    _DebugReportVar('$oWordApp.AddIns', $oWordApp.AddIns)
    For $addIns In $oWordApp.AddIns
        $Nom = _DebugOut($addIns.Name)
        $Chemin = _DebugOut($addIns.Path)
        _DebugOut($addIns.Installed)
        _DebugOut('')
        MsgBox(48, "titre", _DebugReportVar('$oWordApp.AddIns', $oWordApp.AddIns))
    Next
    ; Safe document
    _Word_DocSave($oDocument)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocSave Example", "Error saving the Word document." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    ; Fermeture du document
    _Word_DocClose($fichier)

EndFunc   ;==>modeles

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×