Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create a messagebox? #756

Closed
NamelessUzer opened this issue Sep 23, 2017 · 6 comments · Fixed by #1994
Closed

How to create a messagebox? #756

NamelessUzer opened this issue Sep 23, 2017 · 6 comments · Fixed by #1994
Milestone

Comments

@NamelessUzer
Copy link

I mean how to run the VBA function "MsgBox" in python, so that I can send something useful to users.

@tagomatech
Copy link

tagomatech commented Nov 12, 2017

Depending on exactly what you want to achieve this can be as simple as writing a VBA macro and calling it from pyrhon:

  • Your VBA macro
Sub MyMsgBox()
    MsgBox "Hello World!"
End Sub
  • On the python side:
# Import Xlwings
import xlwings as xw

# Say you wrote your VBA function in the (sole) workbook currently opened
app = xw.apps[0]

# Assign your macro to a python variable and call it
my_msgBox_macro = app.macro('MyMsgBox')
my_msgBox_macro()

Actually, I was surprised it is that simple.

HTH

@skasi7
Copy link
Contributor

skasi7 commented Jan 29, 2018

You can try with:

import win32api
import win32con
import xlwings as xw

win32api.MessageBox(
  xw.apps.active.api.Hwnd, 'Hello world!', 'Info',
  win32con.MB_ICONINFORMATION)

@estebancsi
Copy link

estebancsi commented Oct 21, 2021

You can try with:

import win32api
import win32con
import xlwings as xw

win32api.MessageBox(
  xw.apps.active.api.Hwnd, 'Hello world!', 'Info',
  win32con.MB_ICONINFORMATION)

Worked Great!!

@skasi7 Where could i find more information or documentation about these 2 modules (win32api, win32con)? It'd be very helpful, thankss.

@fzumstein
Copy link
Member

These days it's even better to do the following instead of relying on the active app:

win32api.MessageBox(
  mybook.app.hwnd, 'Hello world!', 'Info',
  win32con.MB_ICONINFORMATION)

where mybook is your xw.Book object. You can find more infos here:

@ericbmoreira
Copy link

Since this is still open, and I just worked with it, I will add this link. You can use it as Felix's example above, except you do not need the win32con. Just use the hexadecimal number. WITHOUT the 'L' at the end, as you will see in the link.

win32api.MessageBox(
  mybook.app.hwnd, 'Hello world!', 'Info', 0x00000040
)

I wanted an OK/Cancel window so I used the code below. If you assign a variable to it you will still get the window in Excel. Then the variable will be == 1 if it was OK, and == 2 if it was Cancel.

ok_cxl = win32api.MessageBox(
    wb.app.hwnd, 'a string from df.to_string()', 'Large Modifier Changes', 0x00000001
)

@fzumstein
Copy link
Member

fzumstein commented Aug 19, 2022

  • The AppleScript equivalent is:

    tell application "Microsoft Excel"
        display alert "hello"
    end tell

and in appscript:

import xlwings as xw

from osax import OSAX
from appscript import k

# create an OSAX (Open Scripting Architecture Extension)) instance for StandardAdditions
# /System/Library/ScriptingAdditions/StandardAdditions.osax
sa = OSAX(pid=xw.apps.active.pid)
# bring current process to front so dialog box will be visible
sa.activate()
print(sa.display_alert('Hello World!', as_=k.critical))
  ```

See:
https://github.com/hhas/appscript/blob/master/py-appscript/sample/osax/standard_additions.py

and a legacy reference:
https://github.com/mattneub/appscript/blob/master/py-appscript/tags/py-appscript-0.16.2/Lib/osax/wrappers.py#L67-L86

* Google Apps Script:

```js
SpreadsheetApp.getUi().alert("hello")
  • Office Scripts: not sure if possible at all

@fzumstein fzumstein added this to the 0.27.13 milestone Aug 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

6 participants