2012-04-20 87 views
0

我有一個窗體,在窗體內我有一個有excel文件的對象(工作表)。 然後我有一個按鈕可以計算工作表中字段的總和,並在表單的字段中顯示它的總和。我如何編碼?我們的想法是這樣的使用lotusscript計算Excel中單元格的總和

totalr = SUM(A2:A51)

doc.total = totalr

回答

2

如果你會從Notes客戶機和機器使用的形式做這個運行Notes客戶端有Excel,那麼你可以使用VBA。只需將文件提取到用戶的硬盤驅動器,使用Excel打開它,在列底部添加一個單元格,並在其中放入一個SUM()函數,獲取該新單元格的值,更新UIDOC中的字段,然後擦除臨時文件文件。

下面的代碼假定您有一個名爲「excelfile」的RichText字段(包含Excel文件)的窗體,名爲「Sum」的字段將包含Excel文件中的總和值,以及包含碼。此外,該文件必須與附件一起保存才能運行。

希望它有幫助。

Sub Click(Source As Button) 

On Error Goto errorhandler 

Dim s As New NotesSession 
Dim ws As New NotesUIWorkspace 
Dim uidoc As NotesUIDocument 
Set uidoc = ws.CurrentDocument 
Dim doc As NotesDocument 
Set doc = uidoc.Document 
Dim rtitem As NotesRichTextItem 
Dim isfileopen As Boolean 

Dim filename As String, rows As Double, cols As Double, colToSum As String, c As Integer 
Dim xlApp As Variant, xlWorkbook As Variant, xlSheet As Variant, pathname As String 

isfileopen = False 

'convert column letter to number 
colToSum = "A" 
c = Asc(colToSum) - 64 

'extract file 
pathname = s.GetEnvironmentString("Directory", True) & "\" 
Set rtitem = doc.GetFirstItem("excelfile") 
If Not (doc.HasEmbedded) Then 
    Msgbox "There are no file attachments on this document" 
    Goto ExitGracefully 
End If 
Forall o In rtitem.EmbeddedObjects 
    If (o.Type = EMBED_ATTACHMENT) Then 
     filename = pathname & o.Name    
     Call o.ExtractFile(filename)   
    End If 
End Forall 

'open file 
Print "Opening file " & filename & "..." 
Set xlApp = CreateObject("Excel.Application") 
xlApp.Workbooks.Open filename 
isfileopen = True 

'get sheet 
Print "Gathering data ..." 
Set xlWorkBook = xlApp.ActiveWorkbook 
Set xlSheet = xlWorkBook.ActiveSheet 
xlSheet.Cells.SpecialCells(11).Activate 
rows = xlApp.ActiveWindow.ActiveCell.Row 
cols = xlApp.ActiveWindow.ActiveCell.Column 

'add a row and put the sum() formula there 
Print "Computing sum with Excel..." 
xlSheet.Cells(rows+1, c).value = "=SUM(" & colToSum & "1:" & colToSum & Cstr(rows) & ")" 

'update UI doc 
Call uidoc.FieldSetText("Sum", Cstr(xlSheet.Cells(rows+1, c).value)) 

exitgracefully: 

'close excel 
Print "Closing Excel..." 
If Not xlWorkbook Is Nothing Then 
    xlWorkbook.Close False 
End If 
If Not xlApp Is Nothing Then 
    xlApp.DisplayAlerts = False 
    xlApp.Quit 
    Set xlApp = Nothing 
End If 

If isfileopen Then 
    'remove temp file 
    Print "Removing " & filename 
    Kill filename 
End If 

'done 
Print "Done" 

Exit Sub 
errorhandler: 
Msgbox Error & " line " & Erl 
Exit Sub 
End Sub 
相關問題