You are currently browsing the category archive for the ‘MS Word’ category.

Example below demonstrates how to:

  • Start a Microsoft Word application
  • Open a new word template from a saved location
  • Paste information from the clipboard
  • Save the new document
  • Quit the MS Word application

_______________________________________________________________
Private Declare Function OpenProcess Lib “kernel32″ _
                  (ByVal dwDesiredAccess As Long, _
                   ByVal bInheritHandle As Long, _
                   ByVal dwProcessId As Long) As Long

Public WithEvents wordApp As Word.Application
Private mobjWordApp As Word.Application

______________________________________________________________
Public Sub CreateWord(xType As String, xName As String, _
           tsavepathO As String, tTemplatePath As String)

   Dim oPara1 As Word.Paragraph
   Dim templName As String    

   Select Case xType
        Case “Final_Figures”:   templName = “MB FinalF Template.dot”
        Case “Customer_List”:  templName = “MB CustomerL Template.dot”
        End Select

   Set mobjWordApp = New Word.Application

   With mobjWordApp
         .Visible = True
         .Documents.Add Template:=(Module1.tTemplatePath & _
                 Application.PathSeparator & templName), _
                 NewTemplate:=False, DocumentType:=0
   End With

   Set oPara1 = mobjWordApp.ActiveDocument.Content.Paragraphs.Add
   oPara1.Range.PasteAndFormat wdFormatPlainText

   With mobjWordApp
      .ActiveDocument.SaveAs tsavepathO & xName
      .ActiveDocument.Close
   End With   

   mobjWordApp.Visible = False
   mobjWordApp.Quit
   Set mobjWordApp = Nothing

End Sub