Avatar billede dylan Nybegynder
09. maj 2004 - 10:30 Der er 8 kommentarer og
1 løsning

Data fra Exel til Word istedet for Acces til Word

Jeg har et program i vba der trækker data fra Acces til Word.
Kan jeg lige så snildt trække data fra exel ved at ændre i programmet. Det er de samme data, men de lægger bare i et regneark.
Jeg prøver at vedlægge prog.
Sub Indtast()
Kundenr = InputBox("indtast kundenr")
    Dim objConn As ADODB.Connection
    Dim objRs As ADODB.Recordset
    Dim strConnString As String
    Dim strSQL As String
   
    Set objConn = New ADODB.Connection
    Set objRs = New ADODB.Recordset
   
    strConnString = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\dokumenter\Kartoteker.MDb"
    objConn.Open strConnString
   
    strSQL = "SELECT Kundenr,Navn, Adresse, postby FROM kundekartotek WHERE kundenr = " & Kundenr
    objRs.Open strSQL, objConn
           
    If Not objRs.EOF Then
       
        Selection.TypeText Text:="Kundenr: " & objRs("Kundenr") & vbCrLf
        Selection.TypeText Text:="" & objRs("Navn") & vbCrLf
        Selection.TypeText Text:="" & objRs("Adresse") & vbCrLf
      Selection.TypeText Text:="" & objRs("postby") & vbCrLf
 
objRs.Close
objConn.Close
 
End If
End Sub
Avatar billede terry Ekspert
09. maj 2004 - 10:41 #1
I dont know much about Excel VBA, but you could easily export the data from Access TO Excel using Access's export wizard. You can also do it in code from Access
http://www.mvps.org/access/modules/mdl0006.htm
Avatar billede terry Ekspert
09. maj 2004 - 10:44 #2
Ah! You want to take the data FROM Excel into Word? I'm not sure I can help there!
Avatar billede dylan Nybegynder
09. maj 2004 - 10:53 #3
ja data skal trækkes fra exel stedet for i acces
Avatar billede terry Ekspert
09. maj 2004 - 10:55 #4
I'll see if I can find something which may help you
Avatar billede dylan Nybegynder
09. maj 2004 - 11:03 #5
tak
Avatar billede terry Ekspert
09. maj 2004 - 11:19 #6
You can try this, may help you get on the right track

Dim objXL As Object 'Excel.Application
Dim objActiveWkb As Object 'Excel.Workbook
Dim iCol As Integer, iRow As Integer

    On Error Resume Next
   
    Set objXL = CreateObject("Excel.Application")
    With objXL.Application
        .Visible = True
        'Open the Workbook
        .Workbooks.Open "C:\test.xls"
       
        Set objActiveWkb = .Application.ActiveWorkbook
       
        With objActiveWkb
            For iRow = 1 To 3
                For iCol = 1 To 3
                    Selection.TypeText Text:=  .Worksheets("Sheet1").Cells(iRow, iCol)
       
                Next iCol
            Next iRow
        End With
       
    End With
   
    objXL.Application.Quit
   
    Set objXL = Nothing
    Set objActiveWkb = Nothing
Avatar billede terry Ekspert
09. maj 2004 - 11:21 #7
Hope you can see what is happening.

If you change these two lines
Dim objXL As Object 'Excel.Application
Dim objActiveWkb As Object 'Excel.Workbook

to this and then (if possible) make a reference to Excel in your Word VBA project. Then you should have access to VBA help to Excel where you can see some examples.

Dim objXL As Excel.Application
Dim objActiveWkb As Excel.Workbook
Avatar billede dylan Nybegynder
09. maj 2004 - 11:39 #8
tak...jeg prøver!
Avatar billede terry Ekspert
09. maj 2004 - 11:42 #9
thanks, hope you can make use of the example!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester