stevennathaniel

Menulis Ke File MS Word 1

Jan 12th, 2016
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports Word = Microsoft.Office.Interop.Word
  2.  
  3.  
  4. Public Class Form6
  5.  
  6.     Dim objWord As New Word.Application
  7.  
  8.     Dim objDoc As New Word.Document
  9.  
  10.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  11.  
  12.         objDoc = objWord.Documents.Open("D:\Proyek VB NET Latihan\Latihan22\Latihan22\Latihan22\Template1.dotm")
  13.  
  14.         With objWord
  15.  
  16.             'Make Word Application Visible
  17.             .Visible = True
  18.  
  19.             'Sets Alignment to Left
  20.             .Selection.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
  21.  
  22.             'Set Font
  23.             .ActiveWindow.Selection.Font.Name = "Arial"
  24.  
  25.             'Set Font Size
  26.             .ActiveWindow.Selection.Font.Size = 8
  27.  
  28.             'Focus on Footer ( Use wdSeekCurrentPageHeader to focus on header )
  29.  
  30.             .ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter
  31.  
  32.             'Set font
  33.             .ActiveWindow.Selection.TypeText("PDAM Kota Balikpapan")
  34.  
  35.             'Resets focus back to main document
  36.             .ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument
  37.  
  38.             'Perintah untuk Mengirim file ke printer
  39.             ' .PrintOut()
  40.         End With
  41.  
  42.         Dim strOpening As String = "Dear " & TextBox1.Text & "," & vbCrLf & vbCrLf
  43.  
  44.         Dim strLetter As String = "Letter Body Here" & vbCrLf & vbCrLf
  45.  
  46.         Dim strClosing As String = "Yours Sincerely, " & vbCrLf & vbCrLf & TextBox2.Text
  47.  
  48.         objWord.ActiveWindow.Selection.TypeText(strOpening & strLetter & strClosing)
  49.  
  50.     End Sub
  51.  
  52.     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  53.         'Find out how many files are saved in the Save folder
  54.         Dim int As Integer
  55.  
  56.         int = My.Computer.FileSystem.GetFiles("D:\fileExcel\").Count + 1
  57.  
  58.         'Save As command
  59.  
  60.         objDoc.SaveAs2("D:\fileExcel\" & int & ".doc")
  61.  
  62.  
  63.     End Sub
  64.  
  65.     Private Sub Form6_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  66.         objWord.Quit()
  67.  
  68.         Try
  69.             objWord.Quit()
  70.  
  71.         Catch ex As Exception
  72.  
  73.         End Try
  74.     End Sub
  75. End Class
RAW Paste Data