Ok!
codice:
Private Sub CreaTabella(ByVal p_str_sql As String, _
ByVal p_str_stringaCampiForm As String, _
ByVal p_str_stringaContenutoCampiForm As String, _
ByVal p_nomeFileTabella As String)
Try
oWord = New Word.Application()
Dim docWord As Word.Document = oWord.Application.Documents.Add()
Dim wSelection = oWord.Selection 'questo è l'oggetto selezione
Dim obj_mailMerge As Word.MailMerge
Dim obj_MergeFields As Word.MailMergeFields
Dim arr_campiForm As String()
arr_campiForm = Split(p_str_stringaCampiForm, ";", , CompareMethod.Text)
Dim arr_ContenutoCampiForm As String()
arr_ContenutoCampiForm = Split(p_str_stringaContenutoCampiForm, ";", ,
CompareMethod.Text)
Sql = p_str_sql
ApriConnessione()
ApriDataBase(Sql, Dr)
Dim wdCell As Word.Cells
'Colonna + 1 per Today
Dim int_numeroColonne As Integer = CInt(Dr.FieldCount + arr_campiForm.Length +
1)
' Riga + 1 per intestazione
oWord.ActiveDocument.Tables.Add(wSelection.Range, 1, int_numeroColonne)
Dim contatore As Integer
' ___ INTESTAZIONE (NOMI RECORD USATI DA WORD)
' Inserisco nell'intestazione i nomi dei campi (nomi dei record di word) +
Campi form + Today
' Campi Db
For contatore = 0 To Dr.FieldCount - 1
Dim campoDb As String = Dr.GetName(contatore).ToString
campoDb = Replace(campoDb, ".", "")
campoDb = Replace(campoDb, "[", "")
campoDb = Replace(campoDb, "]", "")
wSelection.TypeText(Text:=campoDb)
oWord.Selection.MoveRight()
Next
'Campi Form
For contatore = 0 To arr_campiForm.Length - 1
wSelection.TypeText(Text:=arr_campiForm(contatore))
oWord.Selection.MoveRight()
Next
' Today
wSelection.TypeText(Text:="Oggi")
oWord.Selection.MoveRight()
' _________________________________________________________
int_record = 0
While Dr.Read
Dim str_nomeCampo As String
oWord.Selection.Rows.Add()
For contatore = 0 To Dr.FieldCount - 1
wSelection.TypeText(Text:=Dr(contatore))
oWord.Selection.MoveRight()
Next
For contatore = 0 To arr_campiForm.Length - 1
wSelection.TypeText(Text:=CStr(arr_ContenutoCampiForm(contatore)))
oWord.Selection.MoveRight()
Next
wSelection.TypeText(Text:=CStr(Today))
oWord.Selection.MoveRight()
int_record += 1
End While
If int_record <= 0 Then
Exit Sub
End If
Dr.Close()
ChiudiDataBase(Dr)
oWord.ActiveDocument.SaveAs(Environment.CurrentDirectory & "\Modelli\" &
p_nomeFileTabella & ".doc")
oWord.ActiveWindow.Close()
wSelection = Nothing
oWord = Nothing
docWord = Nothing
Catch ex As Exception
MessageBox.Show("Operazione annullata", "", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try
End Sub
' ----------------------------------------------------------------------------------------
Private Sub StampaUnione(ByVal p_fileDati As String, ByVal p_modello As String)
If int_record = 0 Then
MessageBox.Show("Nessun risultato trovato.", "",MessageBoxButtons.OK,
MessageBoxIcon.Information)
Exit Sub
End If
Try
' Dichiaro l'oggeto oWord
oWord = New Word.Application()
oWord.Documents.Open(p_modello)
oWord.Visible = True
oWord.ActiveDocument.MailMerge.MainDocumentType =
WdMailMergeMainDocType.wdFormLetters
oWord.ActiveDocument.MailMerge.OpenDataSource(p_fileDati)
With oWord.ActiveDocument.MailMerge
.Destination = WdMailMergeDestination.wdSendToPrinter
.MailAsAttachment = False
.MailAddressFieldName = ""
.MailSubject = ""
.SuppressBlankLines = True
.Execute(Pause:=True)
End With
oWord.Documents.Close(WdSaveOptions.wdDoNotSaveChanges)
oWord = Nothing
Catch ex As Exception
MessageBox.Show("Operazione annullata", "", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Finally
oWord = Nothing
End Try
'Questo invece usato da Access a Word anzichè Word Word ma non l'ho usato!
'Dim objWord As Word.Document
'objWord = GetObject(percorso & p_modello)
'' Make Word visible.
'objWord.Application.Visible = False
'objWord.MailMerge.OpenDataSource( _
' Name:=percorso, _
' LinkToSource:=True, _
' Connection:="TABLE STAMPAUNIONE", _
' SQLStatement:="SELECT * FROM STAMPAUNIONE")
'objWord.MailMerge.Destination = WdMailMergeDestination.wdSendToNewDocument
'objWord.MailMerge.Execute()
'objWord.Application.Options.PrintBackground = False
'objWord.Application.ActiveDocument.PrintOut()
End Sub ' Tutte le stampe usano queste sub. E' il motore delle stampe