Solved

Insert table of cont. into new Word doc (Yes, this is a Notes Q)

Posted on 2001-07-03
20
1,158 Views
Last Modified: 2013-12-18
I have a script agent that runs on selected documents that takes the Word file attachments from those documents and merges them into one new Word file (thanks to stamp). The script begins like this:

==== BEGIN CODE ====
Dim session As New NotesSession
Dim db As NotesDatabase
Dim collection As NotesDocumentCollection
Dim word As Variant

Set db = session.CurrentDatabase
Set collection = db.UnprocessedDocuments
Set word = CreateObject("Word.application")
word.documents.open"X:\Templates\Design\Merge Blank.doc"
word.Application.Visible = True
word.Application.Selection.TypeText("Document merged from Notes Database ")
word.Application.Selection.InsertDateTime
word.Application.Selection.TypeParagraph
word.Application.Selection.TypeParagraph
word.Application.Selection.InsertBreak 2
===== END CODE =====

From there, it cycles through the unprocessed documents, merging the attachments. (There are other objects declared for those, edited here for simplicity.)

What I would like to do is insert a table of contents at the beginning of the file.

I've tried adding this just before the word.Application.Selection.InsertBreak 2

==== BEGIN CODE ====
word.Application.Selection.TablesOfContents.Add word.Application.Selection.Range, True, 1, 2, False, 1, True, True
===== END CODE ====

The parameter list for the TablesOfContents.Add method (listed under the heading of the Microsoft Word 9.0 Object Library in the list of OLE Classes) is:

Add(range, optional useheadingstyles as variant, optional upperheadinglevel as variant, optional lowerheadinglevel as variant, optional usefields as variant, optional tableid as variant, optional rightalignpagenumbers as variant, optional includepagenumbers as variant, optional addedstyles as variant, optional usehyperlinks as variant, optional hidepagenumbersinweb as variant) as variant

When I run this, I get a pop-up error: "Instance member TABLESOFCONTENTS does not exist" and the script stops running.

I've tried it with word.Application.Selection.TablesOfContents.Add, with word.Application.TablesOfContents.Add, and with word.TablesOfContents.Add, each time getting the same error.

I imagine there's some quirky little thing that I'm just missing, as this is my first poke through the OLE Classes list, and I'm not real sure what I'm doing. Can someone point me in the right direction?

Thanks!

-- b.r.t.

0
Comment
Question by:BarryTice
  • 10
  • 6
  • 2
  • +2
20 Comments
 
LVL 10

Expert Comment

by:zvonko
Comment Utility
Hello  b.r.t.,

I have changed my account from stamp to zvonko. Unfortunatelly I could not take my points with me :-)) So please increase the points for this. I will help you as good as I can (and I can!).

So, first of all: there is a discrepance in your OLE code at the very beginning.
You instantiate an object of type "Word.application" and call from this instance:
word.Application.Visible = True

This property is present, but confuse everyone reading this...

My code starts with:
Set word = CreateObject("Word.Document")

so I can add without problems this after looping trough selection and adding all documents:
word.Application.Selection.GoTo 0, 1, 1, ""   '//** decription bellow
'//** Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=1, Name:=""
word.Application.Selection.InsertBreak 7
'//** Selection.InsertBreak Type:=wdPageBreak
word.Application.Selection.GoTo 0, 1, 1, ""
word.TablesOfContents.Add word.Application.Selection.Range, True, 1, 2, False, 1, True, True
word.SaveAs( "c:\temp\Collect" & "0001" & ".doc" )
word.Application.Quit
Set word = Nothing


You will to insert something like:
word.ActiveDocument..TablesOfContents.Add ...

So please either place the complete code here or send it to stamp@worldonline.de
(...and increase the points ;-))

Regards,
zvonko
0
 
LVL 13

Expert Comment

by:cri
Comment Utility
Easy question, huh ?

IMO this will only work if the documents you are merging use a well defined set of styles, i.e. titles of paragraphs are defined as headings. To my experience most users do not even know about this and keep on 'typewriting'. What could be done is some kind of list of the merged file.
0
 
LVL 6

Expert Comment

by:ghassan99
Comment Utility
Stamp why did u change your login???

-Gus
0
 
LVL 22

Expert Comment

by:Dreamboat
Comment Utility
I agree with cri. It's quite doubtful that you'll be able to automate a TOC on documents that you receive from others unless they're adhering to strict templates/guidelines of using styles in the documents. You might as well just give him the points and start creating templates for the senders to use, or trash the whole TOC idea altogether.
0
 
LVL 10

Expert Comment

by:zvonko
Comment Utility
Hy Gus,

when I was creating my EE account in 1998 I was checking only the account "Zvonko". It was in use, so I choose this stupid synonym. Now I have to pay for this ;-)
The Proppercase name was registered by an Croatian who newer posted nor asked for eneything. But the the EE comunity support told me: it is not a problem; take the first letter lowercase ans it will work.
So, I start now from scratch to catch you and Heman ;-)
On other hand I discowerde that JavaScript is in some cases more technical  than this endless jokes from Arun and company...
Se we will see.

By the way: I have just now a big problem with categorized views in a monster database showing several hundred thousend document in nine categories (all docs with readerfields of corse). I solwed it (partaialy) with JavaScript. (...and the rest I hope to solve also with JavaScript).
So if not here #1 so look for me in the JavaScript area. I tell you: they even do not imagine wath is possible with JavaScript and Domino...

Forgive me, I start to talk around like the  others I ment in the starting ;-))
0
 
LVL 6

Expert Comment

by:ghassan99
Comment Utility
Goodluck stamp...oops or zvonko:)
But its really a waste to lose all those points you have gathered over the years, cant you try and ask EE to move them to your account?  Or are you after more shirts?:)

-Gus
0
 
LVL 10

Expert Comment

by:zvonko
Comment Utility
Ha ha, I do not beleve to get even those from stamp. Until now I got only ONE!!!
And look for the shirts now. The rules are not more described. I mean the old experts will surely get them (in the far future), but the new candidate even do not know about this...
Transforming points and question history is not realised until now in EE, so bad luck for me. The only offer EE comunity support made to me was to give me question points from stamp (ha ha), but no expert points or EE pro status. But newer mind. It make it more interesting to make a comeback.
The only problem is that I got visit from Makedonia for three monts. So I can not hang in front of my PC all the time. This week they are visiting other relatives so I have holidays...

(Wath was the question here?)
0
 
LVL 7

Author Comment

by:BarryTice
Comment Utility
Hello again, stamp/zvonko, and all else.

I do not remember specifically why I am using the code the way I am, other than that's the way it works. I've cobbled together a few bits and pieces of things, including the code from http://www.experts-exchange.com/jsp/qManageQuestion.jsp?qid=20109367&jsessionid=305560988410743933 to merge the documents in a sorted order. That code was provided by HemanthaKumar. You (stamp) proposed an alternate method in that Q, but my first tests of your alternate method proved slow on a merge of 200 documents, which is well within the range of what a person might merge. By the time other methods were suggested, I had implemented the bubble sort, which gave a delay of about 7 seconds for 200 records (on a good day).

Given that they let me do Notes development when I have gaps in my real job (and that there is no other Notes developer in the building...), time pressures left the working script in place with no further experimentation, in spite of your plea to try more elegant means (a concept with which I agree, as time allows).

I'm taking a few days off around the U.S. holiday, and won't be back in the office until Monday. (I don't have a copy of the database on my computer at home -- and like to keep it that way!) At that time I'll post the complete code as it stands, and add some points to get you back on your way to the top, if that's what it takes. (A look at my earlier posts will tell you that I'm not stingy with points when I have them to spend. I often post extra questions to other contributors to my questions, whether their answers were used or not, as long as they were given in the spirit of trying to solve my problems.

As for the practicality of having the TOC work, that shouldn't be a problem. In this case, the design documents I'm merging are each created from a specific template with a basic structure already defined. Users merely create a new document from the template and fill in all the proper data under the existing headings. This way, their lack of understanding of heading styles, etc., is a blessing, as they are less likely to try to "fix" the styles that are already in place.

Thanks again, all. I'll post more Monday.

-- b.r.t.
0
 
LVL 10

Expert Comment

by:zvonko
Comment Utility
Hello BarryTice,

I think like you that there should be no problem to implemet this TOC. Also implementing this into your actual running code is the best way of doing with less truble.
I am this week (and hope also next week) sitting at home curing a sports injury ;-) So I have planty of time to test for you. I can inplant the TOC into this buble sort solution (hi hi), but I will offer also the NotesViewNavigator solution. Your last question led me definitly to this feature and I am excited of it.

So bring me so fast you can your actuall code ;-)

So long,
zvonko

PS: If you like, send the code to stamp@worldonline.de <|;-)
0
 
LVL 7

Author Comment

by:BarryTice
Comment Utility
zvonko --

Here's the script I've been using. Have fun with it!

==== BEGIN CODE ====
Sub Initialize
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim collection As NotesDocumentCollection
    Dim doc As NotesDocument
    Dim docarray() As NotesDocument
    Dim word As Variant
    Dim extname As String
    Dim i As Integer
   
    Set db = session.CurrentDatabase
    Set collection = db.UnprocessedDocuments
    Set word = CreateObject("Word.application")
    word.documents.open"X:\Templates\RD & Design\TD Merge Blank.doc"
    word.Application.Visible = True
    word.Application.Selection.TypeText("Document merged from Notes Database ")
    word.Application.Selection.InsertDateTime
    word.Application.Selection.TypeParagraph
    word.Application.Selection.TypeParagraph
    word.Application.Selection.InsertBreak 2
   
    Call CollectionToSortedArray(collection, docarray())
   
    For i = 0 To (collection.Count - 1)
        Set doc = docarray( i )
        Forall item In doc.Items
            If (item.type=RICHTEXT)Then
                If Isarray(item.EmbeddedObjects) Then
                    Forall o In item.EmbeddedObjects
                        If (o.type=EMBED_ATTACHMENT) Then
                            extname=Ucase$(Right$(o.Name,3))
                            If extname="DOC" Then
                                Print "Adding: " & o.Name
                                Call o.extractfile ("c:\" & o.Name)
                                word.Application.Selection.InsertFile("c:\" & o.Name)
                                Kill "c:\" & o.Name
                                word.Application.Selection.InsertBreak 2
                                ' break 7 is page break
                                ' break 2 is section break, next page
                            End If
                        End If
                    End Forall
                End If
            End If
        End Forall
    Next
    Set word = Nothing
End Sub


Sub Swap(doc1 As NotesDocument ,doc2 As NotesDocument)
    Dim d As NotesDocument
    Set d = doc1
    Set doc1 = doc2
    Set doc2 = d    
End Sub


Sub CollectionToSortedArray(col As NotesDocumentCollection, docs() As NotesDocument)    
    Dim i As Integer
    Dim doc As NotesDocument
    Dim n As Integer, sorted As Integer
    Dim s1, s2 As String
   
    n = col.count
    Redim docs(n) As NotesDocument
 ' push to array
    For i = 0 To n - 1
        Set docs(i) = col.GetNthDocument(i+1)
    Next
   
 'bubble sort array
    sorted = False
    Do While (sorted = False)          
        sorted = True
        For i = 0 To n - 2
     ' sort based on CR No. field
            s1 = docs(i).CLARIFY_NUMBER(0)
            s2 = docs(i+1).CLARIFY_NUMBER(0)
            If(Strcomp(s1, s2, 5) = 1) Then  'No Pitch, No Case Compare
                Call Swap(docs(i),docs(i+1))
                sorted = False
            End If
        Next
    Loop
   
    sorted = False
    Do While (sorted = False)          
        sorted = True
        For i = 0 To n - 2
     ' sort based on Object field
            s1 = docs(i).Object(0)
            s2 = docs(i+1).Object(0)
            If(Strcomp(s1, s2, 5) = 1) Then  'No Pitch, No Case Compare
                Call Swap(docs(i),docs(i+1))
                sorted = False
            End If
        Next
    Loop
   
    sorted = False
    Do While (sorted = False)          
        sorted = True
        For i = 0 To n - 2
     ' sort based on Document Type field
            s1 = docs(i).Document_Type(0)
            s2 = docs(i+1).Document_Type(0)
            If(Strcomp(s1, s2, 5) = 1) Then  'No Pitch, No Case Compare
                Call Swap(docs(i),docs(i+1))
                sorted = False
            End If
        Next
    Loop
End Sub
===== END CODE =====

-- b.r.t.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Accepted Solution

by:
zvonko earned 200 total points
Comment Utility
job done (with fun ;-))

Sub Initialize
  Dim session As New NotesSession
  Dim db As NotesDatabase
  Dim collection As NotesDocumentCollection
  Dim doc As NotesDocument
  Dim docarray() As NotesDocument
  Dim word As Variant
  Dim extname As String
  Dim i As Integer
 
  Set db = session.CurrentDatabase
  Set collection = db.UnprocessedDocuments
  Set word = CreateObject("Word.application")
  word.documents.open(|"C:\Templates\RD & Design\TD Merge Blank.doc"|)
  word.Application.Visible = True
  word.Application.Selection.TypeText("Document merged from Notes Database ")
  word.Application.Selection.InsertDateTime
  word.Application.Selection.TypeParagraph
  word.Application.Selection.TypeParagraph
  word.Application.Selection.InsertBreak 2
 
  Redim docarray(collection.Count-1) As NotesDocument
  For i = 0 To collection.Count - 1
    Set docarray(i) = collection.GetNthDocument(i+1)
  Next
  'Call CollectionToSortedArray(collection, docarray())
 
  For i = 0 To (collection.Count - 1)
    Set doc = docarray( i )
    Forall item In doc.Items
      If (item.type=RICHTEXT)Then
        If Isarray(item.EmbeddedObjects) Then
          Forall o In item.EmbeddedObjects
            If (o.type=EMBED_ATTACHMENT) Then
              extname=Ucase$(Right$(o.Name,3))
              If extname="DOC" Then
                Print "Adding: " & o.Name
                Call o.extractfile ("c:\" & o.Name)
                word.Application.Selection.InsertFile("c:\" & o.Name)
                Kill "c:\" & o.Name
                word.Application.Selection.InsertBreak 2
                ' break 7 is page break
                ' break 2 is section break, next page
              End If
            End If
          End Forall
        End If
      End If
    End Forall
  Next
  word.Selection.GoTo 0, 1, 1, ""   '//** decription bellow
  '//** Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=1, Name:=""
  word.Selection.InsertBreak 7
  '//** Selection.InsertBreak Type:=wdPageBreak
  word.Selection.GoTo 0, 1, 1, ""
  word.ActiveDocument.TablesOfContents.Add word.Selection.Range, True, 1, 2, False, 1, True, True
  word.ActiveDocument.SaveAs( |"C:\Templates\RD & Design\TD Merge With TOC.doc"| )
  word.Quit
  Set word = Nothing
End Sub


This should work on your side (after replacing drive letter C: with X:) and uncommenting CollectionToSortedArray() function.
For the NotesViewNavigator speedup I had to ask some question to you (Tomorrow, now it is too late for me) .
But test this first...

So long,
zvonko

0
 
LVL 7

Author Comment

by:BarryTice
Comment Utility
zvonko --

Excellent!

Now if I could just figure out why you keep closing my Word documents, I'd be all set :-)

I've moved a few things around to streamline:
    I put moved the Insert Section Break to just before the Merge File instead of just after
    I moved my typed header info to the end, just before the inserted table of contents.

And I removed where you save and close the Word file. (For our purposes, users will merge the documents together, then probably print that document and discard it. The merged document will not generally be saved.)

This works great for merging in the order that the sorts (above) put the records into. But didn't you say you were working on coming up with a way to merge them in the order they show in the view? So much the better, I think.

Thanks!
0
 
LVL 10

Expert Comment

by:zvonko
Comment Utility
OK,

so far, so good.

You have two options:
1.) either uncomment the sorting call in my proposed code
2.) or create a view where this sorting is allready done

Perhaps the sorted view is allready the one where the user select their Word documents.
Anycase the first column has to be sorted (I think with this field: CLARIFY_NUMBER).
So give me the view name, and I will change the actual sorting code to use the view index.

So long,
zvonko
0
 
LVL 7

Author Comment

by:BarryTice
Comment Utility
zvonko --

I've already uncommented the sort call, and if that's the best I'll ever get, I'm happy with it.

In a perfect world, though, the attached documents would be merged in the order they appear in the view from which they're merged, whichever view that is. There are dozens of views in this database, allowing different categories of users to find records in the manner most useful to them.

Is that too difficult? Would it require a different script for each view, referencing that view's index? That would be a whole lot more maintenance than I care for.

Thanks -- b.r.t.
0
 
LVL 10

Expert Comment

by:zvonko
Comment Utility
Yes, sorry, but it is the best choice then.

Because you need a view object to get its NotesViewNavigator object.
The problem is: db.UnprocessedDocuments gets documents in order created and does not supply the view name.

So stay at BubbleSort side; sorry.

At least throw away my lines before sorting; I mean from Redim to sorting:
Redim docarray(collection.Count-1) As NotesDocument
For i = 0 To collection.Count - 1
 Set docarray(i) = collection.GetNthDocument(i+1)
Next
       'Call CollectionToSortedArray(collection, docarray())

So Redim and the For loop are not needed when using the Call to sort function.

So next time perhaps I can serve you with my NotesViewNavigator's idea ;-)

Regards,
zvonko
0
 
LVL 10

Expert Comment

by:zvonko
Comment Utility
...but stop!

When you allways do the sort by this "CLARIFY_NUMBER" why can you not create a view (perhaps hidden) where this field is sorted?

...and give me the name of this view to process the selected document from either view in this hidden view order <|;-)
0
 
LVL 7

Author Comment

by:BarryTice
Comment Utility
zvonko --

I don't always sort by CLARIFY_NUMBER for all my views. It's just a reasonable default to sort by if I can't match the sort order of the view.

If the documents would always be merged in the order in which they appear in one particular view (perhaps hidden), then what is the advantage of ordering them through that view rather than through the sorted array?

Thanks! -- b.r.t.
0
 
LVL 10

Expert Comment

by:zvonko
Comment Utility
Ha HA! ...does this mean you have no universal sorting function and have to adapt every time this bubles to actual sorting fields?
Wath about transforming the agent to a shared view action? Than you get NotesUIWorkspace, NotesUIView, db.UnprocessedDocuments and all the other stuff you allready used in the agent...

Is this a way for you?

 
0
 
LVL 7

Author Comment

by:BarryTice
Comment Utility
zvonko --

Sorry for the long absence. I took a week off from work and it's taken me several weeks to get caught up again.

No, this doesn't mean I adapt my bubble sort from different views. I have instead gone with a happy medium.

What I had hoped for was that the merge would occur in the order the documents are shown in the view, regardless of the view. What I have settled for is the documents always appearing in the order produced by the above sorts, regardless of which view is used to generate the merge file.

Thanks for the help on this. I'm content with the way things are working now, and nobody else is complaining, either. So now it's on to new problems and challenges...

-- b.r.t.
0
 
LVL 10

Expert Comment

by:zvonko
Comment Utility
if you are happy with this I am too <|;-)

so long,
zvonko
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This is an old article, please see an updated version of this article, located here: http://www.experts-exchange.com/articles/23619/Notes-8-5x-Windows-7-Notes-info-and-tips.html
Article by: Rob
Notes 8.5 Archiving Steps and Tips This article covers setting up a Notes archive, and helps understand some of the menu choices making setting up and maintaining a Notes archive file easier.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now