Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.
Thu Dec 28, 2017 4:43 pm
hello
i am beginner and i try transform at field.mergefield and fieldif to bookmarks for a lot of document.
but i dont find how to remove one field in the collection document.fieds in vb
Dim chIF As Fields.IfField
Dim chMM As Fields.MergeField
For Each ch As Fields.Field In document.Fields
Try
Select Case ch.Type
Case FieldType.FieldMergeField
chMM = CType(ch, Fields.MergeField)
' create bookmark with the name of mailmerge fiel and text in bookmark "namemailmerge
## help needed## ' delete field Mailmerge
Case FieldType.FieldIf
chIF = CType(ch, Fields.IfField)
## help needed## delete fieldif maybe chIF remove Or doccument.fields.remove at .....????
End Select
'tempo.Add(ch.Code)
'tempo.Clear()
Catch ex As Exception
MessageBox.Show("erreur de traitement de champs mail merge -> " & ch.ToString())
End Try
Nextthank's for your help
-
labaffagaston
-
- Posts: 42
- Joined: Sun Dec 24, 2017 7:38 pm
Fri Dec 29, 2017 6:21 am
Hello,
Thanks for your inquiry.
Please refer to the below code to accomplish your task.
- Code: Select all
Dim document As New Document()
document.LoadFromFile(word_file_open_dialog.FileName)
For Each section As Section In document.Sections
For Each dobj As DocumentObject In section.Body.ChildObjects
If TypeOf dobj Is Paragraph Then
Dim objs As DocumentObjectCollection = (TryCast(dobj, Paragraph)).ChildObjects
Dim i As Integer = 0
Dim nav As New BookmarksNavigator(document)
Do While i < objs.Count
If TypeOf objs(i) Is Field Then
Dim field As Field = TryCast((TryCast(dobj, Paragraph)).ChildObjects(0), Field)
If field IsNot Nothing AndAlso field.Type = FieldType.FieldIf Then
'remove filed and insert bookmark
'you can name the bookmark according to your need, here I take the "IFField" as an example
Dim bkstart As New BookmarkStart(document, "IFField")
Dim bkend As New BookmarkEnd(document, "IFField")
objs.RemoveAt(i)
objs.Insert(i, bkstart)
objs.Insert(i + 1, bkend)
'insert content into the bookmark
nav.MoveToBookmark("IFField")
nav.InsertText(field.Code)
End If
If field IsNot Nothing AndAlso field.Type = FieldType.FieldMergeField Then
Dim bkstart As New BookmarkStart(document, "MergeField")
Dim bkend As New BookmarkEnd(document, "MergeField")
objs.RemoveAt(i)
objs.Insert(i, bkstart)
objs.Insert(i + 1, bkend)
'insert content into the bookmark
nav.MoveToBookmark("MergeField")
nav.InsertText(field.Code)
End If
End If
i += 1
Loop
End If
Next dobj
Next section
document.SaveToFile("fieldresult.docx")
If the code does not work on your side, please send your sample document to us(
support@e-iceblue.com) and then I will provide the solution accordingly.
Sincerely,
Jane
E-iceblue support team
-
Jane.Bai
-
- Posts: 1156
- Joined: Tue Nov 29, 2016 1:47 am
Sat Dec 30, 2017 2:35 pm
thank you for your answer
i wil try it
fred
-
labaffagaston
-
- Posts: 42
- Joined: Sun Dec 24, 2017 7:38 pm
Thu Jan 04, 2018 10:00 am
Hello,
Greetings from e-iceblue!
Have you tried the code? Looking forward to your reply.
Sincerely,
Jane
E-iceblue support team
-
Jane.Bai
-
- Posts: 1156
- Joined: Tue Nov 29, 2016 1:47 am