Devo utilizzare Word per stampare in StampaUnione una serie di documenti con una fonte dati in Excel.
La funzione devo integrarla in un programma xHarbour+Fivewin.
Non riesco a collegare il foglio di excel come database per la StampaUnione.
Questo è il codice:
Code: Select all
STATIC FUNCTION PrnDocMM( nRek, cFileDoc, cDataSource, cTable, nDoc )
LOCAL oWord, oFile, nReturn := 0
// Connessione Word
TRY
oWord := CREATEOBJECT( "Word.Application" )
CATCH
nReturn := 1
END
IF nReturn == 0
// Apertura File
TRY
oFile := oWord:Documents:Open( cFileDoc )
CATCH
nReturn := 2
END
IF nReturn == 0
// Collegamento DataSource
// ? cDataSource, IF( File( cDataSource ), "Esiste", "Non Esiste" )
TRY
// <MailMergeObject>.OpenDataSource Name:="C:\MyDB.mdb", _
// SQLStatement:="SELECT * FROM [MyTable]"
// WITH OBJECT oFile:MailMerge:OpenDataSource
// :Name = cDataSource
// // :Connection = "Cartella Stampa Contratti"
// END // WITH
// ? cDataSource, cTable
oFile:MailMerge:OpenDataSource( cDataSource, , .T., , , , , , , , cTable )
CATCH
nReturn := 3
END
IF nReturn == 0
// seleziono il record e stampo
TRY
WITH OBJECT oFile:MailMerge
:DataSource:FirstRecord = nRek
:DataSource:LastRecord = nRek
:Destination = wdSendToPrinter
// :Destination = wdSendToNewDocument
:SuppressBlankLines = True
:Execute()
END // With
// oWord:ActiveDocument:SaveAs2( ".\MyDoc" + PadL( nDoc, 3, "0" ) + ".doc", 0 ) // 0 = .doc; 16 = default=>docx; 17 = pdf
// oFile:SaveAs2( "s:\Consorzi\Stampaunione\MyDoc" + PadL( nDoc, 3, "0" ) + "", 0 ) // 0 = .doc; 16 = default=>docx; 17 = pdf
// oFile:SaveAs2( ".\MyDoc" + PadL( nDoc, 3, "0" ) + "", 0 ) // 0 = .doc; 16 = default=>docx; 17 = pdf
// ? "Stampato"
CATCH
nReturn := 4
END
ENDIF
oFile:Close()
ENDIF
oWord:QUIT()
ENDIF
Return nReturn