Business Objects Launch from Excel - excel

I am in the process of creating a Business Objects macro for myself using VBA. I have found this code and others similar across the forums everywhere. However upon compiling this in VBA, everything just starts to freeze at the Set BoApp stage. Am I missing something in creating the object? Does the BusinessObjects.application have to say something else that is specific to my Business Objects directory?
Any help is appreciated!
Sub Open_Reports()
Dim BoApp As Object
Application.DisplayAlerts = False
Application.Wait (Now + TimeValue("1:00:00"))
On Error Resume Next
Set BoApp = CreateObject("BusinessObjects.application")
With BoApp
.Visible = True
.LoginAs "username", "password", , "DVBOCEN-APP01"
.documents.Open ("Q:\MI Reporting (BAU)\BAU Daily\Operational Reports\All reports\Sales - Advisor - Daily.rep")
With .Activedocument
.Refresh
.Close
End With
.Application.Quit
End With
Set BoApp = Nothing
ThisWorkbook.Saved = True
.Quit
End Sub

Not sure if it'll help, since it should work either way, but try:
Dim BoApp As busobj.Application
and
Set BoApp = New busobj.Application
I'm assuming you're able to successfully launch the DeskI application manually, and that you've added the BusinessObjects x Object Library as a reference?

Related

Open & Check out Excel workbook from SharePoint

I'm trying to write data into an Excel workbook that is hosted in our SharePoint document library.
I instantiate Excel from Microsoft Project.
I tried the following:
Check if file can be checked out
If it can be checked out, then open it
Here's the code snippet:
If ExcelApp.Workbooks.CanCheckOut (FileURL) = True Then
Set NewBook = ExcelApp.Workbooks.Open(FileName:=FileURL, ReadOnly:=False)
ExcelApp.Workbooks.CheckOut (FileURL)
Else
MsgBox "File is checked out in another session."
End If
The CanCheckOut function always returns FALSE. I'm not able to tell when a file can be checked out by the Excel instance.
Is it not working because I'm calling the VBA code from MS Project?
My app should be able to check if a file is not checked out, then check it out, update it, and save + check it back in.
I've found through trial and error that Workbooks.CanCheckOut (Filename:= FullName) where FullName is the URL for the SharePoint file only works for files that are not open in the current instance of Excel.
The method will always return False if you have the file open in the current instance of Excel which is obviously the case here.
Workbooks.CheckOut (ActiveWorkbook.FullName) opens the file, checks it out and then inexplicably, closes the file. So opening and checking out a SharePoint file becomes a 3 step process.
Sub CheckOutAndOpen()
Dim TestFile As String
TestFile = "http://spserver/document/Test.xlsb"
If Workbooks.CanCheckOut(TestFile) = True Then
Workbooks.CheckOut(TestFile)
Workbooks.Open (TestFile)
Else
MsgBox TestFile & " can't be checked out at this time.", vbInformation
End If
End Sub
This is all a bit counter intuitive because when working manually with SharePoint files you have to open them to see if they can be checked out and then perform the check-out operation.
Neither MSDN or Excel VBA help mention that the Workbooks.CanCheckOut (Filename:= FullName) method always returns False if you have the file open in the current instance of Excel.
The other methods never worked for me. This will CheckOut the file and either open it hidden and terminate (Visible = False), or you can just have it open (Visible = True) and remove the Quit, BUT while the doc is Checked out, I can't seem to target or check in that mXLApp doc further. The solution is to not leave the mXLApp doc open, but then once closed to open that same doc as normal, and then it will Check in with the Check in code line.
Sub TestCheckOut()
Dim FileName as String
FileName = "http://spserver/document/Test.xlsx"
SP_CheckOut FileName
End Sub
Sub SP_CheckOut(docCheckOut As String)
Set mXlApp = CreateObject("Excel.Application")
' Determine if workbook can be checked out.
' CanCheckOut does not actually mean the doc is not currently checked out, but that the doc can be checked in/out.
If mXlApp.Workbooks.CanCheckOut(docCheckOut) = True Then
mXlApp.Workbooks.Open fileName:=docCheckOut
mXlApp.Workbooks.CheckOut docCheckOut
' False is hidden
mXlApp.Visible = False
mXlApp.Quit
Set mXlApp = Nothing
Workbooks.Open fileName:=docCheckOut
Else
MsgBox "Unable to check out this document at this time."
End If
End Sub
As for Checkin, can't get any methods to work except:
Workbooks(CheckName).checkin SaveChanges:=True, Comments:=""
Sub CheckIn(CheckName As String, CheckPath As String)
' Must be open to save and then checkin
Dim wb As Workbook
On Error Resume Next
Set wb = Workbooks(CheckName)
If Err = 0 Then
WorkbookIsOpen = True
Else
WorkbookIsOpen = False
Set wb = Workbooks.Open(CheckPath)
End If
wb.CheckIn SaveChanges:=True, Comments:=""
End Sub
I did try using a Query on the SharePoint browser link to determine who has the doc checked out (if anyone). This worked sometimes. If it did work, half the time it would take too long to be useful, and the other half of the time it would throw a timeout error. Not to mention the query would disrupt other processes, like saving or certain other macros. So I put together a WebScrape which quickly returns who might have the doc checked out.
Sub TestWho()
Dim SPFilePath As String
SPFilePath = "http://teams.MyCompany.com/sites/PATH/PATH/Fulfillment/Forms/AllItems.aspx"
Debug.Print CheckedByWho(SPFilePath , "YOURdocName.xlsx")
End Sub
Function CheckedByWho(ShareFilePath As String, ShareFileName As String)
Dim ie As Object
Dim CheckedWho As String
Dim ImgTag As String
Dim CheckStart, CheckEnd As Integer
Dim SplitArray() As String
Set ie = GetObject("new:{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}")
With ie
.Visible = False
.Navigate ShareFilePath
Do Until .readyState = 4: DoEvents: Loop
Do While .Busy: DoEvents: Loop
End With
CheckedWho = "Not Check Out"
For Each objLink In ie.document.getElementsByTagName("img")
ImgTag = objLink.outerHTML
CheckedOutPos = InStr(objLink.outerHTML, ShareFileName & "
Checked Out To:")
If CheckedOutPos > 0 Then
CheckStart = InStr(objLink.outerHTML, "Checked Out To: ")
CheckedWho = Mid(objLink.outerHTML, CheckedOutPos + 41)
SplitArray = Split(CheckedWho, """")
CheckedWho = SplitArray(0)
End If
Next objLink
CheckedByWho = CheckedWho
ie.Quit
End Function

Can't work on Word CommandButton object from within Excel

I'm writing an Excel macro that opens up a Word document and looks for a CommandButton object, by Name. When it finds the object, it tries to check if it has a picture associated with it. It seems to be locating the object, but dies a "catastrophic" death when I try to reference the handle of the picture. I've done this before and looking to see if the picture's handle is zero has worked for me. Not sure what's up here, maybe someone else can see what I'm missing?
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open(strFileName)
objWord.Visible = True
Set cmdSignatureButton = fncGetCommandButtonByName("NameOfCommandButtonImLookingFor", objDoc)
MsgBox "h=" & cmdSignatureButton.Picture.Handle
' It dies here, giving the error:
' Runtime error -2147418113 (8000ffff)
' Automation error
' Catastrophic failure
Private Function fncGetCommandButtonByName(strName As String, objDoc As Word.Document)
Dim obj As Object
Dim i As Integer
For i = objDoc.InlineShapes.Count To 1 Step -1
With objDoc.InlineShapes(i)
If .Type = 5 Then
If .OLEFormat.Object.Name = strName Then
Set fncGetCommandButtonByName = .OLEFormat.Object
MsgBox "Found the Command Button object" ' Seems to find the CommandButton object here
Exit Function
End If
End If
End With
Next
End Function
I was able to get this functioning without an issue. You may want to step through the code to see if the document is fully loaded first.
Here's the code that's working for me, edited to match the format of the original question posed.
Dim objWord As Object: Set objWord = CreateObject("Word.Application")
Dim objDoc As Object: Set objDoc = objWord.Documents.Open(strFileName)
objWord.Visible = True
Dim cmdSignatureButton As Object
Set cmdSignatureButton = fncGetCommandButtonByName("CommandButton1", objDoc)
If Not cmdSignatureButton Is Nothing Then
'Do something when it isn't nothing
MsgBox "h=" & cmdSignatureButton.Picture.Handle
Else
'Something here
End If
Private Function fncGetCommandButtonByName(strName As String, objDoc As Word.Document) As Object
Dim i As Integer
For i = objDoc.InlineShapes.Count To 1 Step -1
With objDoc.InlineShapes(i)
If .Type = 5 Then
If .OLEFormat.Object.Name = strName Then
Set fncGetCommandButtonByName = .OLEFormat.Object
Exit Function
End If
End If
End With
Next
Set fncGetCommandButtonByName = Nothing 'set it equal to nothing when it fails
End Function
If you are still receiving that error, I'm thinking it may have something to do with the picture not being fully loaded. If so, I'd add some error handling to catch that error and process a retry a second later to see if the picture's handle is available.
Here's what I get when I run that code:
OK, I think I have an approach, at least. I moved on to my next problem, which is very similar. In this case, I am looking for images within Command Buttons within an Excel spreadsheet, but I'm doing so from Access. Instead of trying to jump through hoops and get Access VBA to interrogate the Excel file, I put a Public Function into the Excel file that Access calls. Excel has no problem checking the button for an image, so it just returns the answer for me.
Had to figure out how to Run Public Functions, but that was easy enough. Thanks for the feedback, Ryan. Still not sure why yours worked and mine didn't, but at least I got around it.

VBA Compile error What is the cause and solution?

Set myIE = New InternetExplorer
myIE.Silent = True
myIE.navigate sURL
myIE.Visible = False
Do While myIE.Busy
Do Until myIE.ReadyState = READYSTATE_COMPLETE
Loop
Loop
Application.Wait (Now() + TimeValue("00:00:02"))
Set HTMLDoc = myIE.document
HTMLDoc.getElementById("loginID").Value = ICUSER
HTMLDoc.getElementById("password").Value = ICPASS
For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next
Application.Wait (Now() + TimeValue("00:00:02"))
' Read and Write Data to the reports
Application.DisplayAlerts = False
End Sub
I have been using this code for a while and It works just fine on my computer, but when I share the file with others some computers can not run the code and gives this error. Any one know the solution to this issue, please help.
My best guess is that there is a different in versions of Internet Explorer. When faced with version issues between different computers in VBA you can switch from using Tools>>References to Late binding.
To use late binding here change your code from
Set myIE = New InternetExplorer
To:
Set myIE = CreateObject("InternetExplorer.Application")
Now, as long as the other computer has any version of Internet Explorer installed, this code will work.
Check that the references mentioned in the code are sill set (Tools>References...). If that doesn't work, last resort is to change the order of the references (Tools menu in vb window). Unfortunately it is just trial and error..

VBA Excel interacting with IE, gives unusual runtime error

I'm using some code found here elsewhere on Stack, but I get an unusual run time error that I can't seem to find any info on:
Run-time error '-2147023179 (800706b5)'
Automation error
The interface is unknown.
With so little info on it, and what info existing being barely relevant, I started to think it has to do with References. I added one that I read about that seemed like it could help (Microsoft Internet Controls), but it didn't. Am I missing something others that may be required? Any direction would really be helpful.
The error occurs on the following line of code:
IE.Document.getElementById("CRM_Number").Value = "CRM12345"
This is the ORIGINAL code, which I only changed the website and the ElementID (can only access on VPN so won't help to link here). Presumably, this will work fine unless it's been changed since posted:
Dim IE As Object
Sub submitFeedback3()
Application.ScreenUpdating = False
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://spreadsheetbootcamp.com/excel-efficiency-trainer-feedback/"
Application.StatusBar = "Submitting"
' Wait while IE loading...
While IE.Busy
DoEvents
Wend
**********************************************************************
IE.Document.getElementById("Form_Attempts-1372643500")(0).Value = "1"
IE.Document.getElementById("submit-1-1372643500")(0).Click
**********************************************************************
Application.StatusBar = "Form Submitted"
IE.Quit
Set IE = Nothing
Application.ScreenUpdating = True
End Sub

I am getting run time error 462

HI,
I have the following piece of code in Access.
Dim objSht As excel.Worksheet
Dim objexcel As New excel.Application
Dim wbexcel As excel.Workbook
Dim wbExists As Boolean
Dim objRange As excel.Range
Dim isFileAlreadyPresent As Boolean
Set objexcel = CreateObject("excel.Application")
Set wbexcel = objexcel.Workbooks.Open(file_name)
Set objSht = wbexcel.Worksheets(table_name)
isFileAlreadyPresent = True
objSht.Activate
objSht.Range(Range_para).Select
Charts.Add
ActiveChart.chartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Sheets(table_name).Range(Range_para), _
PlotBy:= xlColumns
ActiveChart.Location Where:=xlLocationAsNewSheet
ActiveChart.HasLegend = False
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.text = CHart_title
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
End With
If isFileAlreadyPresent = True Then
wbexcel.Save
Else
wbexcel.SaveAs (file_name)
End If
objexcel.Visible = True
wbexcel.Close
I am having two problems. Every second time I run the code I get an run time error 462 (The remote server machine does not exist or is unavailable ) at line Charts.add.
I know that I am not using the objexcel property correctly but I am not sure where I am going wrong.
Also after the code is run, even though excel closes. The process runs in the background and this interferes with the next run of the code. How do I close excel and get rid of it from task manager processes also?
I think you will need to create the chart object like this, since your using late binding it won't know what "Charts" is unless you call it from the parent object.
objexcel.Charts.Add
Error 462 usually means something isn't qualified right, even though the message is sort of cryptic.
Just as a quick fix trying setting everything you declare/use to nothing at the end of your code to ensure that nothing is left open or active.
Set objSht = Nothing
Let me know if that fixes the problem
As you your question
How do I close excel and get rid of it
from task manager processes also?
You should be able to use the Application.Quit command at the end of your code as long as you aren't running the sub from another application other than Excel. Or, you should be able to do an objexcel.Quit command. Another alternative method is to delegate this to a shell command: Shell "taskkill /f /im excel.exe".
I hope this helps. Did you get a working piece of code yet?

Resources