Thursday, September 2, 2010

VBScript - MS Word

'Create a word Document
Set objWord = CreateObject("Word.Application")
objWord.Visible = True

'Documents collection includes all of the open documents.
'Add method is used to create a blank document.
'Document object returned by Add method is assigned to a variable, objDoc.
Set objDoc = objWord.Documents.Add()

'Open a word Document file
Set wrd = CreateObject("Word.Application")
wrd.Visible = True

'To open an existing document, use the Open method with the Documents collection.
'The following instruction opens a document "Temp.doc" located in the folder "D:\"

wrd.Documents.Open "D:\Temp.doc"
Set wrd.Nothing

'Writting a Text in Word Application
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set oDoc = oWord.Documents.Add()
Set oSelection = oWord.Selection

'Selection property represents selected area in a document or an insertion point
oSelection.TypeText = "Sample Text"

'Formatting the text in the word document
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set oDoc = oWord.Documents.Add()
Set oSelection = oWord.Selection
'Setting the Font Name
oSelection.Font.Name = "Arial"
'Setting the Font Size
oSelection.Font.Size = "20"
'Setting the type face to Bold
oSelection.Font.Bold = True
oSelection. ParagraphFont.Alignment = 1
'0 - Aligns text to Left, 1- Aligns text to center
'2 - Aligns text to Right, 3 - Justify the text
oSelection.TypeText = "Add your text here"

'Replacing a Word in Word Document
Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.documents.Open("C:\WordDocument.docx")
Set oWords = oDoc.Words
For word = 1 to oWords.Count
oWords.Item(word).Text = Replace(oWords.Item(word).Text, "sample","New word")
Next
oDoc.Save
oDoc.Close
oWord.Quit
Set oDoc = Nothing
Set oWords = Nothing
Set oWord = Nothing

'Saving the Word Document

To save a single doucument use the Save method with the Document object. The following insturction saves the document named "Sales.doc"

Documents("Sales.doc")Save

You can save all open documents by applying Save method to the Documents collection. The following instruction saves all open documents.

Documents.Save

'To save to a new location use "SaveAs" else use "Save" method to save it on default location
oDoc.SaveAs("c:\MyDoc.doc")

'Closing the Word Document
oWord.Quit

'Working with Tables
Set oWord = CreateObject("Word.Application")
oWord.visible = True
Set oDoc = oWord.Documents.Add()
Set oRange = oDoc.Range()
oDoc.Tables.Add oRange,3,3
Set oTable = oDoc.Tables(1)
For Row = 1 to 3
For Column = 1 to 3
oTable.Cell(Row,Column).Range.Text = Row&" Row, "&Column&" Column"
Next
Next
'Make the First Row Data to Bold
oTable.Rows.Item(1).Range.Font.Bold = True
'Makes the First Row Data to Italic
oTable.Rows.Item(1).Range.Font.Italic = True
'Changes the font size
oTable.Rows.Item(1).Range.Font.Size = 12
'Sets the width of the Column
oTable.Columns.Item(1).SetWidth 100,0

'Open and Append Text to a Document
Dim oWord
Set oWord = CreateObject("Word.Application")
oWord.Documents.Open "c:\text.doc"
oWord.Selection.TypeText "Text which is appended to existing document"
oWord.ActiveDocument.Save
oWord.Quit
Set oWord = Nothing

Selection property by default appends the text at the beginning only. To put the statements for selection at the end before entering the text, use the below cose.

Dim oWord
Set oWord = CreateObject("Word.Application")
oWord.Documents.Open "c:\text.doc"
oWord.Selection.EndKey 6,0
oWord.Selection.TypeText "Text which is appended to existing document"
oWord.ActiveDocument.Save
oWord.Quit
Set oWord = Nothing

'EndKey method moves the selection to the specified unit.
'Two parameters are unit and Extend. We have used 6 for unit
'which is to move at the end of story & 0 as extend which is to move the selection.
'If you don't specify these parameters, selection moves to the end of line.

'Prints the word document
Dim oWord
Set oWord = CreateObject("Word.Application")
oWord.Documents.Open "c:\test.docx"
oWord.Activedocument.PrintOut
oWord.Quit
Set oWord = Nothing

5 comments:

  1. I opened one xml word document from that i need copy those two tag xxx and yyy and paste into any text ex d:\sample.txt by using macro give some idea for that.

    ReplyDelete
  2. open file using fso object readall and then using fso again save it as .txt

    ReplyDelete
  3. can i hv the code for insert tablesofcontent

    ReplyDelete