We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Insert Text at a Specific Location

I want to locate a series of characters in a document (e.g. <>), and then start inserting specific paragraphs at that point (replacing the existing text). I have no trouble opening the document and using the Find method to locate the text, but I can't see how to start inserting and formatting my new paragraphs at that location.

I would greatly appreciate a few lines of VB.Net code illustrating how to do this.



8 Replies

YG Yavanaarasi G Syncfusion Team May 23, 2008 12:43 PM UTC

Hi David,

You can insert the text between the tags and replacing the existing text.

Please refer the below code:

[VB]
' Open an existing avery template
Dim doc As WordDocument = New WordDocument("..\..\document1.doc")
Dim docMaster As WordDocument = New WordDocument("..\..\Essential DocIO.doc")
Dim SelBegin As TextSelection = doc.Find("BEGIN ", True, True)
OutputText("Found block start...")
Dim SelEnd As TextSelection = doc.Find("END", True, True)
OutputText("Found block end...")
Dim paraStart As WParagraph = SelBegin.GetAsOneRange().OwnerParagraph
SelBegin.GetAsOneRange().Text = SelBegin.GetAsOneRange().Text.Replace(SelBegin.GetAsOneRange().Text, String.Empty)
Dim paraEnd As WParagraph = SelEnd.GetAsOneRange().OwnerParagraph
SelEnd.GetAsOneRange().Text = SelEnd.GetAsOneRange().Text.Remove(0)
OutputText("Removing start and end text...")
Dim BodyPart As TextBodyPart = New TextBodyPart(doc)
Dim BodySel As TextBodySelection = New TextBodySelection(paraStart.Items(0), paraEnd.Items(paraStart.Items.Count - 1))
BodyPart.Copy(BodySel)
OutputText("Copying block...")
BodyPart.PasteAfter(docMaster.Sections(0).HeadersFooters.Header.Paragraphs(0))
OutputText("Pasting block in Master document...")
' Save the document to disk.
docMaster.Save("..\..\labelspartial.doc")
OutputText("Saving the Master document...")


Please let me know if you have any other concerns.

Regards,
G.Yavana



AD Administrator Syncfusion Team May 26, 2008 03:08 AM UTC

This seems a very complex piece of code to do what I thought would be very simple! In any case I am now trying to locate a placeholder (<>) and replace it with a merge field. From your example I have come up with this:

' Create an empty work space document with 1 section and 1 paragraph
Dim mTempDoc As WordDocument = New WordDocument()
Dim p As IWParagraph = mTempDoc.AddSection.AddParagraph
' Add the new merge field
p.AppendField("ClientPhone", FieldType.FieldMergeField)
' Create a new TextBodyPart and add my merge field to the BodyItems
Dim tbp As New TextBodyPart(mTempDoc)
tbp.BodyItems.Add(p)
'Open the source template file and replace the place holder
Dim sourceDoc As WordDocument = New WordDocument(templateName)
sourceDoc.Replace("<>", tbp, False, True)

This replaces the text with the merge field, but it does NOT preserve the formatting of the existing place holder, and sometimes resets the formatting of the whole paragraph.

Is this the best approach, and how can it be modified so that the merge field retains the formatting of the text it replaces?





AD Administrator Syncfusion Team May 26, 2008 03:12 AM UTC

Note: your forum seems to be removing some of the text I typed. This is the same as the previous post but will hopefully contain everything I typed:

This seems a very complex piece of code to do what I thought would be very simple! In any case I am now trying to locate a placeholder (~ClientPhone~) and replace it with a merge field. From your example I have come up with this:

' Create an empty work space document with 1 section and 1 paragraph
Dim mTempDoc As WordDocument = New WordDocument()
Dim p As IWParagraph = mTempDoc.AddSection.AddParagraph
' Add the new merge field
p.AppendField("ClientPhone", FieldType.FieldMergeField)
' Create a new TextBodyPart and add my merge field to the BodyItems
Dim tbp As New TextBodyPart(mTempDoc)
tbp.BodyItems.Add(p)
'Open the source template file and replace the place holder
Dim sourceDoc As WordDocument = New WordDocument(templateName)
sourceDoc.Replace("~ClientPhone~", tbp, False, True)

This replaces the text with the merge field, but it does NOT preserve the formatting of the existing place holder, and sometimes resets the formatting of the whole paragraph.

Is this the best approach, and how can it be modified so that the merge field retains the formatting of the text it replaces?




BP Bhuvaneswari P Syncfusion Team May 26, 2008 01:09 PM UTC

Hi David,

I am able to reproduce the issue. We suspect the issue - Does not preserve the formatting of the existing place holder while replacing
to be a defect and we have forwarded this to our
development team for more analysis. We will update you on 29 th May with more details regarding this issue.

Thank you for your interest in Syncfusion Essential Studio.

Best Regards,
Bhuvana



AD Administrator Syncfusion Team June 6, 2008 04:24 AM UTC

Has there been any update on this issue yet?



BP Bhuvaneswari P Syncfusion Team June 9, 2008 05:53 AM UTC

Hi David,

Sorry for the delay in getting back to you.

After analysis the issue we found that the issue of preserve the formatting of the existing place holder while replacing has been confirmed as a feature request. Because textbody part is a collection of items which have its own formatting. Our model inserts bodyparts as they are .This feature will be available after our next release of Volume3. We will get back to you once this feature is implemented.

Best Regards,
Bhuvana




AD Administrator Syncfusion Team October 24, 2008 12:12 AM UTC

I just downloaded the latest release (6.4.0.15) and this problem is still occurring. Can you give me an update on when a fix will be available?



AD Administrator Syncfusion Team October 24, 2008 12:41 AM UTC

Please ignore that last post - I see you have fixed it by adding the "saveFormatting" parameter to the Replace function.

Works perfectly - thanks very much.


Loader.
Live Chat Icon For mobile
Up arrow icon