afdruk samenvoegen vanuit macro

Status
Niet open voor verdere reacties.

polderploer

Gebruiker
Lid geworden
13 sep 2007
Berichten
69
Ik heb een gegevensbestand en wil als er op de knop wordt gedrukt een aantal handelingen uitgevoerd worden. Dit gaat allemaal uitstekend. Vervolgens wil ik dat de macro een Word bestand opent en via afdruk samenvoegen de excel gegevens invoert.

Echter als ik via de macro het word bestand open, met de volgende code:

Code:
Set appWD = CreateObject("Word.Application")

With appWD
    .Visible = True
    .Documents.Open Filename:="C:\docs\berekening v2\samenvoegen0.1.doc"
End With

dan opent hij netjes het word bestand, alleen voert hij niet afdruk samenvoegen uit. Sterker, hij vraagt niet eens of hij de SQL opdracht wil uitvoeren.

Als ik het bestand nu handmatig open, dan werkt alles wel, en vraagt ie netjes of de SQL opdracht moet worden uitgevoerd

Hoe krijg ik dit nu goed in de macro??

BVD
 
Inmiddels ben ik al wat verder:

Als ik de onderstaande code via WORD uitvoer werkt het perfect:

Code:
Private Sub CommandButton1_Click()
ChangeFileOpenDirectory "C:\docs\berekening v2\"
    Documents.Open FileName:="samenvoegen0.1.doc", ConfirmConversions:=False, _
        ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
End Sub

Vervolgens zet ik dit onder een knopje in excel en dan werkt het niet!

De error is dan, sub of function niet gedefineerd.
Dus probeer ik een combinatie van beide codes:

Code:
Private Sub CommandButton1_Click()
Set appWD = CreateObject("Word.Application")

With appWD
   ' .Visible = True
    .Documents.Open Filename:="C:\docs\berekening v2\samenvoegen0.1.doc", ConfirmConversions:=False, _
        ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
End With

End Sub

Nu geeft ie de error:

fout 4198 bij uitvoering

opdracht mislukt


Bij help verschijnt er geen info, misschien hebben jullie er wat aan om mij verder te helpen.
 
Laatst bewerkt:
ik heb inmiddels al wat meer info, maar ik kom er echt niet zelf uit. ik toon toch aan dat ik genoeg zelf zoek, of weet echt niemand iets over dit probleem?

Code:
Option Explicit
Option Compare Text
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' modActivateExcel
' By Chip Pearson, www.cpearson.com, chip@cpearson.com
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Window API Declarations
' These Declares MUST appear at the top of the
' code module, above and before any VBA procedures.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function BringWindowToTop Lib "user32" ( _
    ByVal HWnd As Long) As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long

Private Declare Function SetFocus Lib "user32" ( _
    ByVal HWnd As Long) As Long


Public Sub ActivateExcel()

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ActivateExcel
' This procedure activates the main Excel application window,
' ("XLMAIN") moving it to the top of the Z-Order and sets keyboard
' focus to Excel.
' ' !!!!!!!!!!!!!!!!!!!!!!!!!
' NOTE: This will not work properly if a VBA Editor is open.
' If a VBA Editor window is open, the system will set focus
' to that window, rather than the XLMAIN window.
' !!!!!!!!!!!!!!!!!!!!!!!!!
'
' This code should work to activate any application. Change the
' value of C_MAIN_WINDOW_CLASS to the application's main window
' class (e.g., "OpusApp" for Word).
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



Dim Res As Long ' General purpose Result variable
Dim XLHWnd As Long ' Window handle of Excel
Const C_MAIN_WINDOW_CLASS = "XLMAIN"



'''''''''''''''''''''''''''''''''''''''''''
' Get the window handle of the main
' Excel application window ("XLMAIN"). If
' more than one instance of Excel is running,
' you have no control over which
' instance's HWnd will be retrieved.
' Related Note: You MUST use vbNullString
' not an empty string "" in the call to
' FindWindow. When calling API functions
' there is a difference between vbNullString
' and an empty string "".
''''''''''''''''''''''''''''''''''''''''''


XLHWnd = FindWindow(lpClassName:=C_MAIN_WINDOW_CLASS, _
    lpWindowName:=vbNullString)

If XLHWnd > 0 Then

    '''''''''''''''''''''''''''''''''''''''''
    ' If HWnd is > 0, FindWindow successfully
    ' found the Excel main application window.
    ' Move XLMAIN to the top of the
    ' Z-Order.
    '''''''''''''''''''''''''''''''''''''''''

    Res = BringWindowToTop(HWnd:=XLHWnd)
    If Res = 0 Then
        Debug.Print "Error With BringWindowToTop: " & _
            CStr(Err.LastDllError)
    Else

        '''''''''''''''''''''''''''''''''
        ' No error.
        ' Set keyboard input focus XLMAIN
        '''''''''''''''''''''''''''''''''

        SetFocus HWnd:=XLHWnd
    End If
Else

    '''''''''''''''''''''''''''''''''
    ' HWnd was 0. FindWindow couldn't
   ' find Excel.
   '''''''''''''''''''''''''''''''''
    Debug.Print "Can't find Excel"
End If

End Sub


Bron:
http://www.cpearson.com/excel/ActivateExcelMain.aspx

Dit zou mijn probleem moeten oplossen alleen ik krijg het niet aan de praat, ook niet met het voorbeeld vanuit word naar excel.
 
Laatst bewerkt:
Afdruk samenvoegen vanuit excel

Hallo,

Tot op heden heeft nog niemand gereageerd (op mij na dus). Ik zit met hetzelfde probleem. Heb je toevallig al een oplossing?

Groeten,
Arno
 
ja ik heb de oplossing, sorry nu geen tijd om te posten, wellicht morgen

Ik hoop dat je zolang kunt wachten...

ff in het kort want ik zit krap qua tijd:

vanuit excel open je een word bestand:

Code:
Set appWD = CreateObject("Word.Application")
    With appWD
    .Visible = True
    .Documents.Open Filename:="U:\\overgangbestand.doc"
    End With

Nu wordt dit overgangsbestand geopend. hierin staat de volgende macro:

Code:
Private Sub Document_Open()

 ChangeFileOpenDirectory "U:\"
 Documents.Open FileName:="samenvoegbestand.doc", ConfirmConversions:=False, _
        ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
        PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
        WritePasswordTemplate:="", Format:=wdOpenFormatAuto, XMLTransform:=""
        
    ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
    ActiveDocument.MailMerge.OpenDataSource Name:= _
        "U:\xxxxx.xls", ConfirmConversions _
        :=False, ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
        PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
        WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
        Connection:= _
        "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=U:\XXXXXX.xls;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password=""" _
        , SQLStatement:="SELECT * FROM `'afdruk samenvoegen$'`", SQLStatement1:= _
        "", SubType:=wdMergeSubTypeAccess
    ActiveDocument.MailMerge.ViewMailMergeFieldCodes = wdToggle
    
    
    ActiveWindow.ActivePane.VerticalPercentScrolled = 0
    With ActiveDocument.MailMerge
        .Destination = wdSendToNewDocument
        .SuppressBlankLines = True
        With .DataSource
            .FirstRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
            .LastRecord = ActiveDocument.MailMerge.DataSource.ActiveRecord
        End With
        .Execute Pause:=False
    End With
      
End Sub

Deze code heb ik via de macro recorder bij elkaar gekregen. Van hier uit kun je nog aanpassingen doen, zoals data toevoegen oid. (Ik post niet mijn volledige code, deze is prive)

Vervolgens is er dus een samenvoegbestand, dat via bovenstaande code wordt geopend. Dit samenvoegbestand hbe ik van te voren geconfigureerd door op bepaalde plaatsen de data in te voegen (zoals met afdruk samenvoegen)
Wat de bovenstaande code doet is:

het samenvoegbestand openen,

de data uit excel halen (let op het excel bestand moet open staan!!) en

alles wegschrijven naar een nieuw bestand. (bij mijn code gebeuren en nog een aantal zaken, maar deze staan hier niet in)

Wellicht heb je hier wat aan, ander ben ik bereid je verder te helpen
 
Laatst bewerkt:
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan