RE:ENLAZAR ACCES CON WORD
Solo necesitas un control panel llamado pnlword, el programa abre word llenas el doc con datos de acces y se guarda en C:\, espero te ayude...........
Private Const SW_MINIMIZE As Integer = 6
Private Const SW_MAXIMIZE As Integer = 3
Private Const SW_RESTORE As Integer = 9
Declare Function SetParent Lib "user32" (ByVal hWndChild As System.IntPtr, ByVal hWndNewParent As System.IntPtr) As System.IntPtr
Declare Function ShowWindow Lib "user32" (ByVal hWnd As System.IntPtr, ByVal nCmdShow As Integer) As Boolean
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Dim wdapp As New Word.Application
Dim wddoc As Word.Document
Dim wdsave As Word.WdSaveOptions
Dim OLEConn As New OleDb.OleDbConnection
Dim OLEDa As New OleDb.OleDbDataAdapter
Dim cmd As New OleDb.OleDbCommand
Dim MiView As DataView = New DataView
Dim DSet As New DataSet
Dim i, x As Int32
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
OLEConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:\Documents and Settings\Laptop\Mis documentos\NORTHwind.MDB"
OLEDa.SelectCommand = cmd
cmd.CommandText = "SELECT IdCategoría,NombreCategoría,Descripción FROM Categorías" 'where productname like 'p*' INNER JOIN Products ON Categories.CategoryID = Products.CategoryID"
cmd.Connection = OLEConn
DSet.DataSetName = "Datan"
DSet.Clear()
OLEDa.Fill(DSet, "Pedidos")
MiView.Table = DSet.Tables("Pedidos")
'OpenWord''''''''''''''''''''''''''''''''''''''
Call OpenWord()
'''''''''Insert data in word'''''''''''''''''''
Application.DoEvents()
pnlWord.Enabled = False ' Deshabilita el panel contenedor
InsertaDatos()
End Sub
Sub InsertaDatos() ' Inserta y da formato texto
wdapp.ActiveWindow.Activate()
With wdapp.Selection
.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
FormatText(.Font.Name, 16, True, False, Word.WdUnderline.wdUnderlineSingle, Word.WdColor.wdColorBlue)
.TypeText("Pedidos de mi BD")
.TypeParagraph()
.TypeParagraph()
.TypeParagraph()
.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
FormatText(.Font.Name, 12, False, False, Word.WdUnderline.wdUnderlineNone, Word.WdColor.wdColorAutomatic)
.TypeText(Date.Today.ToLongDateString)
.TypeParagraph()
.TypeParagraph()
.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
.TypeText(Text:="Creado por: ")
FormatText("Courier new", 13, True, False, Word.WdUnderline.wdUnderlineNone, Word.WdColor.wdColorSeaGreen)
.TypeText("Harold Hensen V.")
FormatText(.Font.Name, 11, True, False, Word.WdUnderline.wdUnderlineNone, Word.WdColor.wdColorDarkBlue)
.TypeParagraph()
.TypeParagraph()
.TypeParagraph()
For i = 0 To DSet.Tables(0).Columns.Count - 1
If i = 0 Then
.TypeText(DSet.Tables(0).Columns.Item(i).ColumnName & Space(1))
Else
.TypeText(DSet.Tables(0).Columns.Item(i).ColumnName & Space(3))
End If
Next
.TypeParagraph()
wdapp.ActiveDocument.Shapes.AddLine(85, 210, 510, 210)
.TypeParagraph()
Dim lenC, lenCa, lenDes As Int32
Dim cod, cat, ndes, des As String
FormatText("Courier new", 11, False, False, Word.WdUnderline.wdUnderlineNone, Word.WdColor.wdColorAutomatic)
'enlaza los datos de access al documento...................
For i = 0 To DSet.Tables(0).Rows.Count - 1
cod = MiView.Item(i).Item(0)
cat = MiView.Item(i).Item(1)
des = MiView.Item(i).Item(2)
lenC = Len(cod)
lenCa = Len(cat)
lenDes = Len(des)
If lenDes < 37 Then
.TypeText(cod & Space(12 - lenC) & cat & Space(18 - lenCa) & des & Chr(13))
Else
ndes = Microsoft.VisualBasic.Left(des, 30) & "..."
.TypeText(cod & Space(12 - lenC) & cat & Space(18 - lenCa) & ndes & Chr(13))
End If
Next
End With
wddoc.SaveAs("C:\AccesData.doc") ' guarda el doc aqui puedes darle varios criterios
' como password,solo lectura,etc............
End Sub
Sub FormatText(ByVal fname As String, ByVal FSize As Integer, ByVal fBold As Boolean, ByVal fitalic As Boolean, ByVal funderline As Word.WdUnderline, ByVal fcolor As Word.WdColor)
With wdapp.Selection.Font
.Name = fname
.Size = FSize
.Bold = fBold
.Italic = fitalic
.Underline = funderline
.UnderlineColor = Word.WdColor.wdColorAutomatic
.Color = fcolor
End With
End Sub
Private Sub frmWordinForm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Call CloseWord()
End Sub
'Subs para encontrar Word y Cerrar aplicacion
Sub OpenWord()
Dim ret As System.IntPtr
wdapp.Visible = True
ret = FindWindow("Opusapp", vbNullString)
If ret.ToInt32 <> 0 Then
SetParent(ret, Me.pnlWord.Handle)
System.Threading.Thread.CurrentThread.Sleep(100)
ShowWindow(ret, SW_MAXIMIZE)
Me.pnlWord.Visible = True
wddoc = wdapp.Documents.Add
End If
End Sub
Sub CloseWord()
wddoc.Close(wdsave.wdDoNotSaveChanges)
wdapp.Quit()
wdapp = Nothing
End Sub
End Class