PDA

View Full Version : Solved: Word 97 problem with table creation code-cross platform problem



lucas
04-16-2008, 06:40 AM
I have a template that creates a table. This works fine in 2003 but I have a user with 97 that says it doesn't work but I have no way of even checking it let along trying to fix it as I don't have 97. Anyone have an idea of how to do this that will work across all platforms?

Option Explicit
Sub Test()
With ActiveDocument
' ActiveDocument.Tables(1).Delete
Selection.HomeKey Unit:=wdStory

Dim Tbl As Table
Set Tbl = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=1, NumColumns:=1, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed)
'next line hides the border on the table
Selection.Tables(1).UpdateAutoFormat
Selection.TypeText Text:="Investment Name"
End With
End Sub

lucas
04-16-2008, 06:49 AM
Edit: The code from post #1 is contained in the file attached to this post.

While I'm asking about cross platform, can someone who uses 2007 tell me how my menu and code will run on 2007 please.

Menu is after Help and it is named "Abstract Mgmt". Two of the menu items assign a style to the active cell and the last item creates a table of contents.

I would like to know if this menu will be available to users of 2007 and whether the code runs on 2007. If someone could take a minute to check this for me I would really appreciate it.

lucas
04-16-2008, 09:51 AM
Ok, I got the 97 compatability problem fixed I think. I changed the code above to this to add the table:

Dim Tbl As Table
Set Tbl = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=1, NumColumns:=1, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed)

And I changed the create table of contents code to this:
.TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
LowerHeadingLevel:=3, IncludePageNumbers:=True, AddedStyles:=""
.TablesOfContents(1).TabLeader = wdTabLeaderDots

If anyone could test this in any other version I would appreciate it. There is a menu item next to help.....run the Table of contents item.

I have tested it in 97 & 2003

perhol
04-16-2008, 10:30 AM
Hi Steve
I am using Office 2003, but downloaded your file to learn from.
Thought i would let you know about an error i recieved.

Running the 'Create Table of Contents' from your menu i got this error:


Run-time error '5941':

Member of selection dos not exist.

Debugging said that this line was wrong:

Selection.Style = ActiveDocument.Styles("Heading 2")

In 2 instances of that line i changed ("Heading 2") to ("HeadingSub") and the error was gone!

lucas
04-16-2008, 11:02 AM
Thanks perhol, I still wish someone with 2007 would tell me what happens when you run it.

Here is the final so far....let me know if you find any more errors.

This one should run in 97-2003 without problem.

perhol
04-16-2008, 03:59 PM
I have got a friend to run it in Excel 2007.
Hi had to use [alt]+[F8] and run the macro TOC, but it was functioning OK.
You see, i don't have excel 2007, and he dos not know what he is doing in Excel ;-)

lucas
04-16-2008, 06:04 PM
Thanks perhol, that's about what I expected.

lucas
04-17-2008, 06:53 AM
I'm marking this solved as I seem to have everything working ok. If anyone has any ideas about the menu situation in 2007 I would appreciate any input. It has not become an issue with this project but it inevitably will come up later.