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---1112Had 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.1314The 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).1516Not much has changed since I last used it several years ago (VB.net) - I did not look into advancements in C#.1718```19Module Module120 'Print debug messages21 Const debug = False22 'If no parameters are specified, scan this document23 Const default_file = "c:\mydoc.doc"24 'The keywords we want to match on25 Const keywords = "c:\keywords.txt"26 'The logfile we want to write our results to27 Const logfile = "c:\headerfooter.log"28 Dim alKeywords As New ArrayList()2930 Sub Main()31 loadKeywords()32 If Command().Length = 0 Then33 Console.WriteLine("No Params Passsed")34 scanDocument("c:\mydoc.doc")35 Else36 Console.WriteLine(Command())37 scanDocument(Command())38 End If39 Console.ReadLine()40 End Sub41 Sub loadKeywords()42 'Read keywords into global string arraylist4344 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 reading46 Dim d As New StreamReader(fs)47 'creating a new StreamReader and passing the filestream object fs as argument48 d.BaseStream.Seek(0, SeekOrigin.Begin)49 'Seek method is used to move the cursor to different positions in a file, in this code, to50 'the beginning51 While d.Peek() > -152 'peek method of StreamReader object tells how much more data is left in the file53 Dim strKeyword As String54 strKeyword = d.ReadLine()5556 If debug Then57 Console.WriteLine(strKeyword)58 End If5960 alKeywords.Add(strKeyword)61 'displaying text from doc file in the RichTextBox62 End While63 d.Close()64 End Sub65 Sub scanDocument(ByVal sFile As String)66 Dim oApp As Word.Application67 Dim oDoc As Word.Document68 Dim oSec As Word.Section69 Dim oPageStart As Word.Range70 Dim iPage As Integer, iTotalPages As Integer, iSection As Integer71 Dim sHeader As String, sFooter As String7273 Const wdStatisticPages = 274 Const wdGotoPage = 175 Const wdGotoAbsolute = 176 Const wdHeaderFooterFirstPage = 277 Const wdHeaderFooterPrimary = 17879 'Open the document80 oApp = New Word.Application81 oDoc = oApp.Documents.Open(sFile)82 iTotalPages = oDoc.ComputeStatistics(wdStatisticPages)8384 'Retrieve the headers and footers on each page85 With oDoc8687 iSection = 08889 For iPage = 1 To iTotalPages9091 'Go to the page represented by the page number iPage and92 'retrieve its section93 oPageStart = oDoc.GoTo(What:=wdGotoPage, _94 Which:=wdGotoAbsolute, Count:=iPage)95 oSec = oPageStart.Sections(1)9697 'If this is a different section than the one in the previous98 'iteration and it has a first page header/.footer, then99 'retrieve the first page header/footer for this section.100 'Otherwise, retrieve the primary header/footer for this section101 If (iSection < oSec.Index) And _102 (oSec.PageSetup.DifferentFirstPageHeaderFooter) Then103 sHeader = oSec.Headers(wdHeaderFooterFirstPage).Range.Text104 sFooter = oSec.Footers(wdHeaderFooterFirstPage).Range.Text105 Else106 sHeader = oSec.Headers(wdHeaderFooterPrimary).Range.Text107 sFooter = oSec.Footers(wdHeaderFooterPrimary).Range.Text108 End If109110 iSection = oSec.Index111 If debug Then112 Console.WriteLine("Page " & iPage & ", Section " & iSection & _113 ":" & vbCrLf)114 Console.WriteLine(" Header: " & sHeader)115 Console.WriteLine(" Footer: " & sFooter)116 End If117118 checkText(sHeader, sHeader, sFooter, iPage, iSection)119 checkText(sFooter, sHeader, sFooter, iPage, iSection)120 Next121122 End With123124 'Make Word visible to compare the document with the results in the125 'debug window126 'oApp.Visible = True127 oDoc.Close()128 oApp.Quit()129 End Sub130 Sub checkText(ByVal sText As String, ByVal sHeader As String, ByVal sFooter As String, ByVal iPage As Integer, ByVal iSection As Integer)131132 Dim i As Integer133134 For i = 0 To (alKeywords.Count() - 1)135 If sText.ToUpper.Contains(alKeywords.Item(i).ToString().ToUpper()) Then136 log("Match in document " & Command() & " Text: " & sText)137 log(" Match Keyword: " & alKeywords.Item(i).ToString() & " Page: " & iPage & " Section: " & iSection)138 End If139 Next140 End Sub141142 Sub log(ByVal sTest As String)143 Console.WriteLine(sTest)144 End Sub145End Module146```147148149