Sunday, February 21, 2010

Microsoft Word Operations

1 ) Check the existence of  a Microsoft Word file and if the file is present delete it and add a new file....

' Declare the variables

Dim objFso,objMyFile,strFileName,objWord,objDoc,objSelection

  strFileName="Subrat.doc"
 
  'Create an object of class FileSystemObject
   
    Set objFso = CreateObject("Scripting.FileSystemObject")
 
  'Check if the folder structure exists, if not, create it

    Dim sFolder : sFolder = "c:\MyFile"
    If (objFso.FolderExists(sFolder)) Then
        'do nothing
   Else
        Set objFolder= objFso.CreateFolder(sFolder)
   End If
  
    Dim FileName : FileName = "c:\MyFile\"&strFileName
   
    If objFso.FileExists(FileName) Then
            Set objMyFile = objFso.GetFile(FileName)
            On error resume next
                objMyFile.Delete
                If err.number <> 0 Then
                    LogWarn "Could not delete the file : >>> ", "c:\MyFile\"&strFileName
                    err.clear
                End If
    End If
       
    'Add a ms word document
 
  Set objWord = CreateObject("Word.Application")
  Set objDoc = objWord.documents.add
  Set objSelection = objWord.selection
  objSelection.typetext "My new document"
  objDoc.saveas(FileName)
  objWord.Quit
 
  'Clean up
 
  set objWord = nothing
  Set objDoc= nothing
  Set objSelection = nothing
  Set objFso= nothing
  Set objMyFile= nothing

No comments:

Post a Comment