Developer FAQs - Editing DocumentsWelcome 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?
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:
'***********************************************************************
' 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:
'***********************************************************************
' 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.
SetDocumentVar sVarName$, ""
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.
Sub InsertParas(pCount)
For iLoop = 1 to pCount
InsertPara
Next
End Sub
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.
Insert String$(pCount, Chr$(10))
Selection.Range.InsertAfter String$(pCount, vbCr)
Back to the FAQ Table of Contents
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. |