I'm trying to read excel file which is uploaded on itemadded event. I need to update meta data column of document library by reading uploaded excel sheet. But I get following error,
Here is my code,
SPItem item = properties.ListItem;
string workbookpath = "http://server/sites/dev/Published%20documents/Test.xlsx";
ApplicationClass excel = new ApplicationClass();
excel.Visible = false;
Workbook excelWorkBook = excel.Workbooks.Open(workbookpath, 0, true, 5, "", "", false, XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Sheets sheets = excelWorkBook.Worksheets;
Worksheet worksheet = (Worksheet)sheets.get_Item(1);
Range range = worksheet.get_Range("A1", "A1");
item["Status"] = (string)range.Cells.Value2;
item.Update();
base.ItemAdded(properties);
For testing purpose I've hard coded the excel file path.
Take a look at this blog post. I think it will help
http://hristopavlov.wordpress.com/2008/05/14/uploading-a-file-event-receivers-the-file-has-been-modified-by/
I think the problem that you document is in checked out state because you have open it from excel app, so you need to do following:
close excel app
Check in document back to save chages or undo check out (use item.CheckIn() or item.UndoCheckOut())
after this you should be able to update item properties
Related
I have a procedure that creates a PDF file according to an ms word template and its data is retrieved from a database.
It works fine, creates a PDF file perfectly , no run time errors. The problem is that whenever I shut off the computer, ms word prevents the shutdown and if I press cancel ms word shows a message;
The code goes like this;
Dim wordApp As Word.Application
Dim templateBookmarks As Word.Bookmarks
Dim templateName As String
Dim template As Word.Document
'Some other variables for computations
wordApp = CreateObject("Word.Application")
sourceTable = New DataTable
'Some other procs to fill the data table
templateName = "Foo Template.docx"
template = wordApp.Documents.Add(templatePath & templateName)
templateBookmarks = template.Bookmarks
templateBookmarks.Item("sample bookmark").Range.Text = "foo"
'Then fills the table in the template like...
template.Tables(1).Cell(1, 1).Range.Text = dataSource.Rows(0).Item(0)
'Then saves the document as a pdf
Dim saveName As String = "sample file"
template.SaveAs2(savePath & saveName, Word.WdSaveFormat.wdFormatPDF)
I have tried to force garbage collection for the word COM resources, as well as changing the template from an actual document i.e. docx to a word template .dotx. I also tried the method Quit() but it only shows the ms word message much earlier. This is the first time I needed to use interop so pardon if I don't have much idea about it.
The files I needed are saved, the only problem is the ms word message and unsaved and unnecessary files e.g. Document1,Document2,Document3 that seems to be created aside from the required PDF
Use the Document.Close method which closes the specified document after saving files using the PDF file format. It allows specifying the SaveChanges parameter which specifies the save action for the document. Can be one of the following WdSaveOptions constants: wdDoNotSaveChanges, wdPromptToSaveChanges, or wdSaveChanges.
On Error GoTo errorHandler
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
errorHandler:
If Err = 4198 Then MsgBox "Document was not closed"
I want to copy popup window Text Message and paste that message into Excel file and then compare that data with Actual data. I am able to read the popup window text message and display on consol window, But how to paste that message into excel file
Selenium doesn't provide any functionality to deal with Excel documents, however assuming you're using Java you can read and write Excel files using Apache POI library
Workbook wb = new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(createHelper.createRichTextString("Hello world"));
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
Hope it will help you.
I have been looking online to get the idea how to acess a range and then "clearing" it. Sadly even though i have found several solutions, vba in libre office makes things really complicated to understand. I have tried to record a macro, and edit it... It works but not as i want it to. I need to execute the below code from a Form (Dialog) and it does what its meant to, but shows everything in the background and I can't have that .... is there a way, to do this ? for example in vba Exel I would just write :
Range("A1:DA25000").ClearContents (or .Delete)
Here is what I got with macro recordings:
Function Brisanje()
dim document as object
dim dispatcher as object
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Nr"
args1(0).Value = 2
dispatcher.executeDispatch(document, ".uno:JumpToTable", "", 0, args1())
dim args2(0) as new com.sun.star.beans.PropertyValue
args2(0).Name = "ToPoint"
args2(0).Value = "$A$1:$DA$25000"
dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args2())
dispatcher.executeDispatch(document, ".uno:ClearContents", "", 0, Array())
end Function
The code you are looking for is in Andrew Pitonyak's macro document, section 6.4. Clear a cell. Notice that the code works for multiple cells as well:
REM You can use a range such as "A1:B2"
Macro recordings are not a good way to learn how to write macros. They generate dispatcher code, which is different from working with the UNO API.
Below is a snippet of a function I am using to successfully export a GridView to Excel and this code merges a range of cells, and formats the text. Now I would also like to make this header row appear on every print page should a user elect to print the worksheet. I'm thinking that this must be a configurable item, so what would be the syntax for this please?
Dim xlDoc As New XLWorkbook()
xlDoc.AddWorksheet("Export")
Dim xlWS As IXLWorksheet = xlDoc.Worksheets(0)
xlWS.Row(1).Cell(1).Value = "some text"
xlWS.Row(1).Cell(1).Style.Fill.BackgroundColor = XLColor.FromArgb(255, 255, 102)
xlWS.Row(1).Cell(1).Style.Font.Bold = True
xlWS.Range("A1", "G1").Merge()
xlWS.Range("A1", "G1").Style.Border.OutsideBorder = XLBorderStyleValues.Thin
xlWS.Range("A2", "G2").SetAutoFilter()
I have a VS2010 Vb.net program that creates a Word 2007 file.
My Normal.dot file is customised to give me a new Tab with Buttons in that do specific things via VBA in the Normal.dot program when those Buttons are pressed.
This all works fine, however, I now want to add some functionality whereas as soon as the new Word document is created, it edits a Task in Outlook.
I have edited the 2 "This Document" Procedures and you can see my Normal.Dot file in the attached Screenshot.
When I run my VB.Net program to create a brand new Word 2007 document, the program does NOT stop on either of the message boxes, it just continues and opens the Word document as before, my code is below, what am I doing wrong ?!?
'Open or Create Word document for Editing
myNewsLetter = myFolder + myLeague + "News" + mySession + ".doc"
If File.Exists(myNewsLetter) Then
'do nothing
Else
myTemplate = myTempFolder + "NL Skeleton.doc"
File.Copy(myTemplate, myNewsLetter)
Create_Blank_Newsletter()
End If
'Open Word Newsletter, or switch to it if it's already open
Dim myFileOpen As Boolean
myFileOpen = IsFileOpen(myNewsLetter)
If myFileOpen = False Then
MSDoc = MSWord.Documents.Open(myNewsLetter)
End If
MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
MSWord.Visible = True
MSWord.ActiveDocument.Bookmarks("\StartOfDoc").Select()
OK, sorted this, the full discussion can be found here ... http://www.vbaexpress.com/forum/showthread.php?p=286771#post286771
Basically, I'm not creating a NEW document, I am creating a new document via a Copy and then opening that existing document !!!