IntroductionGetting StartedAll PostsNotesAbout The Author
Word Utilities
Monday, October 12, 2009 - 10:03 AM
Public Function RememberCursorLocation() As Boolean
    On Error Resume Next
    Selection.Collapse Direction:=wdCollapseStart
    If ActiveDocument.Bookmarks.Exists("CursorLocation") Then ActiveDocument.Bookmarks("CursorLocation").DELETE
    ActiveDocument.Bookmarks.Add name:="CursorLocation", Range:=Selection.Range
End Function

Public Function ReturnToCursorLocation() As Boolean
    If ActiveDocument.Bookmarks.Exists("CursorLocation") Then
        Selection.GoTo what:=wdGoToBookmark, name:="CursorLocation"
        ActiveDocument.Bookmarks("CursorLocation").DELETE
    End If
End Function


Public Sub UpdateFields()
    ActiveDocument.Fields.Update
    Dim Sec As Section
    For Each Sec In ActiveDocument.Sections
        With Sec
            .Headers(wdHeaderFooterPrimary).Range.Fields.Update
            .Headers(wdHeaderFooterFirstPage).Range.Fields.Update
            .Footers(wdHeaderFooterPrimary).Range.Fields.Update
            .Footers(wdHeaderFooterFirstPage).Range.Fields.Update
        End With
    Next
End Sub



Public Cont TemplateFileName = "YourTemplateName.dot"

Public Function AttachTemplate()
'assuming template is in same directory as the activedocument
    Dim sDir$: sDir = ActiveDocument.Path
    On Error GoTo Failed
    With ActiveDocument
        .UpdateStylesOnOpen = True
        .AttachedTemplate = sDir & "\" & TemplateFileName
        .UpdateStylesOnOpen = False
    End With
    AttachTemplate = True
    Exit Function
Failed:
End Function


'goes at top of module:
Private AutoTextName$

Public Sub InsertSectionHeader()
'assumes autotext is in an attached template
    AutoTextName = "YourAutotextName"
    Dim FailedTest As Boolean
    On Error GoTo Failed
    ActiveDocument.AttachedTemplate.AutoTextEntries(AutoTextName).Insert Where:=Selection.Range, RichText:=True
    Exit Sub
Failed:
    If Not FailedTest Then FailedTest = True: Call AttachTemplate: Resume Next
    MsgBox ("'" & AutoTextName & "' autotext is missing." & Chr(10) & "Please replace it in the .dot main template file or replace the whole file.")
End Sub


Private Sub ReplaceTextInSelection(strFind$, strReplace$)
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = strFind: .Replacement.Text = strReplace
        .Forward = True: .Wrap = wdFindStop
    End With
    Selection.Find.Execute
    With Selection
        If .Find.Forward Then .Collapse Direction:=wdCollapseStart Else .Collapse Direction:=wdCollapseEnd
        .Find.Execute Replace:=wdReplaceOne
        If .Find.Forward Then .Collapse Direction:=wdCollapseEnd Else .Collapse Direction:=wdCollapseStart
        .Find.Execute
    End With
End Sub


Public Sub UpdateBookmark(doc As Document, BookmarkToUpdate$, TextToUse$)
    Dim tempStr$, i As Byte, BMRange As Range
    On Error GoTo ExitNow
    For i = 0 To 5
        If (i = 0) Then tempStr = BookmarkToUpdate Else tempStr = BookmarkToUpdate & i
        If ActiveDocument.Bookmarks.Exists(tempStr) Then
            Set BMRange = doc.Bookmarks(tempStr).Range
            BMRange.Text = TextToUse: doc.Bookmarks.Add tempStr, BMRange
            If (BookmarkToUpdate = "bmDate") Then BMRange.ParagraphFormat.Alignment = wdAlignParagraphLeft
        End If
    Next
ExitNow:
End Sub


Public Function fApplyStyle(StyleName$) As Boolean
    On Error GoTo Failed: Selection.Style = StyleName: fApplyStyle = True: Exit Function
Failed:
    MsgBox ("Please make sure the style '" & StyleName & "' is available." & Chr(10) & _
        "If it's missing, please put it back in this file and the template.")
End Function

<< Navigate to Monday, October 12, 2009 Add New Comment
No records found        
Add New Comment
Your name   
Subject   
Content   
*Required fields

January, 2009
February, 2009
March, 2009
April, 2009
May, 2009
June, 2009
July, 2009
August, 2009
September, 2009
October, 2009
November, 2009
December, 2009
IntroductionGetting StartedAll PostsNotesAbout The Author