Mega Code Archive

 
Categories / VB.Net Tutorial / Windows
 

Edit Word document

public class Test    public Shared Sub Main         Dim objWordApp As Word.Application         objWordApp = New Word.Application         Dim objWordApp As Word.Application         objWordApp = New Word.Application         Dim objDoc As Word.Document         'Show the Word application window if checked.         objWordApp.Visible = IIf(chkShowWord.Checked, True, False)         'Open an existing document.         objWordApp.Documents.Open("C:\Temp\Sample.doc")         objDoc = objWordApp.ActiveDocument         'Find and replace some text.         objDoc.Content.Find.Execute(FindText:="VB", _             ReplaceWith:="Visual Basic Express", _             Replace:=Word.WdReplace.wdReplaceAll)         While objDoc.Content.Find.Execute(FindText:="  ", _             Wrap:=Word.WdFindWrap.wdFindContinue)             objDoc.Content.Find.Execute(FindText:="  ", ReplaceWith:=" ", _                 Replace:=Word.WdReplace.wdReplaceAll, _                 Wrap:=Word.WdFindWrap.wdFindContinue)         End While         'Save and close the document.         objWordApp.Documents.Item(1).Save()         Console.WriteLine("Replaced all instances of 'VB' with 'Visual Basic Express' " & _             "and saved the document")         objWordApp.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)         objWordApp.Quit()         objWordApp = Nothing         objWordApp.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)         objWordApp.Quit()         objWordApp = Nothing    End Sub End class