Прочитайте установочный файл принтера и получите информацию о разделе принтера и напишите в ListBox - PullRequest
0 голосов
/ 21 февраля 2020

INTRO

прежде всего - извините за мой английский sh - и я новичок в Excel vba.

У меня есть пользовательская форма Excel с несколькими текстовыми полями, двумя списками и одной кнопкой. Если я нажимаю кнопку, открывается диалоговое окно «Файл» и выбираю установочный файл printer.inf. Я пытался прочитать несколько файлов принтера, но я не могу получить доступ / обрезать строку в Line77. Сначала я узнаю производителя, который работает. Тогда я хочу выяснить, если его только 86 или 64 драйвера и сравнить его с производителем, чтобы установить новую переменную. с этой переменной я хочу исследовать раздел и получить каждый элемент принтера и установить его в ListBox. Но он не может выяснить, является ли он 86 или 64 или оба, и сравнить его с производителем с новыми переменными. В Line77 есть переменная, в которой я хочу получить строку для поиска 86 или 64 и обрезать ее максимум до двух новых переменных.

КОД

Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias _
    "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, _
    ByVal nSize As Long, ByVal lpFileName As String) As Long

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, _
ByVal lpFileName As String) As Long

Function IniFileName() As String  
  With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Filters.Add "INF Files", "*.inf", 1
        .Show
        fullpath = .SelectedItems.Item(1)
    End With
  IniFileName = fullpath
End Function

Public Function GetFilenameFromPath(ByVal strPath As String) As String
    If Right$(strPath, 1) <> "\" And Len(strPath) > 0 Then
        GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) - 1)) + Right$(strPath, 1)
    End If
End Function

Function ReadIniFileString(ByVal Sect As String, ByVal Keyname As String, ByVal infFileName As String) As String
    Dim Worked As Long
    Dim RetStr As String * 128
    Dim StrSize As Long

  iNoOfCharInIni = 0
  sIniString = ""
  If Sect = "" Or Keyname = "" Then
    MsgBox "Section Or Key To Read Not Specified !!!", vbExclamation, "INI"
  Else
    sProfileString = ""
    RetStr = Space(128)
    StrSize = Len(RetStr)
    Worked = GetPrivateProfileString(Sect, Keyname, "", RetStr, StrSize, infFileName)
    If Worked Then
      iNoOfCharInIni = Worked
      sIniString = Left$(RetStr, Worked)
    End If
  End If
  ReadIniFileString = sIniString
End Function

Function ReadIniSection(ByVal Filename As String, ByVal Section As String) As String
    Dim RetVal As String * 255, v As Long
    v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
    ReadIniSection = Left(RetVal, v + 0)
End Function

Private Sub btn01_Click()

'GET FILENAME
strIniFileName = IniFileName
strGetFilenameFromPath = GetFilenameFromPath(strIniFileName)

'GET ATTRIBUTES OF INF FILE
strClass = ReadIniFileString("Version", "Class", strIniFileName)

If strClass = "Printer" Then
    Dim S As String
    S = ReadIniSection(strIniFileName, "Manufacturer")
    Dim vLines As Variant
    vLines = Split(S, Chr$(0))
    Dim vLine As Variant
    For Each vLine In vLines
        If vLine <> "" Then
        'MsgBox vLine
            strlength = Len(vLine)
            Pos1 = InStr(vLine, "=")
            'MsgBox "pos1: " & Pos1
            pos2 = Mid(vLine, Pos1 + 1, strlength - Pos1)
            strManufacturer = Left(pos2, InStr(1, pos2, ",") - 1)
            '---> PROBLEM strDriverVersions
            strDriverVersions = Right(pos2, Len(pos2) - Len(strManufacturer) - 1)
            strNewSec2 = ""
            If InStr(strDriverVersions, ",") Then
                strx1 = Left(strDriverVersions, InStr(1, strDriverVersions, ",") - 1)
                strx2 = Right(strDriverVersions, InStr(1, strDriverVersions, ",") + 1)
                'compare strManufacturer and 64 86 in two variables for new sectionsearch
                strNewSec1 = strManufacturer & "." & strx1
                strNewSec2 = strManufacturer & "." & strx2
            Else
                If InStr(strDriverVersions, "86") Then
                'compare strManufacturer and 86 in a variable for new sectionsearch
                strNewSec1 = strManufacturer & "." & strDriverVersions
                End If
                If InStr(strDriverVersions, "64") Then
                'compare strManufacturer and 64 in a variable for new sectionsearch
                strNewSec1 = strManufacturer & "." & strDriverVersions
                End If
            End If
        End If
    Next vLine

    'READ DRIVERNAMES
    S = ReadIniSection(strIniFileName, strNewSec1)
    vLines = Split(S, Chr$(0))
    For Each vLine In vLines
       strlength = Len(vLine)
       Pos1 = InStr(vLine, "=")
       pos2 = replace(Left(vLine, Pos1 - 1), """", "")
       If InStr(strNewSec1, "64") Then
            ListBox1.AddItem pos2
        End If
        If InStr(strNewSec1, "86") Then
            ListBox2.AddItem pos2
        End If
    Next vLine

    If strNewSec2 <> "" Then
        S = ReadIniSection(strIniFileName, strNewSec2)
        vLines = Split(S, Chr$(0))
        For Each vLine In vLines
       strlength = Len(vLine)
       Pos1 = InStr(vLine, "=")
       pos2 = replace(Left(vLine, Pos1 - 1), """", "")
       If InStr(strNewSec2, "64") Then
            ListBox1.AddItem pos2
        End If
        If InStr(strNewSec2, "86") Then
            ListBox2.AddItem pos2
        End If
        Next vLine
    End If

    strDriverVer = ReadIniFileString("Version", "DriverVer", strIniFileName)
    TextBox2.Value = strGetFilenameFromPath
    TextBox3.Value = strManufacturer
    TextBox4.Value = strDriverVer
Else
    MsgBox "NOT A PRINTER INF FILE"
    Exit Sub
End If

End Sub

ПРИМЕРЫ ФАЙЛОВ

Я приложил три выдержки из файла inf. Поэтому я надеюсь, что это может лучше объяснить, что я хочу.

пример файла inf 1:

[Manufacturer]
%LEXMARK%=LEXMARK PRINTERS,NTamd64,NTx86.6.0,NTamd64.6.0

[LEXMARK PRINTERS]
"Lexmark Universal v2 XL" = LMUD1P40.GPD.NTx86.5.1,USBPRINT\LexmarkLexmark_Unive1A7D
"Lexmark Universal v2 XL" = LMUD1P40.GPD.NTx86.5.1,,WSDPRINT\1284_CID_LexmarkPrinterColorA

[LEXMARK PRINTERS.NTamd64]
"Lexmark Universal v2 XL" = LMUD1P40.GPD.NTamd64.5.1,USBPRINT\LexmarkLexmark_Unive1A7D
"Lexmark Universal v2 XL" = LMUD1P40.GPD.NTamd64.5.1,,WSDPRINT\1284_CID_LexmarkPrinterColorA

[LEXMARK PRINTERS.NTx86.6.0]
"Lexmark Universal v2 XL" = LMUD1P40.GPD.NTx86.6.0,USBPRINT\LexmarkLexmark_Unive1A7D
"Lexmark Universal v2 XL" = LMUD1P40.GPD.NTx86.6.0,,WSDPRINT\1284_CID_LexmarkPrinterColorA

[LEXMARK PRINTERS.NTamd64.6.0]
"Lexmark Universal v2 XL" = LMUD1P40.GPD.NTamd64.6.0,USBPRINT\LexmarkLexmark_Unive1A7D
"Lexmark Universal v2 XL" = LMUD1P40.GPD.NTamd64.6.0,,WSDPRINT\1284_CID_LexmarkPrinterColorA

Результат после прочтения файла inf 1:

TextBox1.Value =    "LEXMARK PRINTERS"
ListBox1 =          "Lexmark Universal v2 XL"
ListBox2 =          "Lexmark Universal v2 XL"

пример inf файл 2:

[MANUFACTURER]
%HP%=HP,NTAMD64

[HP.NTAMD64]
"HP Universal Printing PCL 5" = hpcu2306.gpd.NTAMD64,USBPRINT\Hewlett-PackardHP_LaAE38
"HP Universal Printing PCL 6" = hpcu2306.gpd.NTAMD64,USBPRINT\Hewlett-PackardHP_Co5005

Результат после чтения файла inf 2:

TextBox1.Value =    "HP"
ListBox1 =          "HP Universal Printing PCL 5"
                    "HP Universal Printing PCL 6"
ListBox2 =          ""

пример файла inf 3

[Manufacturer]
%Company% = DriverName,NTx86,NTamd64

[DriverName]
"Brother HL-L3210CW series" = BRSHL3210CW17A.DSI,USBPRINT\BROTHERHL-L3210CW_SEEDB1,BROTHERHL-L3210CW_SEEDB1
"Brother HL-L3230CDN series" = BRSHL3230CDN17A.DSI,USBPRINT\BROTHERHL-L3230CDN_S7497,BROTHERHL-L3230CDN_S7497
"Brother HL-L3230CDW series" = BRSHL3230CDW17A.DSI,USBPRINT\BROTHERHL-L3230CDW_SD3D7,BROTHERHL-L3230CDW_SD3D7

[DriverName.NTx86]
"Brother HL-L3210CW series" = BRSHL3210CW17A.DSI,USBPRINT\BROTHERHL-L3210CW_SEEDB1,BROTHERHL-L3210CW_SEEDB1
"Brother HL-L3230CDN series" = BRSHL3230CDN17A.DSI,USBPRINT\BROTHERHL-L3230CDN_S7497,BROTHERHL-L3230CDN_S7497
"Brother HL-L3230CDW series" = BRSHL3230CDW17A.DSI,USBPRINT\BROTHERHL-L3230CDW_SD3D7,BROTHERHL-L3230CDW_SD3D7

[DriverName.NTamd64]
"Brother HL-L3210CW series" = BRSHL3210CW17A.DSI,USBPRINT\BROTHERHL-L3210CW_SEEDB1,BROTHERHL-L3210CW_SEEDB1
"Brother HL-L3230CDN series" = BRSHL3230CDN17A.DSI,USBPRINT\BROTHERHL-L3230CDN_S7497,BROTHERHL-L3230CDN_S7497
"Brother HL-L3230CDW series" = BRSHL3230CDW17A.DSI,USBPRINT\BROTHERHL-L3230CDW_SD3D7,BROTHERHL-L3230CDW_SD3D7

Результат после чтения / обработки файла inf 2:

TextBox1.Value =    "DriverName"
ListBox1 =          "Brother HL-L3210CW series"
                    "Brother HL-L3230CDN series"
                    "Brother HL-L3230CDW series"
ListBox2 =          "Brother HL-L3210CW series"
                    "Brother HL-L3230CDN series"
                    "Brother HL-L3230CDW series"

ВОПРОС

Как извлечь строку в Line77 вправо, чтобы задать максимум две новые переменные для поиска в следующем разделе?

Спасибо Привет Джеральд

...