Link to home
Start Free TrialLog in
Avatar of ericlockshine
ericlockshineFlag for United States of America

asked on

Selecting a Table in Word VBA then applying a Style to the entire area

Experts,
So I have a Word Template with VBA and Macros.  This template has a routine that embeds a table from Excel into the Word Document.  Within this Word document template are styles that are applied to the rest of the text, just not the Table.
Sample:
User generated image
What I have been able to do is select the starting ***, the text and the ending *** but NOT the table (I get only the text within the table).  What I need to be able to do is to select the entire area including the table and then reapply a Style to it.  The style in question allows me to hide the *** <text> ***.

Using VBA, I can get the First character within the table, and the Last character within the table.  What I then do is find my *** before the table and then the *** after the table.  I then apply the style to the newly selected range.

When I try to hide the text, the table border remains.  If I were to select the entire area, and reapply my style, then I am able to hide everything INCLUDING the table.  I can do this only directly in Word.

How can I select the entire area and then reapply the style, all in VBA?

Thanks!
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

sample code
'select the table

ActiveDocument.Tables(1).Select

' apply autofit

ActiveDocument.Tables(1).AllowAutoFit = True
see this link for other table properties
Avatar of ericlockshine

ASKER

the AllowAutoFit only gives the table the ability to fit within the text.  This doesn't apply a style to the table and text (which is what I need to do), unless I am missing something.
did you look at the link I posted for the table properties?

something like this

dim rng As Word.Range
rng.Font.Hidden = True

or something like this

selection.font.hidden=true
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Ray,
Yes, I glanced at the links.  I will check it out again on Monday when I get back into the office.
Thanks for the assist!

Hi GrahamSkan,
I will have to try it when I get back to the office on Monday.

If this doesn't work, I will strip out any proprietary code and upload the documents.

Thanks again
Guys,
I used both your suggestions, but it was actually much simpler than I thought.  Since I already had a selection object I just did this:
Dim objStyle as Style
objStyle = Selection.Style
<other code>
Set objWorkBook = objExcel.Workbooks.Open(sNameFilt)
objWorkBook.ActiveSheet.Range(objWorkBook.ActiveSheet.Cells(1, 1), objWorkBook.ActiveSheet.Cells(iRows, iCols)).Copy

Selection.InsertBefore vbCrLf                   ' ----- Insert a carriage return, then skip past it...
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdMove
				
Selection.Paste
Selection.Tables(1).Range.Select
Selection.Style = objStyle

Open in new window


Thanks for the assist.
Again, thanks!  I needed the right push!