Autor Beitrag
tkutter
Hält's aus hier
Beiträge: 1



BeitragVerfasst: Di 02.10.12 13:08 
Hallo zusammen,

ich schreibe ein Programm, welches mir Word Textmarken (Bookmarks) mit Werten aus einer DB befüllt. Dies löse ich folgendermaßen:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
//...

object oFileName = filePath;
object oFALSE = false;
object oEMPTY = string.Empty;
object oDocFormat = 0;

Word.Application wordApp = new Word.ApplicationClass();
Word.Document wordDoc = wordApp.Documents.Open(ref oFileName, ref oFALSE, ref oFALSE, ref oFALSE, ref oEMPTY, ref oEMPTY, ref oFALSE, ref oEMPTY, ref oEMPTY, ref oDocFormat);

foreach (Word.Bookmark bookmark in wordDoc.Bookmarks)
{
    Word.Range range = bookmark.Range.Duplicate;
    range.Text = "Bsp";
}

//...


Mein Problem ist folgendes:
Nach der Befüllung durch das Programm sind sämtliche Textmarken weg!
Ich löse dies folgendermaßen:

ausblenden volle Höhe C#-Quelltext
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:
//...

ArrayList alBookmarks = new ArrayList();
foreach (Word.Bookmark bookmark in wordDoc.Bookmarks)
{
    if ((bookmark.Name != null) && (bookmark.Name != ""))
        alBookmarks.Add(bookmark);
}

foreach (Word.Bookmark bookmark in alBookmarks)
{
    string sBookmarkName = "";
    try { sBookmarkName = bookmark.Name; }
    catch { continue; }
    Word.Range range = null;
    try
    {
        range = bookmark.Range.Duplicate;
        range.Text = "Bsp";
    }
    catch (Exception) { }
    finally
    {
        if (range != null)
        {
            object oRange = range;
            wordDoc.Bookmarks.Add(sBookmarkName, ref oRange);
            Marshal.ReleaseComObject(range);
        }
    }
}

foreach (Word.Bookmark bookmark in alBookmarks)
    Marshal.ReleaseComObject(bookmark);

//...


Diese Möglichkeit liefert bei mir allerdings sehr schlechte Performance über eine Windows-Terminalsitzung (knapp 8-12 Sec für ~30 Textmarken).
Gibt es eine Möglichkeit die Textmarken zu behalten ohne diesen Umweg?

Vielen Dank im Voraus für eure Hilfe