Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing cell borders in word table 1

Status
Not open for further replies.

Stoffel24

Technical User
Apr 4, 2002
121
ZA
Hi all
I don't know much about VBA so please pretend I am a little slow! I have a situation where a table is created that has some blank rows and cells. I want to format the table so that where there are blank rows, all the vertical borders are removed, and where there are blank cells, all borders are removed (except on the side where this cell borders a cell that does contain data). My thinking was that if I could write a macro that removed all borders from the table and then added borders for those cells that did contain data, then I would have the desired result. In addition, I want this macro to run automatically without user input. Does anyone have any code I could "borrow" or at least some suggestions.

Thanks
 
try something like...

Code:
Option Explicit

Sub test()
  Dim oDOC As Document
  Dim oTBL As Table
  Dim oCELL As Cell
  
  Set oDOC = ActiveDocument
  For Each oTBL In oDOC.Tables
    oTBL.Borders(wdBorderTop).LineStyle = wdLineStyleNone
    oTBL.Borders(wdBorderLeft).LineStyle = wdLineStyleNone
    oTBL.Borders(wdBorderBottom).LineStyle = wdLineStyleNone
    oTBL.Borders(wdBorderRight).LineStyle = wdLineStyleNone
    oTBL.Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
    oTBL.Borders(wdBorderVertical).LineStyle = wdLineStyleNone
    
    For Each oCELL In oTBL.Range.Cells
      If Len(oCELL.Range.Text) > 2 Then
        oCELL.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
        oCELL.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
        oCELL.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
        oCELL.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
      End If
    Next oCELL
    
  Next oTBL
End Sub
 
Justin you are the daddy! Thanks a lot. One more thing I am wondering is, "Is there a way to get a macro to autoexecute when word opens"?
 
from VBA help:

Auto Macros

By giving a macro a special name, you can run it automatically when you perform an operation such as starting Word or opening a document. Word recognizes the following names as automatic macros, or "auto" macros.

Macro name When it runs
AutoExec When you start Word or load a global template
AutoNew Each time you create a new document
AutoOpen Each time you open an existing document
AutoClose Each time you close a document
AutoExit When you quit Word or unload a global template
Auto macros in code modules are recognized if either of the following conditions are true.

· The module is named after the auto macro (for example, AutoExec) and it contains a procedure named "Main."
· A procedure in any module is named after the auto macro.

Just like other macros, auto macros can be stored in the Normal template, another template, or a document. The only exception is the AutoExec macro, which will not run automatically unless it is stored in the Normal template or a global template stored in the folder specified as the Startup folder.

In the case of a naming conflict (multiple auto macros with the same name), Word runs the auto macro stored in the closest context. For example, if you create an AutoClose macro in a document and the attached template, only the auto macro stored in the document will execute. If you create an AutoNew macro in the normal template, the macro will run if a macro named AutoNew doesn't exist in the document or the attached template.

Note You can hold down the SHIFT key to prevent auto macros from running. For example, if you create a new document based on a template that contains an AutoNew macro, you can prevent the AutoNew macro from running by holding down SHIFT when you click OK in the New dialog box (File menu) and continuing to hold down SHIFT until the new document is displayed. In a macro that might trigger an auto macro, you can use the following instruction to prevent auto macros from running.

WordBasic.DisableAutoMacros


Using Events with the Document Object

The Document object supports three events: Close, New and Open. You write procedures to respond to these events in the class module named "ThisDocument." Use the following steps to create an event procedure.

1 Under your Normal project or document project in the Project Explorer window, double-click ThisDocument. (In Folder view, ThisDocument is located in the Microsoft Word Objects folder.)
2 Select Document from the Object drop-down list box.
3 Select an event from the Procedure drop-down list box.

An empty subroutine is added to the class module.

4 Add the Visual Basic instructions you want to run when the event occurs.

The following example shows a New event procedure in the Normal project that will run when a new document based on the Normal template is created.

Private Sub Document_New()
MsgBox "New document was created"
End Sub

The following example shows a Close event procedure in a document project that runs only when that document is closed.

Private Sub Document_Close()
MsgBox "Closing the document"
End Sub

Unlike auto macros, event procedures in the Normal template don't have a global scope. For example, event procedures in the Normal template only occur if the attached template is the Normal template.

If an auto macro exists in a document and the attached template, only the auto macro stored in the document will execute. If an event procedure for a Document event exists in a document and its attached template, both event procedures will run.

Note For information on creating event procedures for the Application object, see Using Events with the Application Object.


Using Events with the Application Object

To create an event handler for an event of the Application object, you need to complete the following three steps:

1 Declare an object variable in a class module to respond to the events.
2 Write the specific event procedures.
3 Initialize the declared object from another module.

Declare the Object Variable

Before you can write procedures for the events of the Application object, you must create a new class module and declare an object of type Application with events. For example, assume that a new class module is created and called EventClassModule. The new class module contains the following code.

Public WithEvents App As Word.Application

Write the Event Procedures

After the new object has been declared with events, it appears in the Object drop-down list box in the class module, and you can write event procedures for the new object. (When you select the new object in the Object box, the valid events for that object are listed in the Procedure drop-down list box.) Select an event from the Procedure drop-down list box; an empty procedure is added to the class module.

Private Sub App_DocumentChange()

End Sub

Initializing the Declared Object

Before the procedure will run, you must connect the declared object in the class module (App in this example) with the Application object. You can do this with the following code from any module.

Dim X As New EventClassModule
Sub Register_Event_Handler()
Set X.App = Word.Application
End Sub

Run the Register_Event_Handler procedure. After the procedure is run, the App object in the class module points to the Word Application object, and the event procedures in the class module will run when the events occur.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top