Copy a merge from one document document to another in VB.net

I am editing a vb.net application for an assistant, and part of the requirements is to copy the merge fields from one document document to another. I can copy them using document.content.text, but they come out as text (should have understood what I think).

I think they were chosen:

Dim tDocFields As Microsoft.Office.Interop.Word.Fields
tDocFields = tDocument.Content.Fields

Then I activate the document that I want to copy, and I think that I need to copy to this document using the linked word.

vDocument.Activate()
vWord.Selection. ??? Insert() ???

Any pointers would be greatly appreciated ... Am I on the right lines even?

+3
source share
1 answer

Fields, . , . :

Dim lo_field As Field
Dim lo_range As Range
Dim lo_fieldText As String

For Each lo_field In mo_doc.Fields
    If lo_field.Type = WdFieldType.wdFieldMergeField Then

        lo_range = lo_field.Code()

        lo_fieldText = lo_range.Text
        MsgBox(lo_fieldText)

    End If
Next

, :

Imports Microsoft.Office.Interop.Word

Public Class Form1

    Dim mo_doc As Document
    Dim mo_missing As Object = System.Reflection.Missing.Value

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ' Open word
        Dim lo_word As New Application
        lo_word.Visible = True

        ' Create a new word document
        mo_doc = lo_word.Documents.Add(mo_missing, mo_missing, mo_missing, mo_missing)
        mo_doc.Activate()

        ' Add a merge field
        mo_doc.Fields.Add(lo_word.Selection.Range, WdFieldType.wdFieldMergeField, "mergefieldname", True)

    End Sub
End Class

, !

0

Source: https://habr.com/ru/post/1715716/


All Articles