Answers.Body: <p>If you're feeling brave - we're using version 0.18.3 (Feb. 2022 release) of PhpWord in Civi.  If you know how to use Composer, you could upgrade to PhpWord 1.1 (May 2023 release).</p>
  <p>If you don't know Composer, you can also probably delete the phpword directory in <code>/vendor/phpoffice</code> and unzip the new one in there.</p>
  <p>Hopefully this is obvious, but don't do either of these on a live site! Use a test site.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-22T12:44:30.313Z
Answers.LastActivityDate: 2023-06-22T12:44:30.313Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>I have customized CiviCase for a client who is using it to manage project grants. Things have gone pretty well overall. There are many custom fields and several related document templates in use. &quot;Print/Merge Document&quot; works fine when generating a pdf, but often fails when generating a docx file.</p>
  <p>The error that is generated on screen is simply &quot;Cannot add TextRun in TextRun.&quot;, but looking into the CiviCRM log shows a little more detail and that the error is in the 3rd-party phpoffice code (which I've never heard of before today).</p>
  <pre><code>    $Fatal Error Details = array:3 [
    &quot;message&quot; =&gt; &quot;Cannot add TextRun in TextRun.&quot;
    &quot;code&quot; =&gt; null
    &quot;exception&quot; =&gt; BadMethodCallException {#12247
      #message: &quot;Cannot add TextRun in TextRun.&quot;
      #code: 0
      #file: &quot;/var/www/sitename/wordpress/wp-content/plugins/civicrm/civicrm/vendor/phpoffice/phpword/src/PhpWord/Element/AbstractContainer.php&quot;
      #line: 266
      ...
      /var/www/sitename/wordpress/wp-content/plugins/civicrm/civicrm/CRM/Utils/PDF/Document.php:90 {
         CRM_Utils_PDF_Document::html2doc($pages, $fileName, $format = [])
      ›   $section = $phpWord-&gt;addSection($pageStyle + ['breakType' =&gt; 'nextPage']);
      ›   \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
      › }
  </code></pre>
  <p>The error only occurs when one of the custom fields of type &quot;Rich Text Editor&quot; is populated. All of the simpler fields seem fine. I'm not familiar with this code and so am not really sure whether this is a bug (and out of my power to fix) or some glitch in my custom fields that should be corrected.</p>
  <p>Any thoughts?</p>
  <p>UPDATE: Converting the rich text field type to multi-line text didn't fix the problem. Accented characters were converted to html entities and paragraph tags were added (or maybe kept) in the filed conversion. However, deleting the content and re-adding it as UTF-8 text with the accents into the multi-line field did prevent the error. I guess there is a bug in the phpoffice code with respect to html entities. Changing the field type is not a fix, but I may be able to live with it if there is no better solution.</p>
  <p>Running CiviCRM 5.61.0 on WordPress 6.2.2 on Debian Bullseye</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-06-21T21:47:41.107Z
Comments.Text: PDF generation is a bit of a dark art and it sounds like you've identified the source of the error as non-UTF-8 non-ascii characters. Your 'rich text field' in combination with wordpress would be responsible for how accented text gets encoded when submitting to CiviCRM, so that's where I'd look for a better solution.
Comments.UserDisplayName: Alan Dixon
Tags: <civicase><printmerge>
Title: error "Cannot add TextRun in TextRun." when generating docx file in CiviCase
ThreadText: # error "Cannot add TextRun in TextRun." when generating docx file in CiviCase
  
  (Asked by: username_0 on 2023-06-20)
  
  I have customized CiviCase for a client who is using it to manage project grants. Things have gone pretty well overall. There are many custom fields and several related document templates in use. "Print/Merge Document" works fine when generating a pdf, but often fails when generating a docx file.
  The error that is generated on screen is simply "Cannot add TextRun in TextRun.", but looking into the CiviCRM log shows a little more detail and that the error is in the 3rd-party phpoffice code (which I've never heard of before today).
      $Fatal Error Details = array:3 [
    "message" => "Cannot add TextRun in TextRun."
    "code" => null
    "exception" => BadMethodCallException {#12247
      #message: "Cannot add TextRun in TextRun."
      #code: 0
      #file: "/var/www/sitename/wordpress/wp-content/plugins/civicrm/civicrm/vendor/phpoffice/phpword/src/PhpWord/Element/AbstractContainer.php"
      #line: 266
      ...
      /var/www/sitename/wordpress/wp-content/plugins/civicrm/civicrm/CRM/Utils/PDF/Document.php:90 {
         CRM_Utils_PDF_Document::html2doc($pages, $fileName, $format = [])
      ›   $section = $phpWord->addSection($pageStyle + ['breakType' => 'nextPage']);
      ›   \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
      › }
  
  The error only occurs when one of the custom fields of type "Rich Text Editor" is populated. All of the simpler fields seem fine. I'm not familiar with this code and so am not really sure whether this is a bug (and out of my power to fix) or some glitch in my custom fields that should be corrected.
  Any thoughts?
  UPDATE: Converting the rich text field type to multi-line text didn't fix the problem. Accented characters were converted to html entities and paragraph tags were added (or maybe kept) in the filed conversion. However, deleting the content and re-adding it as UTF-8 text with the accents into the multi-line field did prevent the error. I guess there is a bug in the phpoffice code with respect to html entities. Changing the field type is not a fix, but I may be able to live with it if there is no better solution.
  Running CiviCRM 5.61.0 on WordPress 6.2.2 on Debian Bullseye
  
  **Tags:** civicase, printmerge
  
  > **Comments:**
  > **username_1**: PDF generation is a bit of a dark art and it sounds like you've identified the source of the error as non-UTF-8 non-ascii characters. Your 'rich text field' in combination with wordpress would be responsible for how accented text gets encoded when submitting to CiviCRM, so that's where I'd look for a better solution.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2023-06-22. Score: 1
  
  If you're feeling brave - we're using version 0.18.3 (Feb. 2022 release) of PhpWord in Civi. If you know how to use Composer, you could upgrade to PhpWord 1.1 (May 2023 release).
  
  If you don't know Composer, you can also probably delete the phpword directory in `/vendor/phpoffice` and unzip the new one in there.
  
  Hopefully this is obvious, but don't do either of these on a live site! Use a test site.
  
  
  ---
  
  