Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Access to Word - Naming Sections For Use

Status
Not open for further replies.

Cobra17

Mechanical
Jun 18, 2020
158
0
0
CA
is there a way to add a "description" to a section in VBA so that I can reference it when coding? so if I insert a new section I don't have to adjust all my section call outs

currently I'm creating a section from Access VBA to Word like so:

Code:
         wd.Selection.InsertAfter Text:=vbTab & "TAG #" & vbTab & vbTab & "QTY" & vbTab & "DESCRIPTION" & vbCr
wd.ActiveDocument.Sections(1).Range.Font.Size = 11
wd.ActiveDocument.Sections(1).Range.Font.Bold = False
wd.ActiveDocument.Sections(1).Range.Font.AllCaps = True
wd.ActiveDocument.Sections(1).Range.Font.Underline = False
wd.ActiveDocument.Sections(1).Range.Font.Italic = False
        wd.Selection.Collapse wdCollapseEnd
        wd.Selection.InsertBreak Type:=wdSectionBreakContinuous
        wd.Selection.TypeParagraph

I'd like to be able to create the section and reference it like "wd.ActiveDocument.Sections("Header").Range.Font.Size = 11" or something.. I currently have 20 sections and its a pain to go back and update all of them.
 
Replies continue below

Recommended for you

Not immediately aware of a way, but you can vastly simplify your code using 'with' statements.

Code:
With wd.ActiveDocument.Sections(1).Range.Font
.Size = 11
.Bold = False
.AllCaps = True
.Underline = False
.Italic = False
  End with

Least this will mean a whole lot less to update,

 
I did figure out how to grab the section

Code:
 SecNum = (wd.ActiveDocument.Range(0, wd.Selection.Paragraphs(1).Range.End).Sections.Count)]

and then
Code:
 With wd.ActiveDocument.Sections(SecNum).Range.Font]

At the start I put in the first bit of code, then my text and what not, then run the second code section... my 'with statement', then put in a section break , then the first code again. I also streamlined it since I got it working how I wanted...

Code:
 'Heading
        SecNum = (wd.ActiveDocument.Range(0, wd.Selection.Paragraphs(1).Range.End).Sections.Count)
            wd.ActiveDocument.Sections(SecNum).Range.Style = "Heading 1"
            With wd.Selection
                .InsertAfter Text:="HEADING" & vbCr
                .Collapse wdCollapseEnd
                .InsertBreak Type:=wdSectionBreakContinuous
                .TypeParagraph
            End With
            
     'Description
        SecNum = (wd.ActiveDocument.Range(0, wd.Selection.Paragraphs(1).Range.End).Sections.Count)
            wd.ActiveDocument.Sections(SecNum).Range.Style = "Normal"
            With wd.Selection
                .InsertAfter Text:=vbTab & "DESCRIPTION" & vbCr & vbCr & vbCr
                .Collapse wdCollapseEnd
                .InsertBreak Type:=wdSectionBreakContinuous
            End With

rinse and repeat.
 
I am not conversant in MS Word per se, but I am familiar with VBA in general and specifically how Collections are handled when using the Add Method.

Add adds an object to the referenced Collection to

Collection.Count + 1

So that directly AFTER Add, if you were to Count the collection, the Collection.Count would be the Index of the newly added Collection Item.

Collection(Collection.Count)

You can assign a Name to any Collection Item and reference any Collection Item by either Name or Index.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.
Back
Top