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
I can get part of the Late Bindings code to work..
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
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