Ilya Cherkasov

Hello everyone!

Is there any easy way to join selected paragraphs into one I mean I select for example 3 paragraphs, I push button and I'd like to see all 3 of them joined into one.

I searched through msdn but did not succeeded, so i will appreciate any help!

Thanks!

Ilya Cherkasov,

RBC Soft



Re: Visual Studio Tools for Office Howto: Word 2003 AddIn: Join selected paragraphs into one

Cindy Meister

There's nothing in the object model that will do this for you.

In essence, however, it's just a matter of locating all the paragraph marks (ANSI character 13) between the paragraphs and deleting them (you don't want to delete the last one). You need to be aware that this may affect the formatting, as deleting a paragraph mark will also remove the paragraph-level formatting.

There are a couple of ways you could approach this, but I'd probably use the Find.Execute method with ReplaceAll and replace the string "^p" with a zero-length string "".

Edit: Note that you want to make sure the last paragraph mark (end of the last paragraph) is not included in the Range/Selection you execute Find on.






Re: Visual Studio Tools for Office Howto: Word 2003 AddIn: Join selected paragraphs into one

Norm Estabrook - MSFT

Hello Ilya,

Try the following code (I created this for a word document-level customization, therefore, you would need to modify it slightly if you are using an application-level add-in for word).

private void ThisDocument_Startup(object sender, System.EventArgs e)
{

Word.Range rng = this.Paragraphs[1].Range;
rng.End = this.Paragraphs[2].Range.End;

object replaceAll = Word.WdReplace.wdReplaceAll;

rng.Find.ClearFormatting();
rng.Find.Text = "\r";

rng.Find.Replacement.ClearFormatting();
rng.Find.Replacement.Text = " ";

rng.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing);

}

Create a range that includes the parapgraphs that you want to merge, then just replace the "\r" character with " ". You can find more information about how to search for text and work with text ranges at the topic "Working with Text in Documents" at http://msdn2.microsoft.com/en-us/library/bb386277(VS.90).aspx. I create this particular sample by using a combination of ideas found in "How to: Search and Replace Text in Documents" (http://msdn2.microsoft.com/en-us/library/f65x8z3d(VS.90).aspx) and "How to: Retrieve Start and End Characters in Ranges" (http://msdn2.microsoft.com/en-us/library/xa46twee(VS.90).aspx).

Please let me know if this helps.

Norm E.






Re: Visual Studio Tools for Office Howto: Word 2003 AddIn: Join selected paragraphs into one

Ilya Cherkasov

Cindy, thanks for answer!

Norm, yep, you are right. It works.

Here's the code:

[CommandHandler(CommandNames.FormatCommandJoinParagraphs)]

public void JoinParagraphs(object caller, object target)

{

DocumentWrapper currentDocument = _actCache.Current;

Paragraphs paraphs = currentDocument.Document.Application.Selection.Paragraphs;

if ( paraphs.Count <= 1 )

return;

Microsoft.Office.Interop.Word.Range range = paraphs[1].Range;

range.End = paraphs[paraphs.Count].Range.End;

object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

range.Find.ClearFormatting();

range.Find.Text = "\r";

range.Find.Replacement.ClearFormatting();

range.Find.Replacement.Text = " ";

range.Find.Execute(

ref _null, ref _null, ref _null, ref _null, ref _null,

ref _null, ref _null, ref _null, ref _null, ref _null,

ref replaceAll, ref _null, ref _null, ref _null, ref _null);

object currentStyle = range.get_Style();

range.set_Style(ref currentStyle);

}

Thanks!

I love you guysStick out tongue

Best regards,

Ilya Cherkasov