Guest User

Untitled

a guest
Feb 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.57 KB | None | 0 0
  1. Imports Microsoft.Office.Interop
  2.  
  3. Public Class Form1
  4.  
  5.  
  6.     Private WithEvents WordApplication As Word.Application
  7.     Private WithEvents WordDocument As Word.Document
  8.  
  9.     Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  10.         WordDocument.Close()
  11.         WordDocument = Nothing
  12.         WordApplication.Visible = Nothing
  13.         WordApplication = Nothing
  14.     End Sub
  15.  
  16.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  17.         InitComboBox()
  18.     End Sub
  19.  
  20.  
  21.     Private Sub InitComboBox()
  22.         Me.ComboBox1.Items.Clear()
  23.         For i As Integer = 0 To 10
  24.             Me.ComboBox1.Items.Add("Signet_" & i)
  25.         Next
  26.     End Sub
  27.  
  28.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  29.         Me.OpenFileDialog1.FileName = String.Empty
  30.         If Me.OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
  31.             Try
  32.                 If WordApplication Is Nothing Then
  33.                     'Créer un nouveau application
  34.                     WordApplication = New Word.Application()
  35.                     WordApplication.Visible = True
  36.                 End If
  37.                 'ouvrir le fichier
  38.                 WordDocument = WordApplication.Documents.Open(Me.OpenFileDialog1.FileName)
  39.                 Me.TextBox1.Text = Me.OpenFileDialog1.FileName
  40.             Catch ex As Exception
  41.                 MessageBox.Show(ex.Message)
  42.             End Try
  43.         End If
  44.     End Sub
  45.  
  46.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  47.         Dim w As Word.Words = WordApplication.Selection.Words
  48.         'Create a rang from first word to last word
  49.         Dim StartIndex As Integer = w.First.Start
  50.         Dim StopIndex As Integer = w.Last.End
  51.         Dim rng As Word.Range = WordDocument.Range(StartIndex, StopIndex)
  52.         rng.Shading.BackgroundPatternColorIndex = Word.WdColorIndex.wdGray25
  53.         rng.Bookmarks.Add(ComboBox1.Text)
  54.         MsgBox("C'est bon")
  55.     End Sub
  56.  
  57.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  58.         Try
  59.             WordDocument.Close()
  60.             WordDocument = Nothing
  61.         Catch ex As Exception
  62.  
  63.         End Try
  64.  
  65.         Try
  66.             WordApplication.Visible = False
  67.             WordApplication = Nothing
  68.         Catch ex As Exception
  69.  
  70.         End Try
  71.         End
  72.     End Sub
  73.  
  74. End Class
Add Comment
Please, Sign In to add comment