How to open an excel file from a Sharpoint - vba

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.

Related

Excel Reference Problems Publishing VB.NET 2015 Program

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

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.

Vba closing excel applications/workbooks freezes

I have a problem with the following code:
Dim excelapp as object
set excelapp = CreateObject("excel.application")
dim ws as object
dim wb as Workbook
wb= excelapp.Workbooks.Open(path)
ws= wb.Sheets(1)
'in the code i send the worksheet object around by reference in order to read the
'worksheet and manipulate data, i dont create other instances of excel apps or
'workbooks
then i try :
wb.Close
and i have also tried :
excelapp.Quit
Neither have worked, they both freeze and say they are waiting on OLE actions, and i have multiple excel processes opening if i do not call these, when i try to open the excel files i had opened via code, i can only open them as read-only because theyre checked out to me.
I also tried executing a shell script that closes all applications "Excel.Exe" but it closes...the actual excel file where the vba is being executed, so thats not a good solution.
Thank you in advance.
It might be that the Excel app has detected that the workbook has changed and is putting up a dialog box (which is invisible because the app is not visible). Try:
wb.Close False
Which tells Excel to ignore any changes to the workbook.

How to save a read-only file as a different file extension in an excel macro?

I have a macro setup to automatically open/save a file that I am opening from the web. The web format is a #csv.gz format. I have code that currently just saves the file in the default location (which I have changed to c:\files). I want to write a macro that will keep the filename of the file, but change the extention to just file.xlsm. Is there a way to do this with VBA/excel? The reason while I need to change the extension is because it currently does not work with my formulas. The default save code I have just saves the file as a #csv.txt.
Is this possible?
Integrate this code into your own:
Sub SaveIt()
Dim wkb As Workbook
Set wkb = ActiveWorkbook 'change to your workbook reference
wkb.SaveAs Replace(wkb.Name, ".txt", ""), 52 'change ".txt" to ".csv" if need be
End Sub
See Excel File Type Enum for more information the 52.

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.