Etudes for Microsoft Word Programmers

Home ] Up ] Etude 1.1. Text Fit ] Etude 1.2. Symbol Case ] Etude 1.3. Finding and Painting Hyperlinks ] Etude 1.4. Styles ] Etude 1.5. Inserting Formatted Text ] Etude 1.6. Text in the Column. ] Etude 1.7. Formatting Headline. ]


Etude 1.5. Inserting Formatted Text

A pattern is a recurring set of events or objects that repeat in a predictable fashion. It can be a template or model which can be used to generate things or parts of a thing, especially if the things that are created have enough in common for the underlying pattern to be inferred, in which case the things are said to exhibit the pattern.

http://en.wikipedia.org/wiki/Pattern

 

Resources

Template "AhTextDateline.dot", 24 Kb, ZIP [download].

Functional Specification

Task

The task is to insert formatted text from some template, prepared beforehand, to the current document. After the insertion the task is to replace the predefined tags in the inserted text according to the replacements table. The code and the "AhTextDateline" toolbar should be placed to the template "AhTextDateline.dot". The  "AhTextDateline" toolbar should contain buttons "Byline" (inserts Author) and "Dateline" (inserts date).

Preformatted template document "AhPatternByline.doc"

The template document "AhPatternByline.doc" looks like this:

In this template such tags as %AH_USER_NAME% and %AH_USER_ROLE% can be found along with any "ordinary" text. These tags should be replaced by the name and the role of the author respectively.

Preformatted template document "AhPatternDateline.doc"

The template document "AhPatternDateline.doc" looks like this:

In this template a tag like %AH_DATELINE% can be found along with the "ordinary" text. The tag should be replaced by the current date in the "MM/DD/YYYY" format.

 

Preformatted template documents "AhPatternByline.doc" and "AhPatternDateline.doc" should be in the same folder with the  "AhTextDateline.dot" template, which uses them.

Toolbar "AhTextDateline"

The toolbar "AhTextDateline" looks like this:

Brief description of the toolbar "AhTextDateLine" buttons is given in the table below.

Button

Macros

Description

Help

AhTextDatelineHelp

Brief prompt on the toolbar buttons.

Byline

AhTextByLineInsertTest

Insertion of the formatted text from the file "AhPatternByline.doc".
The tags %AH_USER_NAME% (author) and
%AH_USER_ROLE% (role) are replaced.

Dateline

AhTextDateLineInsertTest

Insertion of the formatted text from the file "AhPatternDateline.doc".
The tag %AH_DATELINE% is replaced with current date.

Brief Prompt

Brief prompt on the toolbar buttons looks like this:

Insertion of the "Byline"

Formatted text from the file "AhPatternByline.doc" is inserted into the opened document.
The tags %AH_USER_NAME% and %AH_USER_ROLE% are replaced by the name and the role of the author respectively.

An example of the inserted text follows.

Evgeny Akhundzhanov
The Author.

Insertion of the "Dateline"

Formatted text from the file "AhPatternDateline.doc" is inserted into the opened document.
The tag %AH_DATELINE% is replaced by the current date in the "MM/DD/YYYY" format.

An example of the inserted text follows.

Today is 12/14/2005

After insertion of both Byline and Dateline the opened document should look like this:

Implementation - Template "AhTextDateline.dot"

The macros for inserting the formatted text from preformatted documents are implemented in the "AhTextDateline.dot" template.

'
' File AhTextDateline.dot|AhTextDateline
'
' Etudes for Microsoft Word Programmers.
' Etude 1.5. Inserting Formatted Text.
'
' First published on http://www.transcriber.ru
'
' © 2000-2007. Evgeny Akhundzhanov. All rights reserved.
'
Option Explicit
 

 

Constants

'
' Constants
'
Const cStrPattByLn As String = "AhPatternByline.doc"
Const cStrPattDate As String = "AhPatternDateline.doc"

Messages

'
' Messages
'
Const cStrDateLineHelp As String = _
"Etudes for Microsoft Word Programmers." & vbCrLf & _
"Etude 1.5. Inserting Formatted Text." & vbCrLf & _
"http://www.transcriber.ru" & vbCrLf & vbCrLf & _
     "Template 'AhTextDateline.dot' - " & _
     "Inserting formatted text from external document." & vbCrLf & vbCrLf & _
     "Byline - inserting ByLine from '%1'" & vbCrLf & _
     "Dateline - inserting Dateline from '%2'"
Const cStrPatternFileNotFound As String = "%1 template file <%2> was not found."

Procedure AhTextDatelineHelp

'
' AhTextDatelineHelp
'
Sub AhTextDatelineHelp()
    Dim strDateLineHelp As String
    strDateLineHelp = cStrDateLineHelp
    strDateLineHelp = Replace(strDateLineHelp, "%1", cStrPattByLn)
    strDateLineHelp = Replace(strDateLineHelp, "%2", cStrPattDate)
    MsgBox strDateLineHelp
End Sub

Procedure AhUtilFileInsert

'
' AhUtilFileInsert
'
Private Function AhUtilFileInsert(ByVal FileName As String) As Boolean
    On Error GoTo ErrorLabel
    Selection.InsertFile FileName
    AhUtilFileInsert = True
    Exit Function
ErrorLabel:
    AhUtilFileInsert = False
End Function

Procedure AhUtilReplaceTag

'
' AhUtilReplaceTag
'
Private Sub AhUtilReplaceTag(ByVal strTag As String, ByVal strReplace As String)
Dim iNameLength As Long
Dim iSelStart As Long
    On Error Resume Next
    iSelStart = Selection.Start
    With Selection.Find
        .Forward = True
        .ClearFormatting
        .MatchWholeWord = False
        .MatchCase = False
        .MatchWildcards = False
        .Wrap = wdFindContinue
        .Text = strTag
        .Replacement.ClearFormatting
        .Replacement.Text = strReplace
        iNameLength = Len(.Replacement.Text)
    End With
    '
    ' just replace once
    '
    If Selection.Find.Execute(Replace:=wdReplaceOne) Then
        Selection.Start = iSelStart + iNameLength - Len(Tag)
    End If
End Sub

Procedure AhTextBylineInsert

'
' AhTextBylineInsert
'
Sub AhTextBylineInsert(ByVal strPatternFileName As String, ByVal strUserName As String, ByVal strUserRole As String)
Dim bOldReplacement As Boolean
    '
    ' save Application properties
    '
    With Application
        bOldReplacement = .Options.ReplaceSelection
        .Options.ReplaceSelection = False
        .ScreenUpdating = False
    End With
   
    '
    ' assume pattern file is in the same folder
    '
    Dim strFilePath As String
    strFilePath = ActiveDocument.Path + "\" + strPatternFileName
 
    '
    ' replace tags
    '
    If AhUtilFileInsert(strFilePath) Then
        AhUtilReplaceTag "%AH_USER_NAME%", Trim(strUserName)
        AhUtilReplaceTag "%AH_USER_ROLE%", Trim(strUserRole)
    Else
        Dim strMess As String
        strMess = cStrPatternFileNotFound
        strMess = Replace(strMess, "%1", "Byline")
        strMess = Replace(strMess, "%2", strPatternFileName)
        MsgBox strMess
    End If
   
    '
    ' restore Application properties
    '
    With Application
        .Options.ReplaceSelection = bOldReplacement
        .ScreenUpdating = True
    End With
End Sub

Procedure AhTextDatelineInsert

'
' AhTextDatelineInsert
'
Sub AhTextDatelineInsert(ByVal strPatternFileName As String, ByVal strDateLine As String)
Dim bOldReplacement As Boolean
    '
    ' save Application properties
    '
    With Application
        bOldReplacement = .Options.ReplaceSelection
        .Options.ReplaceSelection = False
        .ScreenUpdating = False
    End With
 
    '
    ' assume pattern file is in the same folder
    '
    Dim strFilePath As String
    strFilePath = ActiveDocument.Path + "\" + strPatternFileName
 
    '
    ' replace tags
    '
    If AhUtilFileInsert(strFilePath) Then
        AhUtilReplaceTag "%AH_DATELINE%", Trim(strDateLine)
    Else
        Dim strMess As String
        strMess = cStrPatternFileNotFound
        strMess = Replace(strMess, "%1", "Dateline")
        strMess = Replace(strMess, "%2", strPatternFileName)
        MsgBox strMess
        Exit Sub
    End If
   
    '
    ' restore Application properties
    '
    With Application
        .Options.ReplaceSelection = bOldReplacement
        .ScreenUpdating = True
    End With
End Sub

Procedure AhTextDateLineInsertTest

'
' AhTextDateLineInsertTest
'
Sub AhTextDateLineInsertTest()
    If Documents.Count = 0 Then Exit Sub
    '
    ' settings
    '
    Dim strDateLine As String
    strDateLine = CStr(Date)
   
    AhTextDatelineInsert cStrPattDate, strDateLine
End Sub
 

Procedure AhTextByLineInsertTest

'
' AhTextByLineInsertTest
'
Sub AhTextByLineInsertTest()
    If Documents.Count = 0 Then Exit Sub
    '
    ' settings
    '
    Dim strUserName As String
    Dim strUserRole As String
 
    strUserName = ActiveDocument.BuiltInDocumentProperties(wdPropertyAuthor)
    If (Len(strUserName) <= 0) Then
        strUserName = "User name here..."
        strUserRole = "User role here..."
    Else
        strUserRole = "The Author."
    End If
   
    AhTextBylineInsert cStrPattByLn, strUserName, strUserRole
End Sub

Conclusions

The operations of inserting the formatted text from the template documents prepared beforehand to the current document are investigated in this etude. The template "AhTextDateline.dot" which contains macros for inserting the formatted text and replacing tags is created on the basis of specification. The toolbar "AhTextDateline" provides a simple interface to the operations of inserting the titles "Byline" (the author) and "Dateline" (the current date).

If you perform the operation "Insert File" and then "Undo", the inserted text is deleted but its styles remain in the current document!

Tasks

Task 1

In the code above the authorís name comes from the 'Author' document property if it is not empty. Otherwise some hard-coded text is used ("hard-coded" means that the text is defined directly in the code). Add Form and corresponding code to allow the user to edit values of the replaced tags. Increase the tags list if necessary.

 

Task 2

It is evident that optimal work with documents in a group of people requires some organizing measures for the unification of the styles that are often in use. Letís make a suggestion that there exists a folder with all the samples created by all members of your company or by yourself during a year. Create macro which will process all the documents from this folder and gather all the employed styles in one document.

 

Task 3

Create a template which will contain all standard styles used by your company.

 

Task 4

How many tags are required for the following formatted text with voting results?

Picture from http://www.cnn.com/ELECTION/2004/pages/results/president/

 

 


Etudes for Microsoft Word Programmers. Etude 1.5.


Unless otherwise noted, all materials on this site are
© 2000-2009 Evgeny Akhundzhanov, All Rights Reserved Worldwide
Microsoft is in no way affiliated with, nor offers endorsement of, this site.
www.transcriber.ru | E-mail the Author