Developer FAQs - Editing Documents

Welcome to Baarns Publishing's Word Developers FAQ section. If you develop in Microsoft Word using Word Basic, these tips are for you. Feel free to copy any of the code you find here and use in your development projects.


How can I delete my unused Styles from my document?

How do you remove Document Variables?

How do I insert more than one paragraph at a time?

 

How can I delete my unused Styles from my document?
Keywords: WordBasic Styles Remove Error
Re-Posted July 21, 1997

It is not possible to remove unused built-in styles (Normal, Heading 1, Heading 2, ...) without create a new document. It is possible to remove the custom styles. Below is a macro that does this.

A few comments about the macro solution:

For versions of Word prior to Word 97 (8.0).

To use this macro:

  1. From the menu bar, select Tools - Macro
  2. In the Macro Name text box, insert, RemoveUnusedStyles
  3. Press the Create button
  4. A text window will appear. Delete the contents of the window.
  5. Enter the following WordBasic Code below into the macro window. Note: you can select the code in the Browser. Then copy and paste it into the WordBasic editor.
  6. Activate a document to test this on then run the macro (Tools Macro dialog).
  7. If you anticipate using this frequently, added it to a menu via Tools Customize Menus.
'***********************************************************************
' Author: Steven (Smitch) Mitchell
' Date:   5/15/96
' Copyright Baarns Consulting Group, Inc. 1996
'***********************************************************************
Sub MAIN
   'iStyCnt      Total number of styles.
   'iStyID       Current Style index.
   'szStyName$   Name of the style to remove.
   'Set the constants
   CONTEXT = 0         'List only styles in active document (CONTEXT=0)
   ALL = 0             'Exclude built in styles (ALL=0)
   'Get the number of styles in the document.
   iStyCnt = CountStyles(CONTEXT, ALL)
   'Ignore all errors, this can be dangerous. 
   'It is assume that all burps are self-correcting.
   On Error Resume Next
   'Clear all possible format attributes set in search criteria.
   EditFindClearFormatting
   '
   'Loop through the list of styles starting with the last style. Since 
   'the code deletes some of the styles along the way, the number of 
   'Styles will change.
   '
   For iStyID = iStyCnt To 0 Step - 1
      'Move to the begining of the document.
      StartOfDocument      
      'Extract the style name.
      szStyName$ = StyleName$(iStyID, CONTEXT, ALL)   
      'Check to see if the style exists.
      If StyleDesc$(szStyName$) <> "" Then
         'Specify the style to search for. To see if the style is in use.
         EditFindStyle .Style = szStyName$
   
         'Find the first instance of the style.
         EditFind .Find = "", .Direction = 0, .WholeWord = 0, .MatchCase = 0, \
.PatternMatch = 0, .SoundsLike = 0, .FindAllWordForms = 0, .Format = 1, .Wrap = 0
         'If style not found then delete the style name.
         If EditFindFound() = 0 Then
            'Built-in styles will NOT be deleted.
            FormatStyle .Name = szStyName$, .Delete
         End If
      End If
   Next
End Sub

For versions of Word 97 (8.0).

To use this macro:

  1. From the menu bar, select Tools - Macro
  2. In the Macro Name text box, insert, RemoveUnusedStyles
  3. Click the Create button
  4. A new application window will appear. This is the Visual Basic Editor (VBE). A subroutine will have been created for you.
  5. Enter the following VBA Code below into the VBE window. Note: you can select the code in the Browser. Then copy and paste it into the VBE editor.
  6. Activate a document to test this on then run the macro (Tools Macro dialog).
  7. If you anticipate using this frequently, added it to a menu via Tools Customize Menus.
'***********************************************************************
' Author: Steven (Smitch) Mitchell
' Date:   07/03/97
' Copyright Baarns Consulting Group, Inc. 1996 - 1997
'***********************************************************************
    Dim wrdStyle As Word.Style
    On Error GoTo ErrorHandler
    
    For Each wrdStyle In ActiveDocument.Styles
        '''Ignore all builtin styles not being modified.
        If wrdStyle.InUse = True Then
            With ActiveDocument.Range.Find
                .ClearFormatting
                .Style = wrdStyle.NameLocal
                If .Execute(Format:=True, Wrap:=wdFindStop) = False Then
                    '''Style was not found, so delete it.
                    wrdStyle.Delete
                End If
            End With
        End If
    Next
    
    Exit Sub
ErrorHandler:
    Select Case Err.Number
        '''Add specific cases here.
        Case 4198  '''Command failed
            '''Cannot delete built-in styles so ignore this error.
            Err.Clear
            Resume Next
        Case Else
            MsgBox Err.Number & ", " & Err.Description
            Err.Clear
            Resume Next
    End Select
			

How do you remove Document Variables?
Keywords: Document Variables
Re-Posted July 21, 1997

By setting the document variable to an empty string (""). This removes the document variable name and setting. In versions before Word 6.0c it was not possible to erase the variable name from the document.

Note in Word VBA if you delete a document variable, you will have to specifically add the variable back before you can use it. Therefore you should test to see that the variable exist before assigning a value. This was not the case in WordBasic.

WordBasic Command

SetDocumentVar sVarName$, "" 

Word VBA Command

ActiveDocument.Variables(szVarName).Delete 

How do I insert more than one paragraph at a time?
Keywords: Insert Multiple Paragraphs
Re-Posted July 21, 1997

The InsertPara statement is used to insert paragraphs. It does not accept any parameters for multiple paragraphs in one call. So the solution is to either write a simple routine to insert multiple paragraphs, or to insert the ANSI value of the paragraph, Chr$(10). The function method is best for solutions that will most likely ported to other platforms or future versions of Word for Windows.

WordBasic Command

    Sub InsertParas(pCount)
        For iLoop = 1 to pCount
            InsertPara
        Next
    End Sub 

Word VBA Command

Sub InsertParas(ByVal NumOfParas As Long)
    With Selection.Range
        For iCount = 1 To NumOfParas
            .InsertParagraph
        Next
    End With
End Sub

The following statement works in all Windows versions of Word to date. Where pCount is the variable that contains the number of paragraphs you would like to insert.

WordBasic Command

    Insert String$(pCount, Chr$(10)) 

Word VBA Command

   Selection.Range.InsertAfter String$(pCount, vbCr) 

Back to Top


Back Back to the FAQ Table of Contents


  Random Thoughts...
Life does not begin at conception, but when the kids leave home and the dog dies.


Copyright© 1996-1999, Baarns Consulting Group, Inc. - All rights reserved.