| name | word-vba-reserved-words |
| description | Detect and avoid Microsoft Word / VBA reserved words in identifiers. |
| license | CC-BY-4.0 |
Word/VBA Reserved Words – Naming Safety Skill
Purpose
Help developers avoid case-insensitive identifier collisions in Word VBA.
Collision Categories
VBA keywords & operators
Dim, If, Select, Function, ByVal, And, Or, Not, Set, Let, New, With, etc.
Built-in VBA function names
InStr, Left, Right, Mid, Len, Date, Time, Now, Format, Replace, Split, Join, Trim, etc.
Word object model names
Document, Documents, Paragraph, Paragraphs, Range, Selection, Table, Tables, Cell, Cells, Row, Rows, Column, Columns, Bookmark, Bookmarks, Style, Styles, Section, Sections, HeaderFooter, Field, Fields, Shape, Shapes, InlineShape, InlineShapes, ContentControl, ContentControls, Comment, Comments, Revision, Revisions, Find, Replacement, MailMerge, Footnote, Endnote, Hyperlink, ListFormat, PageSetup, Font, ParagraphFormat, Borders, Shading, Window, Pane, View, Options, Template, FormField, Variable, AutoText, etc.
VBA/Word SQL and special keywords
Name, Text, Value, Type, Index, Count, Item, Add, Delete, Open, Close, Save, Print, Execute, etc.
Special characters/symbols
Spaces, ', ", ., !, ?, *, +, -, =, <, >, #, %, $, &, @, \, /, ^, ~, {}, [], ().
Word and VBA treat names case-insensitively; reusing these terms as identifiers often leads to compile or runtime errors.
Procedure
Priority: Complete Steps 1–4 in order. Step 5 is optional and should be considered only when explicitly requested.
Step 1: Identify Reserved Words (Priority)
- Scan identifiers in the current context (variables, procedure names, module names, bookmark names, content control tags, and macro names).
- Flag exact case-insensitive matches to reserved words and report each occurrence with the specific line number and file name.
- Partial-match guidance: Identifiers that contain a reserved word as a complete substring (e.g.,
DocumentPath, RangeStart) should be noted as potential concerns, but only exact or very obvious collisions require immediate action. Use judgment based on context.
Step 2: Suggest Safe Replacements
- Propose descriptive, context-specific names:
Document → docTarget or docReport
Range → rngSelection or rngParagraph
Selection → selCurrent or rngSelected
Table → tblData or tblSummary
Cell → celTarget or celValue
Name → sDocName or sBookmarkName
Text → sBodyText or sContent
Step 3: Apply Naming Conventions (After renaming)
- Ensure correct formatting:
- Use CamelCase (no spaces or special characters).
- Optionally apply Leszynski/Reddick-style type/object prefixes (e.g.,
strName, lngCount, dtmStart, docMain, rngTarget).
Step 4: Handle Existing Objects
- If renaming VBA objects already in use, prefer renaming the object rather than attempting workarounds; clear, descriptive names prevent subtle bugs and improve maintainability.
Step 5: Repository Analysis (Optional)
- (Optional) Run a repository scan when requested to list offenders and propose bulk renames.
Common Offenders (teach by example)
- Word object names used as identifiers:
Document, Range, Selection, Table, Cell, Row, Column, Style, Section, Field, Comment, Bookmark, Shape
- Generic property names:
Name, Text, Value, Type, Index, Count
- VBA keywords:
date, time, now, format, replace
- VBA functions used as names:
instr, left, right, mid, len, trim
Naming Recommendations
Word-specific prefixes:
| Prefix | Meaning | Example |
|---|
doc | Document | docReport, docTemplate |
par | Paragraph | parFirst, parSummary |
rng | Range | rngHeader, rngBody |
tbl | Table | tblData, tblPricing |
cel | Cell | celTotal, celName |
sty | Style | styCustom, styTitle |
sec | Section | secCover, secAppendix |
bkm | Bookmark | bkmIntro, bkmSignature |
shp | Shape | shpLogo, shpArrow |
fld | Field | fldDate, fldAuthor |
cc | Content Control | ccStatus, ccSummary |
General rules:
- ✅ Prefer descriptive, specific names:
docInvoice, rngHeaderText, tblEmployees.
- ✅ Use CamelCase; consider type/object prefixes (
str, lng, dtm, doc, rng) for clarity.
- ❌ Avoid reserved words and special characters in any identifier.
- ❌ Avoid generic names like
Data, Info, Temp when they collide with object model terms.
Example Prompts (to trigger this skill)
- "Scan this Word VBA module and flag any reserved-word variable names."
- "Suggest safe replacements for variables named
Document and Range."
- "Check my Word macro for identifiers that collide with the Word object model."
- "Rename bookmark names that use reserved words like
Name or Text."
References
- Microsoft: Word object model overview
- Microsoft: VBA language keywords & reserved identifiers
- Microsoft: Naming conventions for VBA
- Allen Browne: Problem names & reserved words