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?
 
Chris BottomleyConnect With a Mentor Software 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
 
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.