Excel 2010 - 'The image part with relationship ID rId1 was not found in the file.' When copying sheets of a workbook into another workbook - excel

I have a source excel workbook (XLSM format) that has sheets containing images. These sheets are programatically copied into another(target) workbook (XLSM) using VB.NET late binding. After all the copy is done i save the workbook and launch it. In the opened excel workbook I see the error message 'The image part with relationship ID rId1 was not found in the file' at all places where the images are placed.
All the operations are done in the client machine no server side code available.
Interestingly this issue doesn't occur in Excel 2013 and it displays the images properly issue is observed only in 2010 and 2007. Is this a know bug in Excel 2010 and 2007 if yes can any one provide me the official link to the ticket so that i can track the issue and get the Hot fix once it is available.
Dim SourceExcelWorkbook As Excel.Workbook = Nothing
Dim TargetExcelWorkbook As Excel.Workbook = Nothing
Dim TargetExcelSheets As Excel.Sheets = Nothing
Dim SourceExcelSheets As Excel.Sheets = Nothing
Dim CopyWorkSheet As Excel.Worksheet = Nothing
Dim XLApp As New Excel.Application
XLApp.Visible = False
XLApp.DisplayAlerts = False
XLApp.ScreenUpdating = False
Dim pobjExcelWorkbooks As Excel.Workbooks = XLApp.Workbooks
SourceExcelWorkbook = pobjExcelWorkbooks.Open("source file path")
TargetExcelWorkbook = pobjExcelWorkbooks.Open("target file path")
TargetExcelSheets = TargetExcelWorkbook.Worksheets
SourceExcelSheets = SourceExcelWorkbook.Worksheets
Dim OriginalSheetCount As Integer = TargetExcelSheets.Count
Dim SheetCount As Integer = OriginalSheetCount
Dim SheetsToBeCopiedCount As Integer = SourceExcelSheets.Count
While SheetsToBeCopiedCount > 0
Dim lobjAfterSheet As Object = TargetExcelSheets.Item(SheetCount)
CopyWorkSheet = SourceExcelSheets.Item(1)
CopyWorkSheet.Move(After:=lobjAfterSheet)
SheetCount = SheetCount + 1
TargetExcelWorkbook.Save()
SheetsToBeCopiedCount = SheetsToBeCopiedCount - 1
End While
TargetExcelWorkbook.Save()

Related

Accessing One Drive files offline in VB .Net

I am working on an application that stores it's files in the Documents folder to easily sync with One Drive. I need to make them available offline, in case of an outage. I have changed the settings so that I have a local copy. When I click directly on the Excel file in File Explorer, the program opens it with no problem. However, when I try to open the file in my application, I get an error stating "No network found..." etc. Is there another way to do this? Thanks in advance.
Public Sub LoadMemberList()
lstMemberList.Clear()
Dim mMember As Members
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim range As Excel.Range
xlApp = New Excel.Application
xlBook = xlApp.Workbooks.Open(CurrentYearPath) '***This is where the exception is thrown.
xlSheet = xlBook.Worksheets("sheet1")
range = xlSheet.UsedRange
Dim rs As Object(,) = CType(range.Value, Object(,))
Dim records As Long = rs.GetUpperBound(0)
If records > 1 Then
For x = 2 To records
mMember.MemberNumber = FormatNumber(rs(x, 1))
mMember.MemberName = rs(x, 4) + " " + rs(x, 3)
lstMemberList.Add(mMember)
Next
End If
xlBook.Close()
xlApp.Quit()
KillExcel()
xlApp = Nothing
xlBook = Nothing
xlSheet = Nothing
range = Nothing
End Sub
Public Function CurrentYearPath() As String
Dim CurrentYear As Integer
CurrentYear = CInt(Format(Now, "yyyy"))
Return My.Computer.FileSystem.SpecialDirectories.MyDocuments + "\DailyBook\Rosters\Membership" + CurrentYear.ToString + ".xlsx"
End Function
Scroll down here
https://learn.microsoft.com/en-us/onedrive/plan-onedrive-enterprise#key-onedrive-features
and you'll find this,
However, if you plan to access a file while disconnected from the internet, you can make the file available offline by right-clicking it, and then selecting Always keep on this device.

vb.net Office365 sharepoint check file open by user

I have the following script to access an excel file from office365 Sharepoint.
dim objApp2 As Excel.Application
dim objBook2 As Excel._Workbook
dim objBooks2 As Excel.Workbooks
dim objSheets2 As Excel.Sheets
dim objSheet2 As Excel._Worksheet
link="sharepointlink"
objApp2 = New Excel.Application()
objBooks2 = objApp2.Workbooks
objBook2 = objBooks2.Open(link, [ReadOnly]:=False)
objSheets2 = objBook2.Worksheets
ws2 = objSheets2("Data")
Dim valoare As Integer
valoare = Int(ws2.Range("a1").Value)
ws2.Range("a1").Value = valoare + 1
objBook2.SaveAs()
objBook2.Close(False)
I have a problem: If someone accesses my link and opens the file, either on Sharepoint or opened locally, I get an error when trying to close the object or save the file.
How can I check if the file is opened by someone when I try to save, or is there another way to access the file?
Welcome to the site. The ReadOnly property should tell you if another person has it open, especially when you said you want it NOT to be. You may ultimately need to switch to a shared workbook, but this should do the trick.
dim objApp2 As Excel.Application
dim objBook2 As Excel._Workbook
dim objBooks2 As Excel.Workbooks
dim objSheets2 As Excel.Sheets
dim objSheet2 As Excel._Worksheet
link="sharepointlink"
If IsFileInUse(link) then
MsgBox "Cannot update Excel file"
Exit Sub 'End
End If
objApp2 = New Excel.Application()
objBooks2 = objApp2.Workbooks
objBook2 = objBooks2.Open(link, [ReadOnly]:=False)
objSheets2 = objBook2.Worksheets
ws2 = objSheets2("Data")
Dim valoare As Integer
valoare = Int(ws2.Range("a1").Value)
ws2.Range("a1").Value = valoare + 1
objBook2.SaveAs()
objBook2.Close(False)
Public Function IsFileInUse(sFile As String) As Boolean
Try
Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
End Using
Catch Ex As Exception
Return True
End Try
Return False
End Function

Disable excel save changes prompt

I have tried all the ways I normally do this and what I can find searching. oxl.DisplayAlerts = False is not working. I still get asked if I want to save changes.
I am essentially trying to use the excel sheet as a template. The full script exports to pdf, but this is enough to re-create the problem. BTW I tried saving it as a xltx file (template) and still get the save promt.
Dim oxl As New Excel.Application
Dim apppath2 As String = My.Application.Info.DirectoryPath.ToString
Dim mywb As Excel.Workbook = oxl.Workbooks.Open(Filename:=apppath2 & "\fuse template.xlsx", [ReadOnly]:=True)
oxl.Visible = False
Dim mysheet As Excel.Worksheet = mywb.Sheets(1)
mysheet.Cells(10, 5) = l_region.Text
mysheet.Cells(11, 5) = comb_emc_name.Text
oxl.DisplayAlerts = False
mywb.Close(False)
mysheet = Nothing
mywb = Nothing
oxl = Nothing
GC.Collect()
I was missing mywb.Saved = True. I have never had to do that before.

How to use OpenOffice Spreadsheet to get an image from an excel file

I have a code that exports image from excel into a picturebox and here it is.
Dim appExcel As Object
Set appExcel = CreateObject("Excel.Application")
appExcel.Visible = False
Dim xlsBook As New excel.Workbook
Dim xlsSheet As New excel.Worksheet
Dim rowlocation As Integer
Dim columnlocation As Integer
Dim celladdress As String
Set xlsBook = appExcel.Workbooks.Open(Text1.Text)
Set xlsSheet = xlsBook.Worksheets("Sheet1")
Dim x As excel.Shapes
For Each x In xlsSheet.Shapes
x.Copy
Picture1.Picture = Clipboard.GetData(vbCFBitmap)
Text2.Text = x.Name
rowlocation = x.TopLeftCell.Row
columnlocation = x.TopLeftCell.Column
celladdress = xlsSheet.Cells(x.BottomRightCell.Row + 1, x.TopLeftCell.Column).Address(RowAbsolute:=False, ColumnAbsolute:=False)
MsgBox ActiveSheet.Range(celladdress)
Next
End If
and unfortunately this code wont work on my friends PC becuase he does not have an Excel installed but he has OpenOffice spreadsheet. I tried to open the Excel in Openoffice then the file opens now my goal is how can i convert the code above in OpenOffice? I mean run the code for OpenOffice files.
This is my code but its not working
Dim objServiceManager As Object
Dim objDesktop As Object
Dim objDocument As Object
Dim objText As Object
Dim objCursor As Object
Dim oDoc As Object
Dim ARG()
Dim oGraph As Object
Dim oView As Object
Dim oDrawPage As Object
Dim oSheet As Object
Dim Image As System_Drawing.Image
Dim oimage As Object
Dim osize As Object
Set objServiceManager = CreateObject("com.sun.star.ServiceManager")
Set objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop")
Set oDoc = objDesktop.loadComponentFromURL("file:///C:\Users\paul\Desktop\Testing.ods", "_blank", 0, ARG())
Set oSheet = oDoc.getSheets().getByIndex(0)
Set oGraph = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
Set oView = oDoc.CurrentController
Set oDrawPage = oView.getActiveSheet.DrawPage
For i = 0 To 2
For j = 0 To 9
' Form1.Image1.Picture = Clipboard.GetData
Form1.Image1.Picture = LoadPicture(oDrawPage)
Next
Next
TYSM for future help
This is the latest code in VB6 and it has an error saying vnd.sun.star is missing
Dim objServiceManager As Object
Dim objDesktop As Object
Dim objDocument As Object
Dim objText As Object
Dim objCursor As Object
Dim oDoc As Object
Dim ARG()
Dim oGraph As Object
Dim oView As Object
Dim oDrawPage As Object
Dim oSheet As Object
Dim Image As System_Drawing.Image
Dim oimage As Object
Dim osize As Object
Dim Cell As Object
Dim sGraphicUrl As String
Dim oDisp
Dim oFrame
Dim opos As Object
Set objServiceManager = CreateObject("com.sun.star.ServiceManager")
Set objDesktop = objServiceManager.createInstance("com.sun.star.frame.Desktop")
Set osize = objServiceManager.Bridge_GetStruct("com.sun.star.awt.Size")
Set opos = objServiceManager.Bridge_GetStruct("com.sun.star.awt.Point")
Set oDoc = objDesktop.loadComponentFromURL("file:///C:\Users\paul\Desktop\ACE Express - Fairview_Sample PC of Gondola.ods", "_blank", 0, ARG())
Set oSheet = oDoc.getSheets().getByIndex(0)
Set oimage = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
Set oView = oDoc.CurrentController
Set oDrawPage = oView.getActiveSheet.DrawPage
Set oimage = oDrawPage.getByIndex(0)
Image1.Picture = LoadPicture(oimage.GraphicURL)
Here is the output of the unzip picture
I do not know exactly what your code does, because I normally do not use Microsoft Office. However it looks like this task can be accomplished using OpenOffice Basic. One of the best places to learn OpenOffice Basic is Andrew Pitonyak's Macro Document.
To start with, look at section 5.9.5. Convert all linked images.
EDIT:
To do this in Calc, first I went to Tools -> Macros -> Organize Dialogs and created a dialog named "ImageViewerForm" with an image control named "MyImageControl".
Then I went to Tools -> Macros -> Organize Macros -> OpenOffice Basic and added the following code:
Sub ShowImageViewerDialog
oDoc = ThisComponent
oDlg = CreateUnoDialog(DialogLibraries.Standard.ImageViewerForm)
oControl = oDlg.Model.MyImageControl
oDrawPage = oDoc.getDrawPages().getByIndex(0)
oImage = oDrawPage.getByIndex(0)
oControl.ImageURL = oImage.GraphicURL
oDlg.execute()
End Sub
To run the code, go to Tools -> Macros -> Run Macro. Here is the result:
The "Next Image" button should be fairly straightforward to implement by adding an event handler.
For documentation, see GraphicObjectShape and UnoControlButtonModel. But mostly I just used the MRI tool to figure it out.
EDIT 2:
Regarding the error you posted, the GraphicURL property in this case is a string that references an in-memory object, not a filepath. An example of the string is shown here: https://www.openoffice.org/api/docs/common/ref/com/sun/star/graphic/XGraphicObject.html.
So it is not suitable for passing to LoadPicture, which takes a filename.
Perhaps you can get the actual image data from oImage.Bitmap or oImage.Graphic. Use MRI to see what attributes are available.
For example, it looks like there is a getDIB() method that might work like this:
Form1.Image1.Picture = oImage.Bitmap.getDIB()
One more idea: Instead of using an Office application, you could write code that unzips the file and reads each image in the Pictures subdirectory.
I have never tried it but according to the docs you can control Open Office through COM automation.
Your VB6 code above is controlling Excel through COM automation. You could install Open Office on your machine, and see whether you can automate Calc from VB6 to open a spreadsheet and extract an image. I don't know whether it allows that. Excel COM automation is very powerful and allows you to do almost anything, Open Office may not be as powerful (I don't know).
I would advise thinking carefully about what problem you are trying to solve and whether there's another approach entirely!

Use Access VBA to check-out an Excel doc from Sharepoint

I have a rather odd situation, the below code can successfully checkout a file from sharepoint to Excel...
Private sub checkoutfromSP()
Dim loc as String
loc = "Location"
if Workbooks.CanCheckOut(loc) = true then
Workbooks.CheckOut loc
end if
However how does this translate into Access? I always receive the error "This document cannot be checked out" with the following code?
Dim objXL as Excel.Application
Dim loc as String
loc = "Location"
objXL = new Excel.Application
if objXL.Workbooks.CanCheckOut(loc) = True then
objXL.Workbooks.CheckOut loc
end if
Reason for the checkout via Access is there are a few pieces of data that need dropped into Excel from Access, however as the Excel file is on sharepoint I need to checkout/checkin to submit the changes.
Open the document with your Excel instance before checking it out and it should work for you:
Dim objXL As Excel.Application
Dim objWB As Excel.Workbook 'NEW
Dim loc As String
loc = "Location"
Set objXL = New Excel.Application 'Make sure you use Set here
If objXL.Workbooks.CanCheckOut(loc) = True Then
Set objWB = objXL.Workbooks.Open(loc) 'NEW
objXL.Workbooks.CheckOut loc
End If
When you check the workbook back in with the line objWB.CheckIn, Excel automatically closes the Workbook object.

Resources