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));
            }
	}