VB.net Source

1---
2title: 'VB.net'
3date: '2007-07-13'
4published_at: '2007-07-13T15:37:00.000+10:00'
5tags: ['programming', 'vb', 'windows']
6author: 'Gavin Jackson'
7excerpt: 'Had a bit of fun (?) at work writing a VB console application that simply opens up a word document and checks all headers and footers in the document for a specified list of keywords. The only real go...'
8updated_at: '2007-07-13T15:48:40.855+10:00'
9legacy_url: 'http://www.gavinj.net/2007/07/vbnet.html'
10---
11
12Had a bit of fun (?) at work writing a VB console application that simply opens up a word document and checks all headers and footers in the document for a specified list of keywords.
13
14The only real gotcha is that you need to install the Primary Interop Assemblies for you version of office (and add them to your project references). Microsoft have a very strong com API for scripting their applications (Word, Excel, Powerpoint, Outlook etc).
15
16Not much has changed since I last used it several years ago (VB.net) - I did not look into advancements in C#.
17
18```
19Module Module1
20    'Print debug messages
21    Const debug = False
22    'If no parameters are specified, scan this document
23    Const default_file = "c:\mydoc.doc"
24    'The keywords we want to match on
25    Const keywords = "c:\keywords.txt"
26    'The logfile we want to write our results to
27    Const logfile = "c:\headerfooter.log"
28    Dim alKeywords As New ArrayList()
29
30    Sub Main()
31        loadKeywords()
32        If Command().Length = 0 Then
33            Console.WriteLine("No Params Passsed")
34            scanDocument("c:\mydoc.doc")
35        Else
36            Console.WriteLine(Command())
37            scanDocument(Command())
38        End If
39        Console.ReadLine()
40    End Sub
41    Sub loadKeywords()
42        'Read keywords into global string arraylist
43
44        Dim fs As New FileStream("c:\keywords.txt", FileMode.Open, FileAccess.Read)
45        'declaring a FileStream to open the file named file.doc with access mode of reading
46        Dim d As New StreamReader(fs)
47        'creating a new StreamReader and passing the filestream object fs as argument
48        d.BaseStream.Seek(0, SeekOrigin.Begin)
49        'Seek method is used to move the cursor to different positions in a file, in this code, to
50        'the beginning
51        While d.Peek() > -1
52            'peek method of StreamReader object tells how much more data is left in the file
53            Dim strKeyword As String
54            strKeyword = d.ReadLine()
55
56            If debug Then
57                Console.WriteLine(strKeyword)
58            End If
59
60            alKeywords.Add(strKeyword)
61            'displaying text from doc file in the RichTextBox
62        End While
63        d.Close()
64    End Sub
65    Sub scanDocument(ByVal sFile As String)
66        Dim oApp As Word.Application
67        Dim oDoc As Word.Document
68        Dim oSec As Word.Section
69        Dim oPageStart As Word.Range
70        Dim iPage As Integer, iTotalPages As Integer, iSection As Integer
71        Dim sHeader As String, sFooter As String
72
73        Const wdStatisticPages = 2
74        Const wdGotoPage = 1
75        Const wdGotoAbsolute = 1
76        Const wdHeaderFooterFirstPage = 2
77        Const wdHeaderFooterPrimary = 1
78
79        'Open the document
80        oApp = New Word.Application
81        oDoc = oApp.Documents.Open(sFile)
82        iTotalPages = oDoc.ComputeStatistics(wdStatisticPages)
83
84        'Retrieve the headers and footers on each page
85        With oDoc
86
87            iSection = 0
88
89            For iPage = 1 To iTotalPages
90
91                'Go to the page represented by the page number iPage and
92                'retrieve its section
93                oPageStart = oDoc.GoTo(What:=wdGotoPage, _
94                                           Which:=wdGotoAbsolute, Count:=iPage)
95                oSec = oPageStart.Sections(1)
96
97                'If this is a different section than the one in the previous
98                'iteration and it has a first page header/.footer, then
99                'retrieve the first page header/footer for this section.
100                'Otherwise, retrieve the primary header/footer for this section
101                If (iSection < oSec.Index) And _
102                   (oSec.PageSetup.DifferentFirstPageHeaderFooter) Then
103                    sHeader = oSec.Headers(wdHeaderFooterFirstPage).Range.Text
104                    sFooter = oSec.Footers(wdHeaderFooterFirstPage).Range.Text
105                Else
106                    sHeader = oSec.Headers(wdHeaderFooterPrimary).Range.Text
107                    sFooter = oSec.Footers(wdHeaderFooterPrimary).Range.Text
108                End If
109
110                iSection = oSec.Index
111                If debug Then
112                    Console.WriteLine("Page " & iPage & ", Section " & iSection & _
113                            ":" & vbCrLf)
114                    Console.WriteLine("   Header: " & sHeader)
115                    Console.WriteLine("   Footer: " & sFooter)
116                End If
117
118                checkText(sHeader, sHeader, sFooter, iPage, iSection)
119                checkText(sFooter, sHeader, sFooter, iPage, iSection)
120            Next
121
122        End With
123
124        'Make Word visible to compare the document with the results in the
125        'debug window
126        'oApp.Visible = True
127        oDoc.Close()
128        oApp.Quit()
129    End Sub
130    Sub checkText(ByVal sText As String, ByVal sHeader As String, ByVal sFooter As String, ByVal iPage As Integer, ByVal iSection As Integer)
131
132        Dim i As Integer
133
134        For i = 0 To (alKeywords.Count() - 1)
135            If sText.ToUpper.Contains(alKeywords.Item(i).ToString().ToUpper()) Then
136                log("Match in document " & Command() & " Text: " & sText)
137                log(" Match Keyword: " & alKeywords.Item(i).ToString() & " Page: " & iPage & " Section: " & iSection)
138            End If
139        Next
140    End Sub
141
142    Sub log(ByVal sTest As String)
143        Console.WriteLine(sTest)
144    End Sub
145End Module
146```
147
148
149