Anthony Cox Anthony Cox - 27 days ago 7
Vb.net Question

Reading and Writing to an Excel file in Resources

I've added my excel file as a resource to my vb project and used the "embedded Resource" option. The name of the file "StoredInformation.xlsx".
The 'Build Action' is set to 'Embedded Resource'.

Edit II - However i'm quite stuck again. It seems as if my code doesnt even work when i run my program

Dim sPath As String

sPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))

For i = 1 To 50
Threading.Thread.Sleep(100)
Application.DoEvents()

Next

If My.Computer.FileSystem.FileExists(sPath & "\Housing\Stored Information.xlsx") Then
Dim APP As New Excel.Application
workbook = APP.Workbooks.Open(sPath & "\Housing\Stored Information.xlsx")
worksheet = workbook.Worksheets("Sheet1")
APP.Visible = False
MessageBox.Show("File Opened!" & Environment.NewLine & "Path: " & sPath & "\Housing\Stored Information.xlsx")
Else
My.Computer.FileSystem.WriteAllBytes(sPath & "\Housing\Stored Information.xlsx", My.Resources.StoredInformation, True)
Dim APP As New Excel.Application
workbook = APP.Workbooks.Open(sPath & "\Housing\Stored Information.xlsx")
worksheet = workbook.Worksheets("Sheet1")
APP.Visible = False
MessageBox.Show("File Created!" & Environment.NewLine & "Path: " & sPath & "\Housing\Stored Information.xlsx")
End If


It's not creating my folder "Housing" or my file "Stored Information.xlsx"
Could someone please take a look and tell me what im doing wrong?

Edit - I found out that it can't create the new dir because of the protection level, so when i changed the destination to the desktop it created the excel file, but it needed to be repaired.

Can someone tell me how to created a new folder and add a file without damaging the excel file?

Edit - Made changes to the code, it's still not creating the new folder in My Documents and add the file "Stored Information.xlsx"

Answer

Unraveling the confusion about Embedded Resources - Found this thread

Explains how to embed resources that you use for your program and how to access it. It deffinetly helped me

Edit: Answer

Dim sPath As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Housing")

    Dim Fpath As String = sPath & "\Stored Information.xlsx"

    IO.Directory.CreateDirectory(sPath) ' If location already exists it will not do anything

    If My.Computer.FileSystem.FileExists(Fpath) = False Then
       My.Computer.FileSystem.WriteAllBytes(Fpath, My.Resources.StoredInformation, True) ' Don't want to append data (although that would not happen in this instance) so True is used for that.
    End If

    Dim APP As New Excel.Application
    workbook = APP.Workbooks.Open(Fpath)
    worksheet = workbook.Worksheets("Sheet1")
    APP.Visible = False
    MessageBox.Show("File Opened!" & Environment.NewLine & "Path: " & Fpath)
Comments