Access-o-Mania

Office-Forum (Deutsch/German) => Microsoft Word => Thema gestartet von: Mik3 am Februar 20, 2007, 08:44:16

Titel: excel erstellen -> befüllen
Beitrag von: Mik3 am Februar 20, 2007, 08:44:16
Hi ihr :)

Wie kann ich aus Word heraus eine Excelldatei erstellen , und daten in diese Einfügen?

danke für eure Tips/Beispiele :)

Mike
Titel: Re: excel erstellen -> befüllen
Beitrag von: Sebi am Februar 20, 2007, 13:51:05
Hallo,

also Excel startest du per VBA

Private Sub CommandExcel()
Shell "excel.exe", vbNormalFocus
End Sub


lg Turbo
Titel: Re: excel erstellen -> befüllen
Beitrag von: Mik3 am Februar 21, 2007, 08:47:15
cool, danke
Titel: Re: excel erstellen -> befüllen
Beitrag von: Mik3 am Februar 22, 2007, 08:27:13
hoi,

ich habe das auch mal mit nem Explorer gemacht..


Shell "explorer.exe", vbMaximizedFocus


will jetzt aber den Ordner , der Angezeigt wird auf C:\Programme\Test\  setzen, wie geht das denn?

thx, mike
Titel: Re: excel erstellen -> befüllen
Beitrag von: atropos am Februar 22, 2007, 08:55:35

Moin moin,

ganz einfach:

Shell "explorer.exe C:\Programme\Test\", vbMaximizedFocus
Titel: Re: excel erstellen -> befüllen
Beitrag von: Sebi am Februar 22, 2007, 08:56:56
Hallo,

was magst du denn genau machen?

Willst du einen Dateiöffnen Dialog erzeugen?

Wenn ja geht das auch schöner.



lg Turbo
Titel: Re: excel erstellen -> befüllen
Beitrag von: imp666 am März 18, 2011, 12:46:26
Du kannst Excel komplett automatisieren aus Access VBA heraus, hier nur mal ein Beispiel. Ich vermute das müsste auch in Word funktionieren....

   Dim objExcel As Object
   Dim objExcelWorkbook As Object
   Dim objExcelSheet As Object
   Dim strWorkspace As String
   Set objExcel = CreateObject("Excel.Application")
   strWorkspace = "c:\pfad\dateiname.xls"
   With objExcel
       .Visible = True
       .Interactive = True
       .Application.ScreenUpdating = False
       Set objExcelWorkbook = .Workbooks.Add
       Set objExcelSheet = objExcelWorkbook.sheets.Add
       objExcelSheet.Name = "Name des Excel-Sheets"
       objExcelSheet.Cells(1, 1).Value = "Irgendeine Überschrift"
       objExcelSheet.Range("A1:H1").MergeCells = True

       objExcelSheet.Cells(1, 1).interior.colorindex = 40
       objExcelSheet.Cells(1, 1).Font.Bold = True
       objExcelSheet.Cells(1, 1).Font.Name = "Arial"
       objExcelSheet.Cells(1, 1).Font.colorindex = 3
       objExcelSheet.Cells(1, 1).HorizontalAlignment = 3 ' entspr. "center"

       objExcelSheet.Cells(3, 1).Value = "Jahr"
       objExcelSheet.Cells(3, 2).Value = "Veranst.Nr."
       objExcelSheet.Range("A3:H3").interior.colorindex = 35
       objExcelSheet.Range("A3:H3").Font.Bold = True
       objExcelSheet.Cells.Columns(1).ColumnWidth = 7
       objExcelSheet.Cells.Columns(2).ColumnWidth = 10
       objExcelSheet.Range("A3:H3").borders(4).linestyle = 1 ' 1=solid / 2=dashed
       objExcelSheet.Range("A3:H3").borders(4).Width = 1
       objExcelSheet.Range("A3:H3").borders(4).colorindex = 1
       .Application.ScreenUpdating = True
   End With

Titel: Re: excel erstellen -> befüllen
Beitrag von: Hondo am März 18, 2011, 13:33:45
Hallo,
Excel-Automatisation ist der einzige richtige Weg, am besten noch per Late Binding, also ohne zusätzlichen Verweiss so wie von imp666 beschrieben.
Was aber noch fehlt ist eine Überprüfung ob es bereits eine existierende Excel-Instanz gibt. Dies macht man per API:


    Dim objExcel As Object
    Dim boolXL AS Boolean

    If fIsExcelRunning Then
        Set objExcel = GetObject(, "Excel.Application")
        boolXL = False
    Else
        Set objExcel = CreateObject("Excel.Application")
        boolXL = True
    End If

    With objXL
    ....
    End With

    If boolXL Then objExcel.Application.Quit



Dieser Code in ein Modul packen:

Private Const SW_SHOWNORMAL = 1

Private Declare Function apiFindWindow Lib "user32" Alias _
                                       "FindWindowA" (ByVal strClass As String, _
                                                      ByVal lpWindow As String) As Long

Private Declare Function apiSendMessage Lib "user32" Alias _
                                        "SendMessageA" (ByVal Hwnd As Long, ByVal Msg As Long, ByVal _
                                                                                               wParam As Long, lParam As Long) As Long

Private Declare Function apiSetForegroundWindow Lib "user32" Alias _
                                                "SetForegroundWindow" (ByVal Hwnd As Long) As Long

Private Declare Function apiShowWindow Lib "user32" Alias _
                                       "ShowWindow" (ByVal Hwnd As Long, ByVal nCmdShow As Long) As Long

Private Declare Function apiIsIconic Lib "user32" Alias _
                                     "IsIconic" (ByVal Hwnd As Long) As Long

Public Function fIsExcelRunning(Optional fActivate As Boolean) As Boolean
    Dim lngH As Long,  lngX As Long, lngTmp As Long
    Const WM_USER = 1024
    On Local Error GoTo fIsAppRunning_Err
    fIsAppRunning = False
    lngH = apiFindWindow("XLMain", vbNullString)
    If lngH <> 0 Then
        apiSendMessage lngH, WM_USER + 18, 0, 0
        lngX = apiIsIconic(lngH)
        If lngX <> 0 Then
            lngTmp = apiShowWindow(lngH, SW_SHOWNORMAL)
        End If
        If fActivate Then
            lngTmp = apiSetForegroundWindow(lngH)
        End If
        fIsAppRunning = True
    End If
fIsAppRunning_Exit:
    Exit Function
fIsAppRunning_Err:
    fIsAppRunning = False
    Resume fIsAppRunning_Exit
End Function


Was imp666 noch vergessen hat ist die Objekte zurückzusetzen am Schluss
set objExcel = Nothing
set ... usw.

Gruß Andreas
Titel: Re: excel erstellen -> befüllen
Beitrag von: database am März 19, 2011, 15:13:33
Hallo,

wobei dann die Frage noch aus dem Jahr 2007 stammt  ;)
Titel: Re: excel erstellen -> befüllen
Beitrag von: Hondo am März 19, 2011, 18:16:43
Äh, das ist jetzt aber peinlich.
Hab ich auch nicht drauf geachtet.
Aber vieleicht hilft es ja jemand anderem mit ähnlichem Problem.

Andreas
Titel: Re: excel erstellen -> befüllen
Beitrag von: database am März 19, 2011, 19:07:07
Hallo,

....es gibt Schlimmeres!  :D ;D ;)