/// <summary> /// An inline rich text control can't contain carriage returns. /// </summary> /// <param name="control"></param> public Word.ContentControl convertToBlockLevel(Word.ContentControl currentCC, bool keepContents, bool updateScreen) { Word.Document document = Globals.ThisAddIn.Application.ActiveDocument; OpenDoPEModel.DesignMode designMode = new OpenDoPEModel.DesignMode(document); // Only do it if the content control is rich text if (!currentCC.Type.Equals(Word.WdContentControlType.wdContentControlRichText)) { log.Warn("convert to block level only operates on rich text controls, not " + currentCC.Type); return(null); } string majorVersionString = Globals.ThisAddIn.Application.Version.Split(new char[] { '.' })[0]; int majorVersion = Convert.ToInt32(majorVersionString); // Only do it if the content control is not already block level // if (isBlockLevel(currentCC)) { return(currentCC); } // Can only do this if the content control is not // nested within some other inline content control if (currentCC.ParentContentControl != null && !isBlockLevel(currentCC.ParentContentControl)) { MessageBox.Show("This content control contains block level content, but can't be converted automatically. Please correct this yourself, by deleting it, and re-creating at block level."); return(null); } if (majorVersion >= 14) { getWordApp().UndoRecord.StartCustomRecord("Promote content control to block-level"); } bool ccIsAtPStart = ccStartsAtStartOfParagraph(currentCC); object collapseStart = Word.WdCollapseDirection.wdCollapseStart; //object collapseEnd = Word.WdCollapseDirection.wdCollapseEnd; object unitCharacter = Word.WdUnits.wdCharacter; // Get a range start of cc. Word.Range ccRange = currentCC.Range; ccRange.Collapse(ref collapseStart); // Delete the cc, but preserve Tag, Title string tagVal = currentCC.Tag; string titleVal = currentCC.Title; string contents = currentCC.Range.Text; currentCC.Delete(true); ccRange.Select(); if (ccIsAtPStart) { document.Windows[1].Selection.TypeParagraph(); } else { // Insert 2 new paragraphs document.Windows[1].Selection.TypeParagraph(); document.Windows[1].Selection.TypeParagraph(); } // Create a cc around the first new p object start = ccRange.Start + 1; object end = ccRange.Start + 1; object newRange = document.Range(ref start, ref end); log.Info("target {0}, {1}", ((Word.Range)newRange).Start, ((Word.Range)newRange).End); designMode.Off(); Word.ContentControl newCC = document.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, ref newRange); designMode.restoreState(); newCC.Tag = tagVal; newCC.Title = titleVal; if (keepContents) // want to do this for XHTML { newCC.Range.Text = contents; } if (updateScreen) { newCC.Application.ScreenUpdating = true; newCC.Application.ScreenRefresh(); } return(newCC); // Approach: // .. Get the paragraph // .. Make a copy // .. in the copy, delete up to our position // .. in the original, delete after our position //Word.Range splittingPoint = sel.Range; //object para1DeleteStartPoint = splittingPoint.Start; //Word.Range paraOrig = Globals.ThisAddIn.Application.ActiveDocument.Range(ref para1DeleteStartPoint, ref para1DeleteStartPoint); //paraOrig.MoveStart(ref unitParagraph, ref back1); //int lengthStartSegment = paraOrig.End - paraOrig.Start; //object startPoint = paraOrig.Start; //paraOrig.MoveEnd(ref unitParagraph, ref forward1); //object endPoint = paraOrig.End; //object endPointPlusOne = paraOrig.End + 1; //// copy it //Word.Range insertPoint = Globals.ThisAddIn.Application.ActiveDocument.Range(ref endPoint, ref endPoint); //paraOrig.Copy(); //insertPoint.Paste(); //// In the copy, delete the first half //// (do this operation first, to preserve our original position calculations) //object para2DeleteEndpoint = (int)endPoint + lengthStartSegment; //Word.Range para2Deletion = Globals.ThisAddIn.Application.ActiveDocument.Range(ref endPoint, ref para2DeleteEndpoint); //para2Deletion.Delete(); //// In the original, delete the second half //Word.Range para1Deletion = Globals.ThisAddIn.Application.ActiveDocument.Range(ref para1DeleteStartPoint, ref endPoint); //para1Deletion.Delete(); if (majorVersion >= 14) { getWordApp().UndoRecord.EndCustomRecord(); } }
/// <summary> /// An inline rich text control can't contain carriage returns. /// </summary> /// <param name="control"></param> public Word.ContentControl convertToBlockLevel(Word.ContentControl currentCC, bool keepContents, bool updateScreen) { Word.Document document = Globals.ThisAddIn.Application.ActiveDocument; OpenDoPEModel.DesignMode designMode = new OpenDoPEModel.DesignMode(document); // Only do it if the content control is rich text if (!currentCC.Type.Equals(Word.WdContentControlType.wdContentControlRichText)) { log.Warn("convert to block level only operates on rich text controls, not " + currentCC.Type); return null; } string majorVersionString = Globals.ThisAddIn.Application.Version.Split(new char[] { '.' })[0]; int majorVersion = Convert.ToInt32(majorVersionString); // Only do it if the content control is not already block level // if (isBlockLevel(currentCC)) return currentCC; // Can only do this if the content control is not // nested within some other inline content control if (currentCC.ParentContentControl != null && !isBlockLevel(currentCC.ParentContentControl)) { MessageBox.Show("This content control contains block level content, but can't be converted automatically. Please correct this yourself, by deleting it, and re-creating at block level."); return null; } if (majorVersion >= 14) { getWordApp().UndoRecord.StartCustomRecord("Promote content control to block-level"); } bool ccIsAtPStart = ccStartsAtStartOfParagraph(currentCC); object collapseStart = Word.WdCollapseDirection.wdCollapseStart; //object collapseEnd = Word.WdCollapseDirection.wdCollapseEnd; object unitCharacter = Word.WdUnits.wdCharacter; // Get a range start of cc. Word.Range ccRange = currentCC.Range; ccRange.Collapse(ref collapseStart); // Delete the cc, but preserve Tag, Title string tagVal = currentCC.Tag; string titleVal = currentCC.Title; string contents = currentCC.Range.Text; currentCC.Delete(true); ccRange.Select(); if (ccIsAtPStart) { document.Windows[1].Selection.TypeParagraph(); } else { // Insert 2 new paragraphs document.Windows[1].Selection.TypeParagraph(); document.Windows[1].Selection.TypeParagraph(); } // Create a cc around the first new p object start = ccRange.Start+1; object end = ccRange.Start+1; object newRange = document.Range(ref start, ref end); log.Info("target {0}, {1}", ((Word.Range)newRange).Start, ((Word.Range)newRange).End); designMode.Off(); Word.ContentControl newCC = document.ContentControls.Add(Word.WdContentControlType.wdContentControlRichText, ref newRange); designMode.restoreState(); newCC.Tag = tagVal; newCC.Title = titleVal; if (keepContents) // want to do this for XHTML { newCC.Range.Text = contents; } if (updateScreen) { newCC.Application.ScreenUpdating = true; newCC.Application.ScreenRefresh(); } return newCC; // Approach: // .. Get the paragraph // .. Make a copy // .. in the copy, delete up to our position // .. in the original, delete after our position //Word.Range splittingPoint = sel.Range; //object para1DeleteStartPoint = splittingPoint.Start; //Word.Range paraOrig = Globals.ThisAddIn.Application.ActiveDocument.Range(ref para1DeleteStartPoint, ref para1DeleteStartPoint); //paraOrig.MoveStart(ref unitParagraph, ref back1); //int lengthStartSegment = paraOrig.End - paraOrig.Start; //object startPoint = paraOrig.Start; //paraOrig.MoveEnd(ref unitParagraph, ref forward1); //object endPoint = paraOrig.End; //object endPointPlusOne = paraOrig.End + 1; //// copy it //Word.Range insertPoint = Globals.ThisAddIn.Application.ActiveDocument.Range(ref endPoint, ref endPoint); //paraOrig.Copy(); //insertPoint.Paste(); //// In the copy, delete the first half //// (do this operation first, to preserve our original position calculations) //object para2DeleteEndpoint = (int)endPoint + lengthStartSegment; //Word.Range para2Deletion = Globals.ThisAddIn.Application.ActiveDocument.Range(ref endPoint, ref para2DeleteEndpoint); //para2Deletion.Delete(); //// In the original, delete the second half //Word.Range para1Deletion = Globals.ThisAddIn.Application.ActiveDocument.Range(ref para1DeleteStartPoint, ref endPoint); //para1Deletion.Delete(); if (majorVersion >= 14) { getWordApp().UndoRecord.EndCustomRecord(); } }