I am creating an Excel application to write out a Word document based on the contents of various cells. No problem with creating the document and writing the text. But, I'm stuck on creating the table of contents. This code generates the error "Wrong number of arguments or invalid property assignment" but I can't work out what is wrong. Any ideas?
With wrdDoc.TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _ True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _ LowerHeadingLevel:=3, IncludePageNumbers:=True, AddedStyles:="", _ UseHyperlinks:=False, HidePageNumbersInWeb:=True, UseOutlineLevels:=True .TablesOfContents(1).TabLeader = wdTabLeaderDots .TablesOfContents.Format = wdIndexIndentEnd With
Last edited by BillWilts; 11-11-2010 at 03:49 AM.
This will do the trick:
Sub tst() with wrDoc With .TablesOfContents.Add (Selection.Range, True, 1, 3, , , True, True, "", False, True) .TabLeader = 1 .Format = 0 End with End With End Sub
Thanks for the reply. Unfortunately I'm still getting the same error!! It might be because I have references to objects mixed up but in spite of experimenting I'm still no further forward. This is my current piece of code:
' These are the references for the code Set wrdApp = CreateObject("Word.Application") Set wrdDoc = wrdApp.Documents.Add(Template:=FullDotName) With wrdApp With .Selection .Font.Name = "Arial" .TypeParagraph .Font.Bold = FontBld .Font.Underline = FontUnd .Font.Size = FontSize .Style = wrdDoc.Styles(ParaStyle) ' Insert table of contents if the "text" to be entered at this point is the ' table of contents If ExcelTxt = "#tableofcontents" Then ' ### ' This line gives the error "Wrong number of arguments or invalid property assignment" With .TablesOfContents.Add(Selection.Range, True, 1, 3, , , True, True, "", False, True) .TabLeader = 1 .Format = 0 End With Else .TypeText Text:=ExcelTxt If ParaNLNP = "New Page" Then .TypeText Text:=Chr(12) If ParaNLNP = "New Paragraph" Then .TypeText Text:=Chr(13) & Chr(13) End If End With End With
Be very careful where to place code that supposes a with statement.
You can't add a table of contents to a selection.
Sub tst55() With GetObject(, "Word.Application") If .documents.Count = 0 Then With .documents.Add With .TablesOfContents.Add(.paragraphs(1).Range, True, 1, 3, , , True, True, "", False, True) .TabLeader = 1 End With End With End If End With End Sub
After much frustrating trial and error I still can't get it to work. The problem seems to be in addressing the ActiveDocument. This piece of code works in a Word document (slightly modified) but not from Excel. Any further thoughts?
Your statement isn't correct.
You'd better not use trial and error, but analyse the code instructionwise. If you know what the code does and why you can effectively adapt it to your wishes.
Sub snb() With CreateObject("Word.application").documents.Add .Application.Visible = True With .TablesOfContents.Add(.paragraphs(1).Range, True, 1, 3, , , True, True, "", False, True) .TabLeader = 1 End With End With End Sub
Thanks for your response. I have incorporated that piece of code into the routine that creates the Word document which now works as intended.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks