gbentley
asked on
Exporting
This should be real easy or impossible!
I need to give the users of the db I'm working on the ability to locate several documents, then export them to Word.
I know I can do an export whilst in a document and export to Word. I know I can use the @command to do the job. What I don't know is how to do this from LotusScript, so I can export the selected documents in a view rather than the view.
Doable or impossible?
I need to give the users of the db I'm working on the ability to locate several documents, then export them to Word.
I know I can do an export whilst in a document and export to Word. I know I can use the @command to do the job. What I don't know is how to do this from LotusScript, so I can export the selected documents in a view rather than the view.
Doable or impossible?
ASKER
zvonko - Thanks for the quick response. I was really after getting the whole document as formatted (exactly what you get if you open the document and choose File/Export) as there are a number of richtext fields.
I've found some code on the Sandbox, so I'll see how that goes for the moment.
I've found some code on the Sandbox, so I'll see how that goes for the moment.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The sample I found also uses COM/OLE and the method is somewhat similar to yours, so don't get too excited!
I'll get back tomorrow with some news.
I'll get back tomorrow with some news.
ASKER
zvonko - Her is the code I came up with on the weekend. It does a mail merge into Word using the fields on the documents in the view it is run from. The Export Template documents just have a name and a template.
I make no claims to this being at all sturdy or perfect, but it is doing what I wanted and as the final format of the export is controlled by the merge document, it satisfies my users.
Sub Initialize
Dim workspace As New NotesUIWorkspace
Dim session As New NotesSession
Dim db As NotesDatabase
Dim sourceView As NotesUIView
Dim sourceDoc As NotesDocument
Dim selectedDocs As NotesDocumentCollection
Dim templateView As NotesView
Dim templateDoc As NotesDocument
Dim templateNames() As String
Dim selectedTemplateNameV As Variant
Dim wordDocs As Variant
Dim wordDoc As Variant
Dim wordObj As Variant
Dim range As Variant
Dim tableObj As Variant
Dim object As Variant
Dim rtitem As Variant
Dim i As Integer
Dim rowNum As Integer
Dim tempString As String
Set db =workspace.CurrentDatabase .Database
Set templateView = db.GetView("Templates")
If templateView.AllEntries.Co unt < 1 Then ' No Templates
Msgbox "You need to create some Templates before you can use the Export facility!"
Exit Sub
Elseif templateView.AllEntries.Co unt = 1 Then ' One Template, no need to prompt
Set templateDoc = templateView.GetNthDocumen t(1)
Else ' Get list of template names from Templates view
Redim templateNames(1 To templateView.AllEntries.Co unt)
For i = 1 To templateView.AllEntries.Co unt
templateNames(i) = templateView.GetNthDocumen t(i).GetFi rstItem("E xportTempl ateName"). Text
Next
' Get users selection
selectedTemplateNameV = workspace.Prompt( PROMPT_OKCANCELLIST, "Templates", _
"Please select a template to use for your export.", _
templateNames(1), _
templateNames)
If selectedTemplateNameV = "" Then ' Exit on Cancel or no selection
Exit Sub
End If
' Select chosen Template to use for export
Set templateDoc = templateView.GetDocumentBy Key( selectedTemplateNameV, True)
End If
'Get documents selected in View
Set selectedDocs = db.UnprocessedDocuments
Set sourceDoc = selectedDocs.GetFirstDocum ent
If sourceDoc Is Nothing Then
Exit Sub
End If
' Create merge source doc and table
Set wordObj = CreateObject("Word.Applica tion.8")
wordObj.visible = True
Set wordDocs = wordObj.Documents
wordDocs.Add
Set wordDoc = wordObj.ActiveDocument
Set range = wordDoc.Range(0,0)
Set tableObj = wordObj.selection.Tables.A dd(range,s electedDoc s.Count+1, Ubound(sou rceDoc.Ite ms))
' Add column headings from field names
For i = Lbound(sourceDoc.Items) To Ubound(sourceDoc.Items)
tableObj.cell(1,i).select
wordObj.selection.typetext sourceDoc.Items(i).Name
Next
rowNum = 2
'Process each document
While Not(sourceDoc Is Nothing)
' Export documents fields to Word table
For i = Lbound(sourceDoc.Items) To Ubound(sourceDoc.Items)
Call TableObj.cell(rowNum,i).se lect
tempString = sourceDoc.Items(i).Text
wordObj.selection.typetext tempString
Next
rowNum = rowNum + 1
Set sourceDoc = selectedDocs.GetNextDocume nt(sourceD oc)
Wend
' Save data file
wordDoc.saveas "C:\temp\data.doc"
wordDoc.close
' Get template from doc
Set rtitem = templateDoc.GetFirstItem(" ExportTemp lateTempla te")
' Trap error on no attachment
On Error Resume Next
Forall o In rtitem.EmbeddedObjects
If ( o.Type = 1453 ) Then
Set object = o
End If
End Forall
On Error Goto 0
' Exit if no attachment
If object Is Nothing Then
Msgbox "That template has no attached file. Please attach a merge document and retry."
wordObj.quit
Exit Sub
End If
Set wordDoc = object.Activate(True)
' Perform merge to document
wordDoc.MailMerge.MainDocu mentType = wdFormLetters
wordDoc.MailMerge.OpenData Source "C:\temp\Data.doc"
wordDoc.MailMerge.Suppress BlankLines = True
wordDoc.MailMerge.Execute True
Msgbox "Export Complete"
End Sub
Regards
Gordon
I make no claims to this being at all sturdy or perfect, but it is doing what I wanted and as the final format of the export is controlled by the merge document, it satisfies my users.
Sub Initialize
Dim workspace As New NotesUIWorkspace
Dim session As New NotesSession
Dim db As NotesDatabase
Dim sourceView As NotesUIView
Dim sourceDoc As NotesDocument
Dim selectedDocs As NotesDocumentCollection
Dim templateView As NotesView
Dim templateDoc As NotesDocument
Dim templateNames() As String
Dim selectedTemplateNameV As Variant
Dim wordDocs As Variant
Dim wordDoc As Variant
Dim wordObj As Variant
Dim range As Variant
Dim tableObj As Variant
Dim object As Variant
Dim rtitem As Variant
Dim i As Integer
Dim rowNum As Integer
Dim tempString As String
Set db =workspace.CurrentDatabase
Set templateView = db.GetView("Templates")
If templateView.AllEntries.Co
Msgbox "You need to create some Templates before you can use the Export facility!"
Exit Sub
Elseif templateView.AllEntries.Co
Set templateDoc = templateView.GetNthDocumen
Else ' Get list of template names from Templates view
Redim templateNames(1 To templateView.AllEntries.Co
For i = 1 To templateView.AllEntries.Co
templateNames(i) = templateView.GetNthDocumen
Next
' Get users selection
selectedTemplateNameV = workspace.Prompt( PROMPT_OKCANCELLIST, "Templates", _
"Please select a template to use for your export.", _
templateNames(1), _
templateNames)
If selectedTemplateNameV = "" Then ' Exit on Cancel or no selection
Exit Sub
End If
' Select chosen Template to use for export
Set templateDoc = templateView.GetDocumentBy
End If
'Get documents selected in View
Set selectedDocs = db.UnprocessedDocuments
Set sourceDoc = selectedDocs.GetFirstDocum
If sourceDoc Is Nothing Then
Exit Sub
End If
' Create merge source doc and table
Set wordObj = CreateObject("Word.Applica
wordObj.visible = True
Set wordDocs = wordObj.Documents
wordDocs.Add
Set wordDoc = wordObj.ActiveDocument
Set range = wordDoc.Range(0,0)
Set tableObj = wordObj.selection.Tables.A
' Add column headings from field names
For i = Lbound(sourceDoc.Items) To Ubound(sourceDoc.Items)
tableObj.cell(1,i).select
wordObj.selection.typetext
Next
rowNum = 2
'Process each document
While Not(sourceDoc Is Nothing)
' Export documents fields to Word table
For i = Lbound(sourceDoc.Items) To Ubound(sourceDoc.Items)
Call TableObj.cell(rowNum,i).se
tempString = sourceDoc.Items(i).Text
wordObj.selection.typetext
Next
rowNum = rowNum + 1
Set sourceDoc = selectedDocs.GetNextDocume
Wend
' Save data file
wordDoc.saveas "C:\temp\data.doc"
wordDoc.close
' Get template from doc
Set rtitem = templateDoc.GetFirstItem("
' Trap error on no attachment
On Error Resume Next
Forall o In rtitem.EmbeddedObjects
If ( o.Type = 1453 ) Then
Set object = o
End If
End Forall
On Error Goto 0
' Exit if no attachment
If object Is Nothing Then
Msgbox "That template has no attached file. Please attach a merge document and retry."
wordObj.quit
Exit Sub
End If
Set wordDoc = object.Activate(True)
' Perform merge to document
wordDoc.MailMerge.MainDocu
wordDoc.MailMerge.OpenData
wordDoc.MailMerge.Suppress
wordDoc.MailMerge.Execute True
Msgbox "Export Complete"
End Sub
Regards
Gordon
ASKER
zvonko - I gave you the points to close the question and because you gave me the link that got me started.
The code I posted (plus a few mods) has worked well for me. Feel free to use/abuse as you want.
The code I posted (plus a few mods) has worked well for me. Feel free to use/abuse as you want.
Doable :-)
Here a lately answered Q:
https://www.experts-exchange.com/questions/20443119/Ordering-of-unprocesseddocs-in-view-agent.html
Tell me your Document fields and I can provide a short reduced version for your design basis.
So long,
Zvonko