Word VBA. Insert period at end of cell in table

hello,

I am new at Word VBA, and I would like to know if there is a way to have a macro that would insert a period at the end of a text string in every cell in the third column, of a table  in Word. (ie not insert a period for text that is not in a table).

Additionally, can the macro first check to see if there is already a period.

thanks,

Paul
pweber_epAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris BottomleySoftware Quality Lead EngineerCommented:
Indeed ... how would the table be identified, all of then certain one or the one currently selected?

Chris
0
pweber_epAuthor Commented:
Chris,

all tables in document,however there should only be one. Table will have 3 columns and varying number of rows. Again, I only need period appended to 3rd column.

thanks!

Paul
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Try this:

Chris

To Create a macro:
------------------

Alt + F11 to open the macro editor

  For User Code:
     Insert | Module to insert a code module into the project
     In the project tree select the module.
     Insert the required macro(s) into the selected module, ('Module1' or similar)

Close the Visual Basic Editor.

Check Security as appropriate:
------------------------------

In the application select Tools | Macro | Security
2003 and Earlier : Select Medium
2007 : Outlook - Warnings for all Macros
     : Others - Enable a trusted location and inhibit macros otherwise so do both!
      : Disable Macros
            Office Button, (top left of the screen)
            Options
            Trust Centre
            Trust Centre Settings
            MAcro Settings
            Disable All MAcros with warnings
      : Enable Trusted Locations
            Office Button, (top left of the screen)
            Options
            Trust Centre
            Trust Centre Settings
            Trusted Locations
            Add a preferred location
            ENSURE YOUR FILE IS IN A TRUSTED LOCATION
            Re-open to ensure it is recognised if the above has been changed
            
Select OK

To run a macro:
---------------

Alt + F8
Select the macro
Select 'Run'


Sub col3()
Dim tbl As Table
Dim cel As Cell
Dim rng As Range
Dim str As String

    For Each tbl In ThisDocument.Tables
        For Each cel In tbl.Columns(3).Cells
            str = Left(cel.Range.Text, Len(cel.Range.Text) - 2)
            If str <> "" Then
                If Right(str, 1) <> "." Then
                    cel.Range.Text = str & "."
                End If
            End If
        Next
    Next
    
End Sub

Open in new window

0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

pweber_epAuthor Commented:
John,

when I run the macro it skips the table.
I used F8, and watched it skip the table.

Attached is my sample word document.

thanks,

example.doc
0
Chris BottomleySoftware Quality Lead EngineerCommented:
I've just run it with the code as posted no problem ... it works on the specific document where the code is placed.  To run it on the activedocument use:

Chris
Sub col3()
Dim tbl As Table
Dim cel As Cell
Dim rng As Range
Dim str As String

    For Each tbl In activedocument.Tables
        For Each cel In tbl.Columns(3).Cells
            str = Left(cel.Range.Text, Len(cel.Range.Text) - 2)
            If str <> "" Then
                If Right(str, 1) <> "." Then
                    cel.Range.Text = str & "."
                End If
            End If
        Next
    Next
    
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkunreinCommented:
How odd.  John's macro should have worked. I even tested the code on the sample document.  By any chance, did you have another Word document open?  Make sure that your document is the selected one (close all other Word documents).

Another thought is that I normally use ActiveDocument instead of ThisDocument. I've not seen that before. I wonder if it is a Word 2007 invention. I don't know what version you're using.  I no longer have Word 2003, so I can't see if earlier versions of Word would choke on this or not.  But, if that were the case, you'd surely be getting a compile error, so I doubt that's it.
0
pweber_epAuthor Commented:
Chris,

(not John, sorry)
thanks! Updated code works.
I do not know why the original was not working. I did have only one document open.
I am using Word 2010 beta - maybe that was the fly in ointment?

Thanks!

Paul
0
Chris BottomleySoftware Quality Lead EngineerCommented:
Since you stepped it through it can't have been macro security so I cannot begine to think why not ... but i'm glad it worked out in the end.

jkunrein:

Activedocument has been there for some time ... I use it in 2003 and 2007.

Chris
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.