ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Call API from vbscript via excel. (https://www.excelbanter.com/excel-programming/392331-call-api-vbscript-via-excel.html)

[email protected]

Call API from vbscript via excel.
 
I'm trying to write a script that uses MS Office to make API calls to
prevent logoff/shutdown from happening (I'm using Office as proxy
since you can't directly call an API from a script). I found a sample
script on the internet that uses MS Excel to make the API call,
GetTickCount. What changes do I need to make in order to get this
script to work for the API call AbortSystemShutdown. Let me explain
why I'm asking. I work at a software company and we use VMware
Workstation all the time to do manual QE testing and run automation on
virtual machines. Every so often someone will log off a computer
while VMs are running. This would always be a real pain when they
were shut off without warning. I need to write a script that would
check for the vmware-vmx executable file and (if the executable is
found) display a message to the user telling them VMs are running and
prevent shut down or log off from happening. Otherwise, if the
executible isn't found, do nothing, let the shutdown proceed. I have
two scripts to do this. The first searches for the executible. The
second script below is only for making the API call to prevent
shutdown, if the executible is found. How do I get it work?

<?xml version="1.0" encoding="ISO-8859-1" ?
<package
<job
<object id="xl" progid="Excel.Application"/
<script language="VBScript"
<![CDATA[
' blatantly stolen from:
' HOWTO: Create and Call an Excel Macro Programmatically from VB
' (Q194611)
' Start Excel
' Make it visible...
xl.Visible = True
' Add a new workbook
Set xlBk = xl.Workbooks.Add
' Add a module
Set xlMod = xlBk.VBProject.VBComponents.Add(1)
' Add a macro to the module...
strCode = Getresource("mac")
xlMod.CodeModule.AddFromString strCode
' Run the new macro!
xl.Run "MyMacro"
Set xlMod = Nothing
xlBk.Saved = True
xl.Quit
]]
</script
<resource id="mac"
<![CDATA[
Declare Function AbortSystemShutdown Lib "advapi32" Alias
"AbortSystemSHutdownA" ( _
ByVal lpmachineName As String) As Long
Sub MyMacro()
AbortSystemShutdown
End Sub
]]
</resource
</job
</package


Peter T

Call API from vbscript via excel.
 
Although you can call API's with Excel-VBA I don't see how you could detect
user logging off from Excel. The most you could do (I think) is detect when
user closes Excel, though even to do that reliably is better done in a
Com-addin rather than VBA.

You might be able to achieve what you want with a small executable, try
asking in
..microsoft.public.vb.general.discussion

Regards,
Peter T

wrote in message
ups.com...
I'm trying to write a script that uses MS Office to make API calls to
prevent logoff/shutdown from happening (I'm using Office as proxy
since you can't directly call an API from a script). I found a sample
script on the internet that uses MS Excel to make the API call,
GetTickCount. What changes do I need to make in order to get this
script to work for the API call AbortSystemShutdown. Let me explain
why I'm asking. I work at a software company and we use VMware
Workstation all the time to do manual QE testing and run automation on
virtual machines. Every so often someone will log off a computer
while VMs are running. This would always be a real pain when they
were shut off without warning. I need to write a script that would
check for the vmware-vmx executable file and (if the executable is
found) display a message to the user telling them VMs are running and
prevent shut down or log off from happening. Otherwise, if the
executible isn't found, do nothing, let the shutdown proceed. I have
two scripts to do this. The first searches for the executible. The
second script below is only for making the API call to prevent
shutdown, if the executible is found. How do I get it work?

<?xml version="1.0" encoding="ISO-8859-1" ?
<package
<job
<object id="xl" progid="Excel.Application"/
<script language="VBScript"
<![CDATA[
' blatantly stolen from:
' HOWTO: Create and Call an Excel Macro Programmatically from VB
' (Q194611)
' Start Excel
' Make it visible...
xl.Visible = True
' Add a new workbook
Set xlBk = xl.Workbooks.Add
' Add a module
Set xlMod = xlBk.VBProject.VBComponents.Add(1)
' Add a macro to the module...
strCode = Getresource("mac")
xlMod.CodeModule.AddFromString strCode
' Run the new macro!
xl.Run "MyMacro"
Set xlMod = Nothing
xlBk.Saved = True
xl.Quit
]]
</script
<resource id="mac"
<![CDATA[
Declare Function AbortSystemShutdown Lib "advapi32" Alias
"AbortSystemSHutdownA" ( _
ByVal lpmachineName As String) As Long
Sub MyMacro()
AbortSystemShutdown
End Sub
]]
</resource
</job
</package




NickHK

Call API from vbscript via excel.
 
Sounds more like you require a Service.
I can't see a Script or Office solution being reliable as you describe.

Or maybe something with Admin scripts/policies, but then that's not an Excel
concern.

NickHK

wrote in message
ups.com...
I'm trying to write a script that uses MS Office to make API calls to
prevent logoff/shutdown from happening (I'm using Office as proxy
since you can't directly call an API from a script). I found a sample
script on the internet that uses MS Excel to make the API call,
GetTickCount. What changes do I need to make in order to get this
script to work for the API call AbortSystemShutdown. Let me explain
why I'm asking. I work at a software company and we use VMware
Workstation all the time to do manual QE testing and run automation on
virtual machines. Every so often someone will log off a computer
while VMs are running. This would always be a real pain when they
were shut off without warning. I need to write a script that would
check for the vmware-vmx executable file and (if the executable is
found) display a message to the user telling them VMs are running and
prevent shut down or log off from happening. Otherwise, if the
executible isn't found, do nothing, let the shutdown proceed. I have
two scripts to do this. The first searches for the executible. The
second script below is only for making the API call to prevent
shutdown, if the executible is found. How do I get it work?

<?xml version="1.0" encoding="ISO-8859-1" ?
<package
<job
<object id="xl" progid="Excel.Application"/
<script language="VBScript"
<![CDATA[
' blatantly stolen from:
' HOWTO: Create and Call an Excel Macro Programmatically from VB
' (Q194611)
' Start Excel
' Make it visible...
xl.Visible = True
' Add a new workbook
Set xlBk = xl.Workbooks.Add
' Add a module
Set xlMod = xlBk.VBProject.VBComponents.Add(1)
' Add a macro to the module...
strCode = Getresource("mac")
xlMod.CodeModule.AddFromString strCode
' Run the new macro!
xl.Run "MyMacro"
Set xlMod = Nothing
xlBk.Saved = True
xl.Quit
]]
</script
<resource id="mac"
<![CDATA[
Declare Function AbortSystemShutdown Lib "advapi32" Alias
"AbortSystemSHutdownA" ( _
ByVal lpmachineName As String) As Long
Sub MyMacro()
AbortSystemShutdown
End Sub
]]
</resource
</job
</package




[email protected]

Call API from vbscript via excel.
 
This script is a logoff script that will run everytime someone logoffs
a computer via group policy. I don't want my script to run when a
user closes Excel. I only want it to run when a user logs off a
computer so that it can check for VMware's executible. If the
executible is found I need to use macros in excel to call the API to
prevent logoff from happening. I didn't know a user could "log off"
excel. I'll try the other newsgroup you suggested.

As far as needing a service... I don't know how to program that. My
knowledge is only in scripting.



All times are GMT +1. The time now is 01:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com