Re: [python-win32] How to enable events for Python Active Scripting?

2023-03-17 Thread Wuping Xin via python-win32
I am still struggling with PyActiveScripting COM events.

Is there a way to "step into" the framework code while running the C++ host?  I 
found PyDump - how can I make use of it?

Thank you
Wuping


-- Original Message --
From "Mark Hammond" 
mailto:mhamm...@skippinet.com.au>>
To "Wuping Xin" mailto:wup...@caliper.com>>
Cc "Python-win32@python.org" 
mailto:Python-win32@python.org>>
Date 3/14/2023 12:47:27 AM
Subject Re: [python-win32] How to enable events for Python Active Scripting?

WARNING EXTERNAL EMAIL


Feel free to put a PR up!

Cheers,

Mark

On 14/03/2023 3:10 pm, Wuping Xin wrote:
Thank you again Mark.

In framework.py, Line 223 - Line 235, I see that it only searches for [source, 
default] interface on the coclass.  That means,  multiple event interface is 
NOT supported for now.

I think I'll have to use "win32com.client.connect.SimpleConnection()" to 
manually create the Source/EventSink connection.

Wuping


-- Original Message --
From "Mark Hammond" 
mailto:mhamm...@skippinet.com.au>>
To "Wuping Xin" mailto:wup...@caliper.com>>
Cc "Python-win32@python.org" 
mailto:Python-win32@python.org>>
Date 3/13/2023 10:55:03 PM
Subject Re: [python-win32] How to enable events for Python Active Scripting?

WARNING EXTERNAL EMAIL


IIRC, each of these objects is a "named item" - so whatever name you use when 
you call AddNamedItem() is the prefix and the suffix is the name of the 
function on the event interface. I'm really not sure how multiple event 
interfaces are supported, if at all.

Cheers,

Mark



On 14/03/2023 1:12 pm, Wuping Xin wrote:
Hi Mark,

- "you would define a function called "ObjectName_EventName"

Thank you again! This is valuable info.  In the following pseudo-code,  the 
Host COM Object implements TWO Events interfaces.  How should I defined 
relevant event sink functions?  For example, if the object instance is named 
"MyHostObject",  should I just directly define the following event sink 
functions?

MyHostObject.Do_A_Event()
MyHostObject.Do_B_Event()
MyHostObject.Do_C_Event()
MyHostObject.Do_D_Event()


interface _IMondayEvents
{
Do_A_Event;
Do_B_event;
}

interface _ITuesdayEvents
{
Do_C_Event;
Do_D_event;
}

coclass HostApplication
{
[default] interface IHostApplication;
[source] interface _IMondayEvents;
[source] interface _ITuesdayEvents;
};


-- Original Message --
From "Mark Hammond" 
mailto:mhamm...@skippinet.com.au>>
To "Wuping Xin" mailto:wup...@caliper.com>>
Cc "Python-win32@python.org" 
mailto:Python-win32@python.org>>
Date 3/13/2023 8:45:37 PM
Subject Re: [python-win32] How to enable events for Python Active Scripting?

WARNING EXTERNAL EMAIL


[adding the list back in]

I'm still not quite sure what you are trying to do. If you are trying to use 
Active Scripting, then things should "just work" in the same way they work for 
other Active Scripting languages. The IE demos show us handling events from IE. 
This all ties together with interfaces described in typelibs and using 
IConnectionPoint etc. In this world you don't implement Python classes which 
derive from EventSink or ScriptItem, you would define a function called 
"ObjectName_EventName"  - eg the "Image_OnClick()" you will find in 
mousetrack.htm.

If you are just trying to use the IConnectionPoint etc without the Active 
Scripting framework, then your best bet is to check out 
win32com\demos\connect.py, and specifically how it uses 
"win32com.client.connect.SimpleConnection()" with the CLSID of an object 
implementing the interface which fires the events.

I'm afraid it has been many years since I've worked on anything related to COM 
or events, so I really don't have great insights into the finer details. I've 
also no additional demos - everything relevant I have is in the repo.

Cheers,

Mark

On 14/03/2023 10:41 am, Wuping Xin wrote:

Hi Mark,

Thank you again for your suggestion.

Here is my use case:
1.  C++ Host Application, which exposes a COM object model (with COM
events).
2.  Users are expected to write Python script to interact with the COM
object model.

Following your advice, I read the following source files
- win32comext\axscript\client\framework.py
- win32comext\axscript\test\testHost.py

But I am still somewhat unsure how to create event sinks at the Python
script side.
- Should I add a user-define Python class that inherits from EventSink
or ScriptItem?

Do you think you might be able to provide more hints or it would be
great if there is an example to illustrate this?

Much appreciated!

Wuping Xin, PE, PhD
Chief Technology Officer | Caliper Corporation
|
1172 Beacon St, Ste 300 • Newton MA 02461 USA
Mobile: 617-564-1676 • Main: 617-527-4700

-- Original Message --
From "Mark Hammond"

To "Wuping Xin" 

Re: [python-win32] How to add citations to a word document

2023-03-17 Thread Steven Manross
A quick google of your error led me to this, although I am not 100% sure it 
will solve your issue… however, it is worth trying at a minimum.

https://stackoverflow.com/questions/17594211/you-are-not-allowed-to-edit-this-selection-because-it-is-protected-but-only-s

They suggest changing the View and explain that Microsoft blocks certain thing 
in Reading Layout.

This linked website also has some comments about changing security settings of 
the document in the Trust Center which may also be of some help, but I did not 
validate or test their suggestions.

Good luck, as I did not have this problem (likely because I started from a 
blank document).

Steven
From: Ryan Dikdan 
Sent: Thursday, March 16, 2023 10:12 PM
To: Steven Manross ; python-win32@python.org
Subject: Re: How to add citations to a word document

Thank you!! I've made a lot of progress. I've been able to reformat citations, 
but I've hit another roadbump that chat gpt can't help me with anymore. I 
successfully have a script that can take a citation file and load it into word 
and automatically place a citation, but if I want to put a citation on top of 
an existing citation Field, it says that the field is protected. I can't find a 
way to unprotect it, either through the selection or the document itself. 
Here's my code:

`word = win32.GetActiveObject("Word.Application")

doc = word.ActiveDocument

doc.Bibliography.Sources.Add(xml_string)

cursor_pos = word.Selection.Range.Start

fields = doc.Fields
current_field = None
for field in fields:
if cursor_pos >= field.Result.Start and cursor_pos <= field.Result.End:
current_field = field
break

if current_field is not None:
if current_field.Type == win32.constants.wdFieldCitation:
current_code = current_field.Code.Text
# \\m is just used to show that there are multiple sources in 
this one citation.
current_field.Code.Text = current_code + f'\\m {tag}'
else:
# \\l 1033 just says means that it's in english
current_field = doc.Range(cursor_pos, 
cursor_pos).Fields.Add(doc.Range(cursor_pos, cursor_pos), 
win32.constants.wdFieldCitation, Text=f" {tag} \\l 1033", 
PreserveFormatting=True)

current_field.Update()`

But this gives the error pywintypes.com_error:
(-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'You are not allowed 
to edit this selection because it is protected.', 'wdmain11.chm', 25604, 
-2146822164), None)​

when the cursor is inside of another field. Any other way to edit or unprotect 
the field would be appreciated. I was thinking of deleting and remaking the 
field, but this duplicates the references I think.


 Yours truly,
  Ryan Dikdan

From: Steven Manross mailto:ste...@manross.net>>
Sent: Wednesday, March 1, 2023 6:43 PM
To: Ryan Dikdan mailto:rjd...@njms.rutgers.edu>>; 
python-win32@python.org 
mailto:python-win32@python.org>>
Subject: RE: How to add citations to a word document

I'd probably start by creating a test Word document and adding a source 
manually in it from the Word GUI.

Then, I would likely want to do something like this in python.

# Sample python to list the bibliography entries
import win32com.client


wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")

wordapp.Visible = True

doc = wordapp.Documents.Open(r"C:\\temp\\testing.docx")

for source in doc.Bibliography.Sources:
print("source: " + source.XML)
# Now save the XML as a variable:
myxml = source.XML

wordapp.Quit()

That should give you the properly formatted source XML that Word is looking 
for  And you can then modify a known working version of the XML so you can 
add all your bibliographical information in Word.

My test (using the above process) showed up like this: