alphamax
Terugkerende gebruiker
- Lid geworden
- 16 mrt 2011
- Berichten
- 2.709
- Besturingssysteem
- Windows 11 en-US
- Office versie
- Office 2007 nl-NL
Haalt de string op van de default printer en de poort in de juiste taal.
Zodat je deze in kan vullen in excel bij
Gets the string of the default printer and port in the right language.
So you can enter it in excel with
Nieuwe verbeterde code / new and improved code.
Excel in Module
Access in Module
Zodat je deze in kan vullen in excel bij
Code:
Application.ActivePrinter
Gets the string of the default printer and port in the right language.
So you can enter it in excel with
Code:
Application.ActivePrinter
Code:
[SIZE=1]Private Function GetDefaultPrinterNameAndPort() 'am_2020
Const HKEY_CURRENT_USER = &H80000001
Dim aON As Variant
Dim sDefaultPrinterName As String
Dim sDefaultPrinterPort As String
Dim sKeyPath As String
Dim sOn As String
Dim sValue As String
sKeyPath = "Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts"
With CreateObject("WbemScripting.SWbemLocator")
sDefaultPrinterName = .ConnectServer(".", "root\cimv2").ExecQuery("Select * from Win32_Printer Where Default = True").ItemIndex(0).name
aON = Split(Application.ActivePrinter, " ")
sOn = aON(UBound(aON) - 1)
.ConnectServer(".", "root\default").Get("stdregprov").GetStringValue HKEY_CURRENT_USER, sKeyPath, sDefaultPrinterName, sValue
sDefaultPrinterPort = Mid$(sValue, 10, 5)
End With
GetDefaultPrinterNameAndPort = sDefaultPrinterName & " " & sOn & " " & sDefaultPrinterPort
End Function[/SIZE]
Nieuwe verbeterde code / new and improved code.
Excel in Module
Code:
Option Explicit
Private Declare Function GetProfileStringA Lib "kernel32" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Private Function GetDefaultPrinterNameAndPortExcel()
'http://www.java2s.com/Code/VBA-Excel-Access-Word/Windows-API/Getdefaultprinterinformation.htm
Dim aOn As Variant
Dim aPrinter As Variant
Dim sOn As String
Dim sPrinter As String * 255
GetProfileStringA "Windows", "Device", "", sPrinter, 254
aPrinter = Split(Application.Trim(sPrinter), ",")
aOn = Split(Application.ActivePrinter, " ")
sOn = aOn(UBound(aOn) - 1)
GetDefaultPrinterNameAndPortExcel = aPrinter(0) & " " & sOn & " " & aPrinter(2)
End Function
Public Sub Main()
Dim sDefaultPrinterNameAndPortExcel As String
sDefaultPrinterNameAndPortExcel = GetDefaultPrinterNameAndPortExcel
End Sub
Access in Module
Code:
Option Explicit
Private Declare Function GetProfileStringA Lib "kernel32" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Private Function GetDefaultPrinterNameAndPortAccess() As Printer
'http://www.java2s.com/Code/VBA-Access-Access-Word/Windows-API/Getdefaultprinterinformation.htm
'https://stackoverflow.com/questions/30485227/ms-access-change-default-printer-via-vba
Dim aPrinter As Variant
Dim sPrinter As String * 255
Dim oPrinter As Printer
GetProfileStringA "Windows", "Device", "", sPrinter, 254
aPrinter = Split(sPrinter, ",")
For Each oPrinter In Application.Printers
If oPrinter.DeviceName = aPrinter(0) Then
Set GetDefaultPrinterNameAndPortAccess = oPrinter
End If
Next
End Function
Public Sub Main()
Dim oDefaultPrinterNameAndPortAccess As Printer
Set oDefaultPrinterNameAndPortAccess = GetDefaultPrinterNameAndPortAccess
End Sub
Laatst bewerkt: