It doesn't seem to be a Reading Layout or View setting. I followed all the
instructions on the linked website and still have the same problem on multiple
computers. Perhaps there was a change that happened with Office 365 as I'm
running Microsoft 365 Apps for Enterprise Version 2302 Build 16.0.16130.20298
from my school that prevents the editing of fields that the cursor is in for
some reason?
But if you've run the code that I just sent (which edits fields which have the
cursor inside of them) and it works, it means that there's probably something
changed in the office version. I wish there was an easy way to find out the
different options I can run on field objects, etc. that are allowed instead of
guessing and checking with dir. I may try to make a work around that just
deletes the field and then makes a new citation field which is a combination of
the two instead, messy, but it'll work hopefully.
Yours truly,
Ryan Dikdan
________________________________
From: Steven Manross <[email protected]>
Sent: Friday, March 17, 2023 2:04 AM
To: Ryan Dikdan <[email protected]>; [email protected]
<[email protected]>
Subject: RE: How to add citations to a word document
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<https://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F17594211%2Fyou-are-not-allowed-to-edit-this-selection-because-it-is-protected-but-only-s&data=05%7C01%7Crjd254%40njms.rutgers.edu%7C8e46890754004ee24db408db26ad83d4%7Cb92d2b234d35447093ff69aca6632ffe%7C1%7C0%7C638146298949688736%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=eQigV%2Fy9bdqzqvouYeOJU3SHbSbW%2BOpDHtnw8ab5GkE%3D&reserved=0>
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 <[email protected]>
Sent: Thursday, March 16, 2023 10:12 PM
To: Steven Manross <[email protected]>; [email protected]
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<file://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<file://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<file://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 <[email protected]<mailto:[email protected]>>
Sent: Wednesday, March 1, 2023 6:43 PM
To: Ryan Dikdan <[email protected]<mailto:[email protected]>>;
[email protected]<mailto:[email protected]>
<[email protected]<mailto:[email protected]>>
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:
<b:Source
xmlns:b="https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fschemas.openxmlformats.org%2FofficeDocument%2F2006%2Fbibliography&data=05%7C01%7Crjd254%40njms.rutgers.edu%7C1eb7c0945071410f7a2c08db1aaeb821%7Cb92d2b234d35447093ff69aca6632ffe%7C1%7C0%7C638133110076478769%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=AsqVPr1onGfezeYnBY4NpCgC3PZBfeVTqHO8Ird%2FdTs%3D&reserved=0"><b:Tag>Ste20</b:Tag><b:SourceType>Book</b:SourceType><b:Guid>{92931D8B-470B-4359-A4B8-3C53859A1B3F}</b:Guid><b:Author><b:Author><b:NameList><b:Person><b:Last>Manross</b:Last><b:First>Steven</b:First></b:Person></b:NameList></b:Author></b:Author><b:Title<https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fschemas.openxmlformats.org%2FofficeDocument%2F2006%2Fbibliography&data=05%7C01%7Crjd254%40njms.rutgers.edu%7C8e46890754004ee24db408db26ad83d4%7Cb92d2b234d35447093ff69aca6632ffe%7C1%7C0%7C638146298949688736%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=NGgwpyKKLqaRUEKO3mlcbEb1WFld8u5Cmr9%2FiI2v2xs%3D&reserved=0>>How
To Add A Bibliography Entry</b:Title><b:Year>2023</b:Year><b:City>Nowhere, AZ<
/b:City><b:Publisher>Some Really Good
Publisher</b:Publisher><b:RefOrder>1</b:RefOrder></b:Source>
# NOW use the Word GUI to Delete the source from the Bibliography info then
save the document using the GUI
And then you can use the code above to verify there are no more sources:
And last but not least, re-add your source info programmatically:
import win32com.client
wordapp = win32com.client.gencache.EnsureDispatch("Word.Application")
wordapp.Visible = True
doc = wordapp.Documents.Open(r"C:\\temp\\testing.docx")
myxml = '<b:Source
xmlns:b="https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fschemas.openxmlformats.org%2FofficeDocument%2F2006%2Fbibliography&data=05%7C01%7Crjd254%40njms.rutgers.edu%7C1eb7c0945071410f7a2c08db1aaeb821%7Cb92d2b234d35447093ff69aca6632ffe%7C1%7C0%7C638133110076478769%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=AsqVPr1onGfezeYnBY4NpCgC3PZBfeVTqHO8Ird%2FdTs%3D&reserved=0"><b:Tag>Ste20</b:Tag><b:SourceType>Book</b:SourceType><b:Guid>{92931D8B-470B-4359-A4B8-3C53859A1B3F}</b:Guid><b:Author><b:Author><b:NameList><b:Person><b:Last>Manross</b:Last><b:First>Steven</b:First></b:Person></b:NameList></b:Author></b:Author><b:Title<https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fschemas.openxmlformats.org%2FofficeDocument%2F2006%2Fbibliography&data=05%7C01%7Crjd254%40njms.rutgers.edu%7C8e46890754004ee24db408db26ad83d4%7Cb92d2b234d35447093ff69aca6632ffe%7C1%7C0%7C638146298949688736%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=NGgwpyKKLqaRUEKO3mlcbEb1WFld8u5Cmr9%2FiI2v2xs%3D&reserved=0>>How
To Add A Bibliography Entry</b:Title><b:Year>2023</b:Year><b:City>Nowhere, AZ<
/b:City><b:Publisher>Some Really Good
Publisher</b:Publisher><b:RefOrder>1</b:RefOrder></b:Source>'
doc.Bibliography.Sources.Add(myxml)
doc.Save()
wordapp.Quit()
I'm pretty sure that your Bibliography XML is likely not formatted the way Word
wants it.. Microsoft likes their own formats on everything.
If It matters, I tested this using Word 2019 but it should be backwards
compatible all the way back to 2007.
HTH and Enjoy
P.S. This is not something that is limited to Python.. It can be done in just
about any language that can access COM objects... VBScript, Perl, Python, etc
and there are tons of references on using VB and or VBScript macros that I
found (but not much on exactly what the source XML looked like)
From: python-win32
<[email protected]<mailto:[email protected]>>
On Behalf Of Ryan Dikdan
Sent: Tuesday, February 28, 2023 11:35 PM
To: [email protected]<mailto:[email protected]>
Subject: [python-win32] How to add citations to a word document
Hello, I'm not sure how this mailing list works for support, but I'm trying to
make a python script that formats downloaded citations and inserts them into
word (avoiding all the hassle of external citation managers). I found pywin32
as the only way, but the documentation on doc.Bibliography.Sources.Add is
scarce, so I was wondering if you all could help me. I have properly formatted
xml of sources (since I could upload it to the source manager successfully,
even though I think it's encoded in utf8 bom or sig or something). But whenever
I put either the string of that content or the filename into the above Add()
command in python it says the XML isn't formatted properly. I'd appreciate any
help. Thanks!
_______________________________________________
python-win32 mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-win32