×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Access to Word - Naming Sections For Use

Access to Word - Naming Sections For Use

Access to Word - Naming Sections For Use

(OP)
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.

RE: Access to Word - Naming Sections For Use

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,

https://engineervsheep.com

RE: Access to Word - Naming Sections For Use

(OP)
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.

RE: Access to Word - Naming Sections For Use

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,

glassesJust traded in my OLD subtlety...
for a NUance!tongue

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login



News


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close