none
Cannot remove mail merge data source from docx template - (VB.NET) RRS feed

  • Question

  • Hi,

    I am trying to programmatically remove the .odc data source from .docx templates with OpenXML in VB.net

    The templates can't connect to the data source because the SQL Server has been shut down (not needed any more), although I still need the templates with the merge fields, just not connected to a .odc data source.

    I have found this thread (the only semi-helpful information on the internet it seems) on how to do it in C#.NET:
    http://social.msdn.microsoft.com/Forums/en-HK/oxmlsdk/thread/8b363660-cc7f-4b7d-addf-30df57a0ecd6

    The problem is that this does not work when converting to VB.NET for many reasons.  (try it and you'll see why).

    Can someone please help me, i have wasted days at work trying to do this for .docx files.  If you can post the solution from where you open the word document (or package), to saving and closing it (or flushing and closing), that would be amazing, i feel like i've tried everything!

    Thankyou.

    • Edited by nickmccomb Monday, December 5, 2011 10:58 PM update
    Monday, December 5, 2011 5:00 AM

Answers

  • I tried a really quick conversion of the code by C Gonnet in the thread you reference, and ended up with something like the following (a lot of the constructs are unfamiliar to me right now so I'm guessing, really)

    I've only tried it on a single file but it looks like a viable starting point to me.

     

    Imports System

    Imports System.Linq

    Imports DocumentFormat.OpenXml

    Imports DocumentFormat.OpenXml.Packaging

    Imports DocumentFormat.OpenXml.Wordprocessing

     

    Module Module1

        Sub Main()

            ' Delete MailMerge Data Source Part

            Dim wordDocument As WordprocessingDocument = Packaging.WordprocessingDocument.Open("c:\\a\\mmmd.docx", True)

            If wordDocument.MainDocumentPart.GetPartsOfType(Of DocumentSettingsPart).ToList.Count > 0 Then

                Dim settingsPart As DocumentSettingsPart = wordDocument.MainDocumentPart.GetPartsOfType(Of DocumentSettingsPart).First

                Try

                    If settingspart.GetPartsOfType(Of MailMergeRecipientDataPart).ToList.Count > 0 Then

                        Dim mmrPart As MailMergeRecipientDataPart = settingspart.GetPartsOfType(Of MailMergeRecipientDataPart).First

                        settingspart.DeletePart(mmrPart)

                    End If

                Catch ex As Exception

                    Throw New ArgumentOutOfRangeException(ex.Message)

                End Try

     

                Dim mailMergeEl As OpenXmlElement = settingsPart.Settings.FirstOrDefault(Function(lName) lName.LocalName = "mailMerge")

                If Not (mailMergeEl Is Nothing) Then

                    settingspart.Settings.RemoveChild(mailMergeEl)

                    settingspart.Settings.Save()

                End If

            End If

            wordDocument.Close()

        End Sub

     

    End Module


    Peter Jamieson
    • Marked as answer by nickmccomb Tuesday, December 6, 2011 7:00 AM
    Tuesday, December 6, 2011 1:26 AM

All replies

  • I tried a really quick conversion of the code by C Gonnet in the thread you reference, and ended up with something like the following (a lot of the constructs are unfamiliar to me right now so I'm guessing, really)

    I've only tried it on a single file but it looks like a viable starting point to me.

     

    Imports System

    Imports System.Linq

    Imports DocumentFormat.OpenXml

    Imports DocumentFormat.OpenXml.Packaging

    Imports DocumentFormat.OpenXml.Wordprocessing

     

    Module Module1

        Sub Main()

            ' Delete MailMerge Data Source Part

            Dim wordDocument As WordprocessingDocument = Packaging.WordprocessingDocument.Open("c:\\a\\mmmd.docx", True)

            If wordDocument.MainDocumentPart.GetPartsOfType(Of DocumentSettingsPart).ToList.Count > 0 Then

                Dim settingsPart As DocumentSettingsPart = wordDocument.MainDocumentPart.GetPartsOfType(Of DocumentSettingsPart).First

                Try

                    If settingspart.GetPartsOfType(Of MailMergeRecipientDataPart).ToList.Count > 0 Then

                        Dim mmrPart As MailMergeRecipientDataPart = settingspart.GetPartsOfType(Of MailMergeRecipientDataPart).First

                        settingspart.DeletePart(mmrPart)

                    End If

                Catch ex As Exception

                    Throw New ArgumentOutOfRangeException(ex.Message)

                End Try

     

                Dim mailMergeEl As OpenXmlElement = settingsPart.Settings.FirstOrDefault(Function(lName) lName.LocalName = "mailMerge")

                If Not (mailMergeEl Is Nothing) Then

                    settingspart.Settings.RemoveChild(mailMergeEl)

                    settingspart.Settings.Save()

                End If

            End If

            wordDocument.Close()

        End Sub

     

    End Module


    Peter Jamieson
    • Marked as answer by nickmccomb Tuesday, December 6, 2011 7:00 AM
    Tuesday, December 6, 2011 1:26 AM
  • that is doing the same thing for me as the converted C# code did.
    It says .ToList and .First are not a members of 'System.Collections.Generic.IEnumberable(...)

    Any ideas?

    Tuesday, December 6, 2011 3:16 AM
  • Never mind, i forgot to import system.linq. 

    it's all working, thanks heaps!

    Tuesday, December 6, 2011 7:00 AM
  • i forgot to import system.linq. 

    Glad you got there! (Sorry, it was the middle of the night for me or I'd have suggested it, but that's why I included the Imports :-) )



    Peter Jamieson
    Tuesday, December 6, 2011 9:29 AM