Opening Excel file located in one drive - excel

I need to open the excel file located in share point using Excel.Application
In Visual Basic 6, I am using following to open the excel file located in Sharepoint:
Shell "C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE https://livecnm-my.sharepoint.com/:x:/g/personal/sghimire_cnm_edu/EZwyv4Ot1_xEnIeT1SIADncBVtRQ0b6THoJj0eLrbpEjXQ?e=NgyEBD", vbNormalFocus
But i want to use Excel.Application instead of shell so, i can hide ribbon, status bar, formula bar and other stuff before excel file open.I have tried following but it not working:
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Workbooks.Open "https://livecnm-my.sharepoint.com/:x:/g/personal/sghimire_cnm_edu/EZwyv4Ot1_xEnIeT1SIADncBVtRQ0b6THoJj0eLrbpEjXQ?e=NgyEBD"
.Application.ExecuteExcel4Macro "Show.toolbar(""Ribbon"",False)"
.Application.DisplayStatusBar = False
.Application.DisplayFormulaBar = False
.Application.DisplayScrollBars = True
.Application.Visible = True
End With

Maybe you give this a try
Sub open_excel_from_one_drive()
Dim sfilename As String
Dim xl As Excel.Application
Dim xlsheet As Workbook
sfilename = "https://d.docs.live.net/78a58439bd7a4267/Investment%20Spreadsheet/Customer%20Management/Security%20Data.xlsm"
Set xl = Application
Set xlsheet = xl.Workbooks.Open(Filename:=sfilename)
End Sub
If you want to go on using your code then you could try
Sub dummy_Code()
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Workbooks.Open "https://d.docs.live.net/78a58439bd7a4267/Investment%20Spreadsheet/Customer%20Management/Security%20Data.xlsm"
.Application.ExecuteExcel4Macro "Show.toolbar(""Ribbon"",False)"
.Application.DisplayStatusBar = False
.Application.DisplayFormulaBar = False
.Application.DisplayScrollBars = True
.Application.Visible = True
End With
End Sub

Related

Save changes in an Excel spreadsheet that were made using MS Access VBA

In my database, I have a VBA script that opens an Excel spreadsheet and removes the first row so the new first row is the header row.
When I save the file in Access VBA and open the file in Excel, nothing is there.
If I reimport the spreadsheet, the data is visible.
I changed my script around and if I don't save the changes the file is ok. If I save the changes then this problem appears.
dim sheetpath as string
dim xl as excel.application
dim xlbook as excel.workbook
dim xlsheet as excel.worksheet
sheetpath = "c:\users\me\export.xlsx"
set xl = createobject("Excel.Application")
set xlbook = GetObject(sheetpath)
xl.visible = true
set xlsheet = xlbook.Worksheets(1)
If xlsheet.Range("a1").mergecells = true then
xlsheet.cells.unmerge
end if
if xlsheet.range("a1") = "Values" then
xlsheet.rows(1).delete
end if
xlbook.close savechanges:=true
xl.application.qit
set xl = nothing
set xlbook = nothing
set xlsheet = nothing
Try a reboot.
But before you do that, check in Task Manager / Process Explorer, if you have multiple Excel processes still running.
Instead of
set xl = createobject("Excel.Application")
set xlbook = GetObject(sheetpath)
do
Set xlbook = GetObject(sheetpath)
Set xl = xlbook.Application
What's this?
xl.application.qit
It should be
xl.Quit
And clear your references in the correct order, from bottom to top:
set xlsheet = nothing
set xlbook = nothing
set xl = nothing

Outlook VBA: Activating a Workbook, Activating a Row, Inserting Copied Rows

I have an Outlook Macro that saves attachments based on a search of an e-mail inbox. The Aggregation File is then opened, then a loop opens the first of the saved attachments and copies the "AggregateThis" named range.
What I need to achieve is:
1). Activate the Aggregation File
2). Activate the Row where the result of the search for "END" is located
3). Insert the copied cells above end
The Outlook Object model is giving me trouble, this would be a total cinch in Excel VBA. Your help would mean so much!
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.EnableEvents = False
.DisplayAlerts = False
.ScreenUpdating = False
.Workbooks.Open ("J:\Retail Finance\Varicent\General Teamshare Resources\Acting Mgr Assignment Bonus Aggregation.xlsx")
Dim x As Variant
i = -1
For Each x In AttachNames
Dim wb As Object
i = i + 1
Set wb = .Workbooks.Open("J:\Retail Finance\Varicent\General Teamshare Resources\Teamshare AAA\" & AttachNames(i))
Set wb = .Worksheets("Additional Assignment Bonus FRM")
'Copies the "Aggregate This" named range from the Individual File (i)
With wb.Range("AggregateThis")
.Copy
End With
'Switches focus to Aggregation File
Set wb = .Workbooks("J:\Retail Finance\Varicent\General Teamshare Resources\Acting Mgr Assignment Bonus Aggregation.xlsx")
With wb
.Activate '#1). I want to put focus on this file it throws an error
End With
'Find EndRow in the Aggregation File
Set wb = .Worksheets("Additional Assignment Bonus FRM").Cells.Find("End")
With wb
.ActivateRow '#2).This throws an error
.PasteSpecialInsertRows '#3). This doesnt work
End With
Next
The original code didn't work properly because, for .Activate to work, ScreenUpdating must be set to True (which it is by default).
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.EnableEvents = False
.DisplayAlerts = False
.ScreenUpdating = True '## Was set to False in code originally##
.Workbooks.Open ("J:\Retail Finance\Varicent\General Teamshare Resources\Acting Mgr Assignment Bonus Aggregation.xlsx")
Dim x As Variant
i = -1
For Each x In AttachNames
Dim wb As Object
i = i + 1
Set wb = .Workbooks.Open("J:\Retail Finance\Varicent\General Teamshare Resources\Teamshare AAA\" & AttachNames(i))
With xlApp
.Worksheets("Additional Assignment Bonus FRM").Range("AggregateThis").Copy 'Copies Range
End With
Set wb = .Workbooks.Open("J:\Retail Finance\Varicent\General Teamshare Resources\Acting Mgr Assignment Bonus Aggregation.xlsx")
With wb
.Worksheets("Additional Assignment Bonus FRM").Rows.Find("End").Select
.Worksheets("Additional Assignment Bonus FRM").Activerange.Paste '##This needs to be fixed##, will edit response soon.
End With
Next
End With
End Sub

How to retrieve data from Excel and add to Word

I have a Word template file that retrieves data from an Excel file to populate a form.
The code looks something like this:
Dim myXL As Object
Set myXL = Getobject("myfile.xls")
myXL.Application.Visible = True
myXL.Parent.Windows(1).Visible = True
This code works fine in Office 2010 and 2007, but when I try it in 2013, it gives run time error 9 which is an array subscript error. When I check the Windows array it has zero elements, so error is correct.
How do I achieve the same result in 2013?
The next bit of code attempts to access the Worksheets("mysheet") and if I skip the Visible = True line accessing the worksheet gives runtime error 1004.
Any help with fixing this would be greatly appreciated.
To make the code work on Office 2013 I added the line myXL.Activate before trying to make the Window visible. So the code becomes:
Dim myXL As Object
Set myXL = Getobject("myfile.xls")
myXL.Application.Visible = True
myXL.Activate
myXL.Parent.Windows(1).Visible = True
This fixed the run-time error, and the code went back to working well.
To retrieve data from an Excel
An Example would be...
Option Explicit
Sub ExcelData()
Dim xlApp As Object ' Application
Dim xlBook As Object ' Workbook
Dim xlSht As Object ' Worksheet
Dim FilePath As String
FilePath = "C:\Temp\Book1.xlsx"
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(FilePath)
Set xlSht = xlBook.Sheets("Sheet1")
With ActiveDocument
.Content = xlSht.Range("A1").Value
End With
xlApp.Visible = True
Set xlApp = Nothing
Set xlBook = Nothing
End Sub

VBScript that runs a macro in excel throws an error

I have a vbscript that opens an excel workbook and runs a particular macro inside at a particular time on a particular day. Here it is
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
While not (Month(Now()) = 11 and Day(Now()) = 17 and Hour(Now()) = 9 and Minute(Now()) = 1)
Wend
Set xlBook = xlApp.Workbooks.Open("C:\Temp\Property by Peril.xlsm")
xlApp.Application.Run "'Property by Peril.xlsm'!Main"
Set xlBook = Nothing
Set xlApp = Nothing
WScript.Echo "Finished."
WScript.Quit
It runs fine up to and including the opening of the workbook and the running of the macro. However, immediately after the macro runs, I get an error
Microsoft VBScript runtim error: Unknown runtime error (on line 16)
and the rest of the script does not get executed.
Any thoughts/help?
Try removing the statement to close the workbook from the macro and adding it to the VBScript. Also, don't forget to quit the application object, otherwise your hidden Excel instance will keep lingering in memory, because it's not automatically discarded when the script terminates.
...
xlApp.Application.Run "'Property by Peril.xlsm'!Main"
xlBook.Close False
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
...
The parameter False will prevent changes to the workbook from being saved. Change it to True if you want changes to be automatically saved.
The following code ran successfully on my system.
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Open("C:\Temp\Property by Peril.xlsm")
xlApp.Application.Run "Main"
xlBook.Close False
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
WScript.Echo "Finished."
WScript.Quit

Excel application not closing from Outlook VBA function

I'm writing a sort of homegrown ticketing system for myself in Outlook VBA, and I'm using Excel to store all the persistant data. I have a function written in Outlook to get some data from the .csv and return it. This is all working fine, but after I close the workbook, quit the application, and set the app to nothing I still have an Excel process running! Here is my code:
Private Function GetNewTicketNumber() As Integer
Dim xlApp As Excel.Application
Set xlApp = New Excel.Application
With xlApp
.Visible = False
.EnableEvents = False
.DisplayAlerts = False
End With
Dim FileStr As String
Dim NumberBook As Workbook
Dim TheRange As Range
FileStr = "C:\OMGITSAPATH.csv"
Set NumberBook = Workbooks.Open(FileStr)
Set TheRange = NumberBook.Worksheets(1).Range("A1")
GetNewTicketNumber = TheRange.Value
TheRange.Value = TheRange.Value + 1
NumberBook.Save
NumberBook.Close
xlApp.Quit
With xlApp
.Visible = True
.EnableEvents = True
.DisplayAlerts = True
End With
Set xlApp = Nothing
End Function
Is there something that I'm doing wrong here? My problem is similar to the one here, but I have disabled DisplayAlerts... What can I do to fix this problem?
Try fully qualifying your references to Excel, from xl_doesnt_quit
The problem presented here is exactly what you have. This line
Range("a1").Value = Range("a1").Value + 1
leave the xl instance open
The most common cause of the problem is a 'global' reference to the automated application. Unfortunately, under some circumstances it is possible to directly refer to an entity (property/method/object) of the automated object. This reference effectively is global to the calling application. Hence, the reference remains in place as long as the calling program is active. Consequently, the operating system will not end the automated application while the caller is active.
Re-cut code below (which also uses late binding - which rules out the unqualified possibility).
Pls change you path to suit.
code
Private Function GetNewTicketNumber() As Long
Dim xlApp As Object
Dim objWB As Object
Dim objWs As Object
Dim FileStr As String
FileStr = "C:\temp\test.xlsx"
Set xlApp = CreateObject("excel.application")
With xlApp
.EnableEvents = False
.DisplayAlerts = False
End With
Set objWB = xlApp.Workbooks.Open(FileStr)
Set objWs = objWB.Sheets(1)
GetNewTicketNumber = objWs.Range("A1")
objWs.Range("A1") = objWs.Range("A1") + 1
objWB.Save
objWB.Close
Set objWB = Nothing
xlApp.Quit
Set xlApp = Nothing
End Function

Resources