Ir ao conteúdo
  • Cadastre-se

Erro ao imprimir


Posts recomendados

Boa tarde!

 

Senhores, esse codigo abaixo executa uma macro no SAP e chegando em determinada tela ele manda imprimir em pdf, ai segue em frente, porém quando ele abre a tela de salvar do PDF ele nao executa mais, pois o pdf teve uma atualização e o botao que era salvar virou save e ele nao aceita e nao prossegue as operações.

 

fico agradecido.

 

Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As LongPtr, ByVal lpString As String, ByVal cch As LongPtr) As LongPtr
Private Declare PtrSafe Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As LongPtr) As LongPtr
Private Declare PtrSafe Function GetWindow Lib "user32" (ByVal hWnd As LongPtr, ByVal wCmd As LongPtr) As LongPtr
Private Declare PtrSafe Function IsWindowVisible Lib "user32" (ByVal hWnd As LongPtr) As Boolean
Private Declare PtrSafe Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hWnd As LongPtr, ByVal msg As LongPtr, ByVal wParam As LongPtr, ByVal lParam As LongPtr, ByVal fuFlags As LongPtr, ByVal uTimeout As LongPtr, lpdwResult As LongPtr) As LongPtr
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As LongPtr, ByVal wParam As LongPtr, lParam As String) As LongPtr
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As LongPtr, ByVal wParam As LongPtr, lParam As String) As LongPtr
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
Private Const WM_CHAR = &H102
Private Const BM_CLICK As Long = &HF5&
Private Const GW_HWNDNEXT = 2
Private Const WM_SETTEXT As Long = &HC
Private Const VK_RETURN As Long = &HD
Private Const WM_KEYDOWN As Integer = &H100
Private Const SMTO_ABORTIFHUNG As Long = &H2
Sub Imprimir_RM()
    Dim hWnd As String
    Dim hwndpdf As String
    Dim hwndsave As String
    Dim button As String
    Dim sendmessageError As String
    Dim nomeArquivo As String
    Dim a As Integer
    Dim pStr As String
    Dim pastaArquivo As String
    
    pastaArquivo = Cells(2, 4)
    If Right(pastaArquivo, 1) <> "\" Then
        pastaArquivo = pastaArquivo & "\"
    End If
    
    On Error Resume Next
    
    If Not IsObject(Julio) Then
       Set SapGuiAuto = GetObject("SAPGUI")
       Set Julio = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(Connection) Then
       Set Connection = Julio.Children(0)
    End If
    If Not IsObject(session) Then
       Set session = Connection.Children(0)
    End If
    If IsObject(WScript) Then
       WScript.ConnectObject session, "on"
       WScript.ConnectObject Julio, "on"
    End If
    On Error Resume Next
    session.findById("wnd[1]/usr/btnSPOP-OPTION2").press
    Application.DisplayAlerts = False
    x = 7
    Do While Cells(x, 12).Value <> ""
        Tipo = UCase(Cells(x, 11).Value)
        If Tipo = "X" Then
            nomeArquivo = Cells(x, 14)
            Sheets("RM").Activate
            session.findById("wnd[0]/tbar[0]/okcd").Text = "ysmedicao"
            session.findById("wnd[0]").sendVKey 0
            session.findById("wnd[0]/usr/ctxtEKPO-WERKS").Text = "0200"
            session.findById("wnd[0]/usr/ctxtEKPO-KONNR").Text = Range("B2").Value
            session.findById("wnd[0]/usr/ctxtESSR-LZVON").Text = ""
            session.findById("wnd[0]/usr/ctxtESSR-LZBIS").Text = ""
            session.findById("wnd[0]/usr/ctxtYSMEDICAO_FRS-MEDICAO").Text = Cells(x, 12).Value
            session.findById("wnd[0]").sendVKey 0
            session.findById("wnd[0]/usr/cmbWC_PARCEIRO").Key = Range("B3").Value
            session.findById("wnd[0]/usr/cmbWC_PARCEIRO").SetFocus
            session.findById("wnd[0]/tbar[1]/btn[16]").press
            session.findById("wnd[0]/usr/tblSAPMYS_MEDICOES_REAJUSTAMENTOTABCTL_FRS").verticalScrollbar.Position = 0
            'impressão
            session.findById("wnd[0]/tbar[1]/btn[5]").press
            session.findById("wnd[1]/tbar[0]/btn[86]").press
            Application.OnKey "{ENTER}", "Continue"
            session.findById("wnd[0]/tbar[0]/btn[15]").press
            'enviar comandos janelas de impressão
            a = 1
            hWnd = ""
            Do While hWnd = "" Or hWnd = "0"
                hWnd = FindWindow(vbNullString, "Imprimir")
                a = a + 1
                Application.Wait (Now + TimeValue("0:00:01"))
                If a > 10000 Then
                    MsgBox "erro"
                    Exit Sub
                End If
            Loop
            button = FindWindowEx(hWnd, 0, "Button", "OK")
            sendmessageError = SendMessage(button, BM_CLICK, 0, 0)
            a = 1
            hwndpdf = ""
            Do While hwndpdf = "" Or hwndpdf = "0"
                hwndpdf = FindWindow(vbNullString, "PDFCreator 1.7.0")
                a = a + 1
                Application.Wait (Now + TimeValue("0:00:01"))
                If a > 10000 Then
                    MsgBox "erro"
                    Exit Sub
                End If
            Loop
            button = FindWindowEx(hwndpdf, 0, "ThunderRT6CommandButton", "&Salvar")
            Call SendMessageTimeout(button, BM_CLICK, 0, 0, 2, 10, 0)
            a = 1
            hwndsave = ""
            Do While hwndsave = "" Or hwndsave = "0"
                hwndsave = FindWindow(vbNullString, "Salvar como")
                a = a + 1
                Application.Wait (Now + TimeValue("0:00:01"))
                If a > 100 Then
                    MsgBox "erro"
                    Exit Sub
                End If
            Loop
            button = ""
            editButton = ""
            hwndsave = FindWindow(vbNullString, "Salvar como")
            arquivo = pastaArquivo & nomeArquivo
            Do While editButton = ""
                button = FindWindowEx(hwndsave, 0, "DUIViewWndClassName", "")
                button = FindWindowEx(button, 0, "DirectUIHWND", "")
                button = FindWindowEx(button, 0, "FloatNotifySink", "")
                button = FindWindowEx(button, 0, "ComboBox", "")
                editButton = FindWindowEx(button, 0, "Edit", "")
                Application.Wait (Now + TimeValue("0:00:01"))
            Loop
            sendmessageError = SendMessage(editButton, WM_SETTEXT, 0, ByVal arquivo)
            Application.Wait (Now + TimeValue("0:00:01"))
            button = FindWindowEx(hwndsave, 0, "Button", "Sa&lvar")
            sendmessageError = SendMessage(button, BM_CLICK, 0, 0)
            Application.Wait (Now + TimeValue("0:00:01"))
            sendmessageError = SendMessage(editButton, WM_SETTEXT, 0, ByVal arquivo)
            sendmessageError = SendMessage(button, BM_CLICK, 0, 0)
        End If
        x = x + 1
    Loop
    session.findById("wnd[0]/tbar[0]/btn[15]").press
End Sub

 

Link para o comentário
Compartilhar em outros sites

Prezados, mais uma vez solicito a ajuda dos senhores para o meu problema, pois estou chegando em uma semana critica onde preciso agilizar diversos procedimentos com este codigo e o mesmo esta inviabilizado pelo fato de nao conseguir mais imprimir e isso esta gerando um imenso HH. Se alguem puder ajudar, ficarei grato.

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

Ebook grátis: Aprenda a ler resistores e capacitores!

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!