Excel Reference Problems Publishing VB.NET 2015 Program - vb.net

I am at the point where I need to publish my program and share it with a team of people. The problem is I reference and excel file to calculate data in the program. I cannot figure out how to share the program without including specific instructions on where to put the excel file in their file system.
I have tried adding the file using the "Setup Wizard" but i'm not sure then how to reference the file path if I include the excel file.
I have also tried importing the excel file to the program, but cannot figure out how to call it to open when it is a resources instead of a file location.
Public APP As New Excel.Application
Public worksheet As Excel.Worksheet
Public workbook As Excel.Workbook
workbook = APP.Workbooks.Open("C:\Desktop\Plant Simulator\Database\Raw Data.xlsx")
worksheet = workbook.Worksheets("Sheet1")

You should be able to place the file in the installation directory and refer to that path with a VB.net command:
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)

I used the "Setup Wizard" and added my excel file "Raw Data.xlxs" as a reference.
Instead of using:
workbook = APP.Workbooks.Open("C:\Desktop\Plant Simulator\Database\Raw Data.xlsx")
I used this and it worked.
workbook = APP.Workbooks.Open(My.Application.Info.DirectoryPath & "\Raw Data.xlsx")

Related

Office 365 Workbooks.open from SharePoint Online opens blank file

Our company is using Office 365 Pro Plus on Semi-Annual update channel. Lately we've been updated from version 1708 to 1803.
We are using some automation macros than open files from our SharePoint online tenant. Since the update, a call of Workbooks.open("https://xxxxxx.sharepoint.com/sites/.../xxx.xlsx") now prompts for an O365 user and password every time, and don't use the one used as Excel account.
It opens a "blank" workbook (see screenshot) :
Workbooks.open() on local documents is still working as before, only opening files from SP Online has this problem.
I tried to switch to monthly channel on a computer, but the problem persists.
Opening the file manually using the link is working.
Here is a sample code we're using :
Sub Transfert_SST_Copy()
Dim Tbl As ListObject
Dim NewRow As ListRow
Dim Data As ListRow
Dim Wb As Workbook
Set Wb = Workbooks.Open(Filename:="https://xxxxx.sharepoint.com/:x:/r/sites/XXX/AMTB%20RFQ%20costing/00%20General/Overview%20RFQs/Cost%20computations%20simple%20overview/2018%20AMTB_Cost_computations_overview_simple.xlsm?d=w8929b5112ed7496bb25d82b0bfc472c5&csf=1&e=PADrRt", ReadOnly:=False)
' Here Excel opens a "Blank" Workbook, so everything after that is giving an error
Set Tbl = Range("CostCalcOverview").ListObject
Set NewRow = Tbl.ListRows.Add(AlwaysInsert:=True)
NewRow.Range.Offset(0, 1).Resize(1, ThisWorkbook.Worksheets("Transfert").Range("A275:AW275").Count).Value = ThisWorkbook.Worksheets("Transfert").Range("A275:AW275").Value
End Sub
Hi I had the same issue - and it was getting to me as it seemed such an easy thing!
The problem is with your filename link.
Set Wb = Workbooks.Open(Filename:="https://xxxxx.sharepoint.com/:x:/r/sites/XXX/AMTB%20RFQ%20costing/00%20General/Overview%20RFQs/Cost%20computations%20simple%20overview/2018%20AMTB_Cost_computations_overview_simple.xlsm?d=w8929b5112ed7496bb25d82b0bfc472c5&csf=1&e=PADrRt", ReadOnly:=False)
The filename link you used is directly from SharePoint when you click "Share" or "Copy link", but what resolved this for me is by actually opening the SharePoint file in Desktop Excel. Then click File. You are then shown the "Info" page which shows the File Name - if you look directly below the filename (above where it says "Protect Workbook") there looks to be like breadcrumb links (folder names from SharePoint separated by >>) - click that section and click "Copy path to clipboard"
Replace your Filename link in your code with this and it will work!

How to open an excel file from a Sharpoint

I'm trying to open an excel file from a sharepoint site using VBA. I used the code from this page, but all it did was pull up my file explorer. I have also tried this code which I usually use for opening a closed workbook, not on a sharpoint.
dim wbk as workbook
set wbk = Workbooks.Open("Filepath")
Using this method returns an error telling me my file path is bad or non-existent, which my file path is correct. Any help would be appreciated. Thanks!
Dim wbk as Workbook
Set wbk = Workbooks.Open(Filename:="\\filepath\file.xlsm", UpdateLinks:=False, ReadOnly:=True)
You need to include the syntax "filename". This iteration also opens the file as a read-only copy. To open as a file you can edit, remove the "ReadOnly:=True" portion of the code.

Excel 2013 VBA Workbooks.Open opens with wrong filename

When I attempt to open a certain target .xls file using the Workbooks.Open command in an Excel VBA macro, the target Excel file opens with a "1" appended to the end of the file name. This altered filename shows up in the file window (). With each subsequent attempt (manually closing the target Excel file & re-running the script) the appended number at the end of the target filename increments by one.
The target Excel file itself does not appear to be corrupted, as I am able to open the file manually in windows explorer without any issue. Also, when I do open it this way, the filename shows up correctly (without the '1' appended to the end). Looking at the 'Type' column in the Explorer window, it shows that the target file is 'Microsoft Excel 97-2003 Worksheet', so the file extension does match with the actual file format.
Below is the code that I am using:
Public Sub Import_TD_TVs()
Dim ActiveWB As Workbook
Dim srcTDWB As Workbook
Dim wkbkStr As String
Set ActiveWB = ActiveWorkbook
wkbkStr = "X:\sites\DTS_Data_Files.xls"
Set srcTDWB = Workbooks.Open(wkbkStr, , False)
End Sub
Any assistance on this issue would be greatly appreciated!
This can happen if the target workbook was originally saved as a template (.xlt) file and then the extension manually changed (to .xls) in Windows Explorer. You can open the target workbook and do a 'save as...' to .xls file format.

open excel sheet pull the value from a cell and store it as a string in vb.net

Basically guys in vb.net I need to open an excel document and read the value of a certain cell(A1 for example) be it text or numerical and store it as a string value to use elsewhere in my code.
Thanks in advance.
Try
'start Excel app
Dim App1 As Microsoft.Office.Interop.Excel.Application
App1 = CreateObject("Excel.Application")
' load excel document
exApp.Workbooks.Open(fname)
Dim Sheet1 As Microsoft.Office.Interop.Excel.Worksheet
Sheet1 = exApp.Workbooks(1).Worksheets(1)
a = Sheet1.Range("A1").Value.ToString
You can use Excel Interop as in this example here. Or you can use Open XML SDK as in this example here. For XLSX file you can also develop a custom solution, xlsx are nothing but zipped folder of XML files, this might be helpfull.

How to open a file in an active workbook? VB.NET 2008

I have a program that filters data and then outputs it to a tabg delimited file. I then use Excel Interop to open this file in Excel because directly outputting to a worksheet takes too long. So currently I am using this code:
AD.DF.WriteToFile(vbTab)
Dim WB As Workbook = ExcelApp.Workbooks.Open(AD.DF.DatafileInfo.WriteToFileLocation)
ExcelApp.Visible = True
The first line takes the filtered data and outputs to a tab delimited file. The second opens that same file in a new workbook in Excel. And obviously the third makes Excel visible. Here is my problem though: right now when this code is run there are two open workbooks. I already have an active workbook and I would just like to open this file to that workbook.
If this is possible, how do I do it?
Thank you.
Look at the GetObject function.
Dim MyXL As Object
MyXL = GetObject(, "Excel.Application")
should get you a reference to the currently running instance of Excel.
In the code I created an object that is an Excel Workbook. I then set the created workbook as the ExcelApp.ActiveWorkbook. Then I was able to open the file without another workbook being created.