Question MS Word Late Bindings

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
Hi there

I am attempting to rework some MS Word 2000 code to work with Late Bindings, but adding a document to word.documents is giving me some grief. The only examples I can find involve opening an existing word document...

Here is the existing code that works with Word until you hit 2007, then they changed the whole label template thing

VB.NET:
Public Function MailMergeLabels_Customers(ByVal DT As DataTable) As Boolean
        Dim vApp As Word.Application = Nothing
        Dim vDoc As Word.Document = Nothing
        Try
            'Start a new Document
            vApp = CreateObject("Word.Application")
            vDoc = vApp.Documents.Add
            Dim vVersion As Integer = vApp.Version
            Dim Path As String = My.Application.Info.DirectoryPath
            If Not IO.Directory.Exists(Path & "\Temp_Data") Then
                IO.Directory.CreateDirectory(Path & "\Temp_Data")
            End If
            If IO.File.Exists(Path & "\Temp_Data\MailMerge.txt") Then
                IO.File.Delete(Path & "\Temp_Data\MailMerge.txt")
            End If

            Dim FullPath As String = Path & "\Temp_Data\MailMerge.txt"
            If CSV_From_DataTable(DT, FullPath) = False Then
                Return False
            End If

            With vDoc.MailMerge
                With .Fields
                    .Add(vApp.Selection.Range, "Name")
                    vApp.Selection.TypeParagraph()
                    .Add(vApp.Selection.Range, "Address")
                    vApp.Selection.TypeParagraph()
                    .Add(vApp.Selection.Range, "CityStateZip")
                End With
                Dim vAutoText As Word.AutoTextEntry = vApp.NormalTemplate.AutoTextEntries.Add("Label_Layout", vDoc.Content)
                vDoc.Content.Delete()
                .MainDocumentType = Word.WdMailMergeMainDocType.wdMailingLabels
                .OpenDataSource(Name:=FullPath)
                'Note default label is 5160
                vApp.MailingLabel.CreateNewDocument(Name:=PrinterLabel, Address:="", AutoText:="Label_Layout")
                .Destination = Word.WdMailMergeDestination.wdSendToNewDocument
                .Execute()
                vAutoText.Delete()

            End With
            vDoc.Saved = True
            vDoc.Close()
            vApp.Visible = True
            vApp.NormalTemplate.Saved = True

            Return True

        Catch ex As Exception
            EmailError(ex)
            Return False
        End Try


    End Function


I can get part of the Late Bindings code to work..

VB.NET:
 Dim vWord As Object = Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application"))
            vWord.GetType().InvokeMember("Visible", BindingFlags.SetProperty, Nothing, vWord, New Object() {False})
            Dim vDocs As Object = vWord.GetType().InvokeMember("Documents", BindingFlags.GetProperty, Nothing, vWord, Nothing)

But run out of steam when I come to add a document - and I have tried numerous methods.... Sigh

Any useful pointers would be gratefully accepted

Thanks
 

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
Not the way I had intended to go, but it works nonetheless

All versions of Word up to 2007 are processed via the Word 9.0 Object library...

All versions of Word from 2007 on are processed via Interop.Word version 14. (I gather if 'Embed Interop Types' is set to True then all versions of Word will run)

The main difference being the earlier version of

VB.NET:
                'Note default label is 5160
                vApp.MailingLabel.CreateNewDocument(Name:=PrinterLabel, Address:="", AutoText:="Label_Layout")

is now run as

VB.NET:
                  'Note default label is 5160 (ID = 1359804671)
                vApp.MailingLabel.CreateNewDocumentByID(LabelID:="1359804671", Address:="", AutoText:="Label_Layout")

And so far labels are now formatting correctly in Word 2000 and 2007 (I would assume that 2012 will work as well)
 
Top Bottom