1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Office.Interop.Word;
using Microsoft.Vbe.Interop;
namespace WindowsApplication1
{
public partial class FormPrincipal : Form
{
public FormPrincipal()
{
InitializeComponent();
}
private void FormPrincipal_Load(object sender, EventArgs e)
{
}
/// <summary>
/// Lancement du traitement
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_Lancement_Click(object sender, EventArgs e)
{
string RepertoireDeRecherche = tbx_PathDesFichiers.Text;
if (!Directory.Exists(RepertoireDeRecherche))
{
MessageBox.Show("Veuillez choisir un répertoire existant", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// On rends inactif le bouton de lancement
btn_Lancement.Enabled = false;
// Ajoute la macro dans tous les documents .doc du répertoire
foreach (string FichierCourant in Directory.GetFiles(RepertoireDeRecherche, "*.doc", SearchOption.AllDirectories))
{
try
{
AjouterMacroDansDocumentWord(FichierCourant);
}
catch (Exception)
{
continue;
}
}
// Ajoute la macro dans tous les documents .dot du répertoire
foreach (string FichierCourant in Directory.GetFiles(RepertoireDeRecherche, "*.dot", SearchOption.AllDirectories))
{
try
{
AjouterMacroDansDocumentWord(FichierCourant);
}
catch (Exception)
{
continue;
}
}
MessageBox.Show("Le traitement sur ce répertoire est terminé", "Traitement terminé", MessageBoxButtons.OK, MessageBoxIcon.Information);
// On rends actif le bouton de lancement
btn_Lancement.Enabled = true;
}
/// <summary>
/// Ajoute une macro dans un fichier word fourni en paramètre
/// </summary>
/// <param name="NomFichierWordComplet">Le nom complet du fichier word (avec son path)</param>
public void AjouterMacroDansDocumentWord(string NomFichierWordComplet)
{
object FileName = NomFichierWordComplet;
// Connexion à Word
Microsoft.Office.Interop.Word.Application MsWord = new Microsoft.Office.Interop.Word.Application();
MsWord.Visible = true; // mettez cette variable à true si vous souhaitez visualiser les opérations.
object Missing = System.Reflection.Missing.Value;
if (File.Exists((string)FileName))
{
// Ouverture du document
Document Doc = MsWord.Documents.Open(ref FileName, ref Missing, ref Missing,
ref Missing, ref Missing, ref Missing, ref Missing, ref Missing,
ref Missing, ref Missing, ref Missing, ref Missing, ref Missing,
ref Missing, ref Missing, ref Missing);
VBComponent Comp = Doc.VBProject.VBComponents.Add(Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule);
Comp.Name = "Module";
CodeModule Module = Doc.VBProject.VBComponents.Item("Module").CodeModule;
int NbLignes = Module.CountOfLines;
Module.InsertLines(NbLignes + 1, "Sub AutoOpen()");
Module.InsertLines(NbLignes + 2, "' AutoOpen Macro");
Module.InsertLines(NbLignes + 3, "' Macro created 05/10/2008 by acer");
Module.InsertLines(NbLignes + 4, "Dim aStory As Range");
Module.InsertLines(NbLignes + 5, "Dim aField As Field");
Module.InsertLines(NbLignes + 6, "Dim tableau(1000) As String");
Module.InsertLines(NbLignes + 7, "Dim i As Integer");
Module.InsertLines(NbLignes + 8, "Dim majOK As Boolean");
Module.InsertLines(NbLignes + 9, "Dim cptTableau As Integer");
Module.InsertLines(NbLignes + 10, "cptTableau = 0");
Module.InsertLines(NbLignes + 11, "For Each aStory In ActiveDocument.StoryRanges");
Module.InsertLines(NbLignes + 12, "For Each aField In aStory.Fields");
Module.InsertLines(NbLignes + 13, "majOK = True");
Module.InsertLines(NbLignes + 14, "For i = 0 To cptTableau");
Module.InsertLines(NbLignes + 15, "If UCase(tableau(i)) = UCase(aField.Code.Text) Then");
Module.InsertLines(NbLignes + 16, "majOK = False");
Module.InsertLines(NbLignes + 17, "End If");
Module.InsertLines(NbLignes + 18, "Next i");
Module.InsertLines(NbLignes + 19, "If majOK = True Then");
Module.InsertLines(NbLignes + 20, "aField.Update");
Module.InsertLines(NbLignes + 21, "If InStr(aField.Code.Text, \"ASK\") <> 0 Then");
Module.InsertLines(NbLignes + 22, "tableau(cptTableau) = aField.Code.Text");
Module.InsertLines(NbLignes + 23, "cptTableau = cptTableau + 1");
Module.InsertLines(NbLignes + 24, "End If");
Module.InsertLines(NbLignes + 25, "End If");
Module.InsertLines(NbLignes + 26, "Next aField");
Module.InsertLines(NbLignes + 27, "Next aStory");
Module.InsertLines(NbLignes + 28, "ActiveWindow.Activate");
Module.InsertLines(NbLignes + 29, "ActiveWindow.WindowState = wdWindowStateMaximize");
Module.InsertLines(NbLignes + 30, "If (ActiveWindow.View.ShowFieldCodes) Then");
Module.InsertLines(NbLignes + 31, "ActiveWindow.View.ShowFieldCodes = Not ActiveWindow.View.ShowFieldCodes");
Module.InsertLines(NbLignes + 32, "End If");
Module.InsertLines(NbLignes + 33, "End Sub");
// Sauver le document
Doc.SaveAs(ref Missing, ref Missing, ref Missing, ref Missing, ref Missing,
ref Missing, ref Missing, ref Missing, ref Missing, ref Missing,
ref Missing, ref Missing, ref Missing, ref Missing, ref Missing,
ref Missing);
Doc.Close(ref Missing, ref Missing, ref Missing);
}
// Fermeture de word
MsWord.Quit(ref Missing, ref Missing, ref Missing);
}
}
} |
Partager