Wednesday, June 9, 2010

Activate window by name in MS-Word

Sub GenerateGlossary()
Dim strSource As String
Dim strDestination As String
Dim strGlossaryName As String

strSource = ActiveWindow.Caption
strGlossaryName = "word"

Documents.Add
ActiveDocument.SaveAs FileName:=strGlossaryName, FileFormat:=wdFormatDocument
strDestination = ActiveWindow.Caption
Windows(strSource).Activate
End Sub

Active document paragraph in word.

Sub loopDemo()
Dim i As Integer
For i = 1 To ActiveDocument.Paragraphs.Count
Application.StatusBar = "formatting" & i & " out of " & ActiveDocument.Paragraphs.Count & "..."
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdMove
Next i
End Sub

Turning Off Track Changes

Sub trac()
Dim blnTrackChangesOn As Boolean
blnTrackChangesOn = ActiveDocument.TrackRevisions
ActiveDocument.TrackRevisions = False
ActiveDocument.TrackRevisions = blnTrackChangesOn
End Sub

To delete all the files in a given directory:-

'Loop through all the files in the directory by using Dir$ function
Dim MyFile As String
MyFile = Dir$("c:\temp\*.*")
Do While MyFile <> ""
KillProperly "c:\temp\" & MyFile
'need to specify full path again because a file was deleted 1
MyFile = Dir$("c:\temp\*.*")
Loop

Kill statement can't delete readonly files:-

Dim KillFile As String
KillFile = "c:\temp\temp.doc"
'Check that file exists
If Len(Dir$(KillFile)) > 0 Then
'First remove readonly attribute, if set
SetAttr KillFile, vbNormal
'Then delete the file
Kill KillFile
End If