[VB] Label uitprinten via print dialog

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

  • Montaner
  • Registratie: Januari 2005
  • Laatst online: 01-09 08:19
Om labels te printen via een CRM in Access, worden er DYMO Label printers gebruikt. Nu staan er hier op het kantoor 2, beide aangesloten op een andere locatie.

Deze code wordt er gebruikt om op 1 printer te printen:

Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
Private Sub Print_Lokaal_Click()
'Rembo wrote this routine - http://scriptorium.serve-it.nl
'This routine prints Excel data on sticker by using a template and the Dymo high level COM.
'It assumes you have a Dymo labelwriter printer and you have created a label that will
'serve as a template. On the label are two'text objects named OText1 and OText2.
'As a data source we assume text in cells B2:B5 and C2:C5 on your the first worksheet but
'obviously you can use any data source you like.

    Dim myDymo As Object
    Dim myLabel As Object
    Dim sPrinters As String
    Dim arrPrinters() As String
    Dim selectedPrinter As String
    Dim i As Long, i2 As Long, iStart As Long
    
    Dim h_prnum As String
    Dim h_fin As String
    Dim h_buyref As String
    Dim h_date As String
    Dim h_amountlabels As Integer
    Dim h_labeltype As Integer
    Dim stDocName As String
    Dim stLinkCriteria As String
    Dim h_hoofddir As String
    
    If IsNull(Me.Sample_labeltype.Value) = True Then
        MsgBox "First select where the sample comes from and what its destination is"
        Me.Sample_labeltype.SetFocus
        Exit Sub
    End If
    
    h_prnum = Me.prnum.Value
    h_fin = Me.fin.Value
    h_buyref = Me.buy_ref.Value
    h_date = Format(Me.Date.Value, "dd/mm/yyyy")
    h_amountlabels = Me.Amount_labels.Value
    h_labeltype = Me.Sample_labeltype.Value
    h_hoofddir = CodeProject.Path
    
    On Error Resume Next
    Set myDymo = CreateObject("Dymo.DymoAddIn")
    Set myLabel = CreateObject("Dymo.DymoLabels")
    If (myDymo Is Nothing) Or (myLabel Is Nothing) Then
        stDocName = "Label_melding"
        DoCmd.OpenForm stDocName, , , stLinkCriteria
        Exit Sub
    End If
    
    'Check forDymo printer(s)
    'If there is one proceed and store the printernames in a variable, else quit
    sPrinters = myDymo.GetDymoPrinters()
    If sPrinters = "" Then
        Exit Sub
    Else
        i2 = 0
        iStart = 1
        For i = 1 To Len(sPrinters)
            If Mid(sPrinters, i, 1) = "|" Then
                i2 = i2 + 1
                ReDim Preserve arrPrinters(i2 + 1)
                arrPrinters(i2) = Mid(sPrinters, iStart, i - iStart)
                iStart = i + 1
            End If
        Next i
    End If
    
    'Store the current default printer and select the Dymprinter of your choice
    'sDefaultPrinter = Application.ActivePrinter
    With myDymo
        '0 is first Dymo printer, you could use the printername instead: SelectPrinter "YourPrintername"
        
 
    
        
        .SelectPrinter arrPrinters(0)
    End With
   
    'Open the label template
    If h_labeltype = 1 Then
    myLabel = myDymo.Open(h_hoofddir & "\dymo-templates\Sample - Logo 1.LWL")
    Else
    myLabel = myDymo.Open(h_hoofddir & "\dymo-templates\Sample - Logo 2.LWL")
    End If
    
    For i = 1 To 1
        'Give text objects OText1 and OText2 on the label a value
    
    If h_labeltype = 1 Then
        With myLabel
            .SetField "TEKST", h_prnum
            .SetField "TEKST_1", h_fin
            .SetField "TEKST_3", h_date
        End With
    Else
        With myLabel
            .SetField "TEKST", h_prnum
            .SetField "TEKST_1", h_fin
            .SetField "TEKST_2", h_buyref
            .SetField "TEKST_3", h_date
        End With
    End If
        'Print the label
       With myDymo
            .StartPrintJob  'Only used for Turbo 400 model and higher for print optimizing, you may omit it
            .Print h_amountlabels, False ' Print 2 copies
            .EndPrintJob    'Only used for Turbo 400 model and higher for print optimizing, you may omit it
       End With
    
    Next i
    
    
    'Make sure the default printer is selected again
    'Application.ActivePrinter = sDefaultPrinter
    
    'Clean up
    Set myLabel = Nothing
    Set myDymo = Nothing
    selectedPrinter = ""
    

End Sub


Dit werkt perfect, sinds hij gewoon de eerste dymo printer pakt die hij tegen komt. Om dan te zeggen dat hij de 2e printer bij een andere knopt pakt, werkt dan weer niet. Sinds op andere plekken meerdere printers aangesloten zijn staan de printers dus in een verschillende volgorde in de arrays op de andere werkstations.

Nu wil ik het zo gaan maken dat hij gewoon een print dialog krijgt, maar ik heb geen idee hoe ik het voor elkaar ga krijgen dat hij alleen de opgebouwde label gaat uitprinten. Ik had de functie DoCmd.RunCommand acCmdPrint gevonden, maar deze print het hele formulier uit, en natuurlijk niet het opgebouwde label.

Nu is mijn vraag, kan ik het bijvoorbeeld niet in een variabele laten opslaan welke printer geselecteerd wordt via het dialog, of hoe de label kan worden verzonden naar de geselecteerde printer via het dialog.

Ik heb al een omweggetje geprobeerd, om via het aantal printers dat hij vind te controleren op naam, en zo een printer te selecteren. Maar op remote desktops en clients zijn de namen weer anders toegevoegd. Helaas werkt printen op sharenaam ook niet, tenzij hier iemand een oplossing voor weet is dat ook perfect.

Acties:
  • 0 Henk 'm!

Verwijderd

Één van mijn eerste hits op google gaf dit url. Kan volgens mij niet moeilijk zijn om dat in de bestaande code te verwerken.