+ Reply to Thread
Results 1 to 7 of 7

Thread: Word Table of Contents from Excel

  1. #1
    Registered User
    Join Date
    08-01-2010
    Location
    Swindon, Wiltshire
    MS-Off Ver
    Excel 2003
    Posts
    30

    Thumbs up Word Table of Contents from Excel

    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 = wdIndexIndent
    End With
    Last edited by BillWilts; 11-11-2010 at 03:49 AM.

  2. #2
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Word Table of Contents from Excel

    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



  3. #3
    Registered User
    Join Date
    08-01-2010
    Location
    Swindon, Wiltshire
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: Word Table of Contents from Excel

    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

  4. #4
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Word Table of Contents from Excel

    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



  5. #5
    Registered User
    Join Date
    08-01-2010
    Location
    Swindon, Wiltshire
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: Word Table of Contents from Excel

    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?

  6. #6
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Word Table of Contents from Excel

    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



  7. #7
    Registered User
    Join Date
    08-01-2010
    Location
    Swindon, Wiltshire
    MS-Off Ver
    Excel 2003
    Posts
    30

    Re: Word Table of Contents from Excel

    Thanks for your response. I have incorporated that piece of code into the routine that creates the Word document which now works as intended.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0