|
Partner
|
|
|
Word Inhaltsverzeichnis auslesen mit C#
Autor:
Kevin Gerndt
|
Sprache:
C#
|
Bewertung:
8,2 (1 Bewertung)
|
Anzahl der Aufrufe:
2148
|
Beschreibung:
Grundvoraussetzung für die Funktion dieser Methode ist, eine Word Installation (Getestet mit Word 2007 und Word 2010).
1. Zunächst muss ein Verweis die zwei COM Objekte: Microsoft.Office.Core und Microsoft.Office.Interop.Word gesetzt werden.
2. Anschließend empfiehlt es sich einen Alias auf "Word" zu setzen, da sonst immer mit dem Vollqualifizierten Namen gearbeitet werden muss.
Abgelegt unter: Word, Word Inhaltsverzeichnis, Word Toc, Inhaltsverzeichnis auslesen.
|
| C# |
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
|
using Word = Microsoft.Office.Interop.Word;
public static List<string> GetTocNodes(FileInfo WordFile)
{
Object filePath = WordFile.FullName;
Word.Application wordApplication = new Word.Application();
wordApplication.Visible = false;
Word.Document wordDocument = new Word.Document();
wordDocument = wordApplication.Documents.Open(filePath);
//Inhaltsverzeichnis in tocContent speichern
string tocContent = wordApplication.ActiveDocument.TablesOfContents[1].Range.Text;
//Word Document und Anwendung schließen
wordDocument.Close();
wordApplication.Quit();
//Zerlegung des Inhaltsverzeichnisses in einzelne Überschriften
tocContent = tocContent.TrimEnd("\r".ToCharArray());
//Zerlegt das Inhaltsverzeichnis in einzelne Segmente
string[] paragraphs = tocContent.Split("\r".ToCharArray());
List<string> tocNodes = new List<string>();
foreach (string paragraph in paragraphs)
{
tocNodes.Add(paragraph)
}
return tocNodes;
}
|
|
Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)
|
|
Diese Snippets könnten für Sie interessant sein:
|
|
|
|
|
|
|
|
|