Reading and updating OpenXML in F# -



Reading and updating OpenXML in F# -

i can't f# code read , update content command text fields within word documents. sec function absolutely nil , first 1 produces error: unhandled exception of type 'system.invalidoperationexception' occurred in system.core.dll additional information: sequence contains no elements

namespace openxml open documentformat.openxml open documentformat.openxml.packaging open documentformat.openxml.wordprocessing open system.linq // add together documentformat.openxml assembly // add together windowsbase assembly module public word = allow query_plain_text_content_control document_path_and_file_name content_control_tag = utilize thedoc = wordprocessingdocument.open((document_path_and_file_name :string), true) allow mainpart = thedoc.maindocumentpart allow block = mainpart.document.body.descendants<sdtelement>().where(fun r -> r.sdtproperties.getfirstchild<tag>().val = content_control_tag).single() allow t = block.descendants<text>().firstordefault() t.text allow update_plain_text_content_control document_path_and_file_name content_control_tag new_text = async { utilize thedoc = wordprocessingdocument.open((document_path_and_file_name :string), true) allow mainpart = thedoc.maindocumentpart allow block = mainpart.document.body.descendants<sdtelement>().where(fun r -> r.sdtproperties.getfirstchild<tag>().val = content_control_tag).single() allow t = block.descendants<text>().firstordefault() t.text <- new_text mainpart.document.save() |> ignore }

seems work fine, couple of tweaks:

#r@"documentformat.openxml.dll" #r"windowsbase.dll" open documentformat.openxml open documentformat.openxml.packaging open documentformat.openxml.wordprocessing open system.linq // add together documentformat.openxml assembly // add together windowsbase assembly module public word = allow query_plain_text_content_control document_path_and_file_name content_control_tag = utilize thedoc = wordprocessingdocument.open((document_path_and_file_name :string), true) allow mainpart = thedoc.maindocumentpart allow block = mainpart.document.body.descendants<sdtelement>().where(fun r -> r.sdtproperties.getfirstchild<tag>().val.tostring() = content_control_tag).single() allow t = block.descendants<text>().firstordefault() t.text allow update_plain_text_content_control document_path_and_file_name content_control_tag new_text = async { utilize thedoc = wordprocessingdocument.open((document_path_and_file_name :string), true) allow mainpart = thedoc.maindocumentpart allow block = mainpart.document.body.descendants<sdtelement>().where(fun r -> r.sdtproperties.getfirstchild<tag>().val.tostring() = content_control_tag).single() allow t = block.descendants<text>().firstordefault() t.text <- new_text mainpart.document.save() |> ignore } allow oldtext = query_plain_text_content_control @".\text.docx" "ctrltag" allow update = update_plain_text_content_control @".\text.docx" "ctrltag" "new text" async.runsynchronously(update) allow newtext = query_plain_text_content_control @".\text.docx" "ctrltag"

.. on document containing single plaintext content control, tag of 'ctrltag', content 'old text', get:

val oldtext : string = "old text" val update : async<unit> val newtext : string = "new text"

without calling .tostring() on 'r.sdtproperties.getfirstchild().val', got error:

the type 'string' not compatible type 'stringvalue'.

perhaps there problem document? error getting seem suggest there no content controls specified tag.

f#

Comments

Popular posts from this blog

javascript - I need to update the text of a paragraph by inline edit -

javascript - THREE.js reposition vertices for RingGeometry -

assembly - What is the addressing mode for ld, add, and rjmp instructions? -