Accessing One Drive files offline in VB .Net - excel

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.

Related

How do I close an Excel spreadsheet in VBA code?

I have the following routine below that is meant to open an Excel spreadsheet and then go row by row to import the results into a table that is passed in. It works fine but the problem is if I try to open that same spreadsheet a second time I get a message that the file is in use and I have to Ctrl-Alt-Del to shut down Excel before I can use it again. I thought that the Set mySheet=Nothing and Set xlApp=Nothing would release the file but apparently not. What more can I do to make sure that Access lets go of the Excel file? Thanks in advance!
Public Sub MakeTempTable(strFilePath As String, tablename As String)
Dim mySheet As Object
Dim xlApp As Object
Dim rs As DAO.Recordset
Dim sql As String
sql = "DELETE * FROM " & tablename
DoCmd.RunSQL sql
Set rs = CurrentDb.OpenRecordset(tablename)
Set xlApp = CreateObject("Excel.Application")
Set mySheet = xlApp.Workbooks.Open(strFilePath).Sheets(1)
xlApp.Visible = False
Set mySheet = xlApp.Sheets("Input")
Dim dRows As Double
dRows = 1
Dim dRow As Double, dCol As Double
dRow = 2
On Error GoTo ERR
Do
dCol = 1
rs.AddNew
If mySheet.cells(dRow, 3) = "" Then Exit Do
Do
If mySheet.cells(dRow, dCol).Value <> "_END_" Then
rs.Fields(dCol).Value = Nz(mySheet.cells(dRow, dCol).Value, "")
dCol = dCol + 1
Else
Exit Do
End If
Loop
rs.Update
dRow = dRow + 1
Loop
EXITSUB:
Set mySheet = Nothing
Set xlApp = Nothing
Exit Sub
ERR:
If ERR.Number = 3265 Then MsgBox "The species selected are incompatible. Canceling import.", vbCritical, "IMPORT ERROR"
GoTo EXITSUB
End Sub
Try using
xlApp.Quit
When you set xlApp to nothing you are only clearing the object within the procedure, you aren't doing anything to the actual Excel instance. All that setting XXX = nothing allows you to do is then reuse that object.
You will need to legally close the workbooks that are open as in
xlApp.Workbooks.Close
EXITSUB:
This will close the instances that are open.
Prior to this, kill all the instances or reboot your machine to clear all the instances that are open.

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

How to resolve error remote server machine does not exist or is unavailable?

I want to open many pdfs in specific folder using word document and copy the contents and paste in my excel sheet without format change. The code which i have written was able to perform the task for 2 pdf files only after that it is showing run time error 462 - The remote server machine does not exist or is unavailable. Kindly help me what is going wrong and how i can resolve it? Thanks in advance.
Sub test()
Dim s As String
Dim path As String
Dim col As Integer: col = 1
Dim t As Excel.Range
path = "C:\Users\121919\Desktop\Direct Dispatch Report_PENS\29 Jul\PDF to Word Try\"
s = Dir(path & "*.pdf")
Do Until s = ""
Dim wd As New Word.Application
Dim mydoc As Word.document
Set mydoc = Word.Documents.Open(Filename:=path & s, Format:="PDF Files", ConfirmConversions:=False)
Dim wr As Word.Range
Set wr = mydoc.Paragraphs(1).Range
wr.WholeStory
Set t = ThisWorkbook.Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
wr.Copy
t.PasteSpecial xlPasteValues
mydoc.Close False: s = Dir
wd.Quit '<- This is where the code stops and throw error
Loop
End Sub

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.

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

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()

Resources