IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Dotnet Discussion :

Problème de style avec automation Word


Sujet :

Dotnet

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Nouveau candidat au Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Avril 2012
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Drôme (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Avril 2012
    Messages : 2
    Par défaut Problème de style avec automation Word
    Bonjour,

    Je cherche à récupérer les styles des différents niveaux de titres d'un document Word puis de les appliqués dans un autre document.
    J'utilise pour cela les méthodes "get_Style()" et "set_Style(ref ?)" comme ont peut le voir dans le code ci-dessous.

    Mais au moment ou j'appelle la méthode "set_Style(ref myStyle)" le programme plante.
    Est ce que la méthode set_Style(ref ?) attend un obj "Word.Style" ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
            public static int get_Level(Word.WdOutlineLevel wdOutLineLevel)
            {
                Word.WdOutlineLevel[] allOutlineLevel = new Word.WdOutlineLevel[] { Word.WdOutlineLevel.wdOutlineLevelBodyText, Word.WdOutlineLevel.wdOutlineLevel1, Word.WdOutlineLevel.wdOutlineLevel2, Word.WdOutlineLevel.wdOutlineLevel3, Word.WdOutlineLevel.wdOutlineLevel4, Word.WdOutlineLevel.wdOutlineLevel5, Word.WdOutlineLevel.wdOutlineLevel6, Word.WdOutlineLevel.wdOutlineLevel7, Word.WdOutlineLevel.wdOutlineLevel8, Word.WdOutlineLevel.wdOutlineLevel9 };
     
                for (int i = 0; i < allOutlineLevel.Length; i++)
                {
                    if (allOutlineLevel[i] == wdOutLineLevel)
                    {
                        return i;
                    }
                }
     
                return 0;
            }
     
    	public void test_WORD()
    	{
                try
                {
    		Word.Style[] tabStyle = new Word.Style[] { null, null, null, null, null, null, null, null, null, null };
     
                    Word.Application myWordApp = toolBox.Wd_createProcess();
                    Word.Document oDoc;
     
                    // Open src Word document
                    oDoc = myWordApp.Documents.Open(this.textBoxPathSrc.Text); // c:\test_src.doc
     
                    foreach (Word.Paragraph myParaph in oDoc.Paragraphs)
                    {
                        int level = toolBox.get_Level(myParaph.OutlineLevel);
                        dynamic style = myParaph.get_Style();
                        if (tabStyle[level] == null)
                        {
                            tabStyle[level] = (Word.Style)style;
                        }
                    }
     
                    oDoc.Close(oFalse, oMissing, oMissing);
                    toolBox.COM_releaseObject(oDoc);
     
                    oDoc = myWordApp.Documents.Open(this.textBoxPathDest.Text); // c:\test_out.doc
     
                    foreach (Word.Paragraph myParaph in oDoc.Paragraphs)
                    {
                        int level = toolBox.get_Level(myParaph.OutlineLevel);
                        object myStyle = tabStyle[level];
                        myParaph.set_Style(ref myStyle);
                    }
     
                    oDoc.Close(oFalse, oMissing, oMissing);
                    toolBox.COM_releaseObject(oDoc);
     
                    myWordApp.Quit(oFalse, oMissing, oMissing);
                    toolBox.COM_releaseObject(myWordApp);
                }
                catch (Exception err)
                {
                    MessageBox.Show(toolBox.format_error(err));
                }
    	}

  2. #2
    Expert confirmé Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Par défaut
    Est ce que la méthode set_Style(ref ?) attend un obj "Word.Style" ?
    Plutôt Word.WdBuiltinStyle

  3. #3
    Nouveau candidat au Club
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Avril 2012
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Drôme (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Avril 2012
    Messages : 2
    Par défaut
    La méthode set_Style(ref ) permet donc de modifier le type de style.
    Mais comment et il possible de modifier la couleur, la police, les interligne et l'alignement (gauche, centre, droit).

    J'ai essayé avec ce code mais sans résultat :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    myParaph.Range.Font = myFont; //Word.Font
    myParaph.Range.ParagraphFormat = myParaphFormat; //Word.ParagraphFormat

  4. #4
    Expert confirmé Avatar de Graffito
    Profil pro
    Inscrit en
    Janvier 2006
    Messages
    5 993
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2006
    Messages : 5 993
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    myParaph.Range.ParagraphFormat.Alignment = 
            Word.WdParagraphAlignment.wdAlignParagraphCenter 
    myParaph.Range.Font.Name = "Tahoma";
    myParaph.Range.Font.ColorIndex = Word.WdColorIndex.wdDarkBlue;

Discussions similaires

  1. [AC-2010] Problème avec automation word
    Par tonio-lille dans le forum VBA Access
    Réponses: 12
    Dernier message: 12/10/2012, 10h29
  2. Problème de style avec un ContextMenu
    Par choas dans le forum Windows Presentation Foundation
    Réponses: 2
    Dernier message: 24/10/2008, 14h19
  3. problème de style avec h:selectOneRadio
    Par al3alwa dans le forum JSF
    Réponses: 0
    Dernier message: 20/10/2008, 10h58
  4. Réponses: 3
    Dernier message: 06/05/2007, 20h11
  5. Problème de style avec firefox
    Par 12_darte_12 dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 21/03/2006, 16h17

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo