achei isso, mas não entendo muito bem e deu errado
Sub teste()
Dim attachpath As String
Dim emailserver As String
Dim MailRecipient As String
Dim ctlTableFactory, RFC_READ_TABLE, eQUERY_TAB, tblOptions, tblData, tblFields, funcControl, objConnection, ctlLogon
Set objFileSystemObject = CreateObject("Scripting.FileSystemObject")
Set ctlLogon = CreateObject("SAP.LogonControl.1")
Set funcControl = CreateObject("SAP.Functions")
Set ctlTableFactory = CreateObject("SAP.TableFactory.1")
Set objWindowsScriptShell = CreateObject("WScript.Shell")
Set objConnection = ctlLogon.NewConnection
'Set filOutput = objFileSystemObject.CreateTextFile(attachpath, True)
objConnection.ApplicationServer = ""
objConnection.SystemNumber = ""
objConnection.Client = ""
objConnection.Language = ""
objConnection.User = InputBox("login")
objConnection.Password = InputBox("senha")
booReturn = objConnection.logon(0, True)
If booReturn <> True Then
MsgBox " Cannot log on! "
MsgBox booReturn
Stop
Else
funcControl.Connection = objConnection
Set RFC_READ_TABLE = funcControl.Add("RFC_READ_TABLE")
Set strExport1 = RFC_READ_TABLE.Exports("QUERY_TABLE")
Set strExport2 = RFC_READ_TABLE.Exports("DELIMITER")
Set tblOptions = RFC_READ_TABLE.Tables("OPTIONS")
Set tblData = RFC_READ_TABLE.Tables("DATA")
Set tblFields = RFC_READ_TABLE.Tables("FIELDS")
'strExport1.Value = "MARA"
'strExport2.Value = ";"
'Change next line to use different selection criteria
' (EQ means =, LT means <, GT means >)
' Repeat the two lines below incrementing the number each time if your selection criteria exceeds 40
'tblOptions.AppendRow
'tblOptions(1, "TEXT") = "MANDT EQ '300' "
'tblOptions.AppendRow
'tblOptions(2, "TEXT") = " AND ARRIVDEPA EQ '" & InputBox("1 for Arrivals" & vbCrLf & "2 for Despatches") & "' AND NUMMBUKRS EQ '1000'"
' Repeat section between /** marks for each field that you want returned by the query, incrementing the number each time
'/**
'tblFields.AppendRow
'tblFields(1, "FIELDNAME") = "MATNR"
'**
'tblFields.AppendRow
'tblFields(2, "FIELDNAME") = "NAME1"
If RFC_READ_TABLE.Call = True Then
If tblData.RowCount > 0 Then
' Change Next line to write a different header row
For intRow = 1 To tblData.RowCount
Dim celula As String
Dim COLUNA As Integer
COLUNA = 0
COLUNA = COLUNA + 1
celula = "A" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "B" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "C" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "D" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "E" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "F" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "G" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "H" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "I" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "J" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "H" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "L" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "M" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "O" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "P" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "Q" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "R" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "S" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "T" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "U" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
COLUNA = COLUNA + 1
celula = "V" & intRow
Call escrever(celula, tblData(intRow, COLUNA))
Next
Else
End If
Else
End If
End If
'Set filOutput = Nothing
'MailProfile = "MS Exchange Settings"
'Set objSession = CreateObject("MAPI.Session")
'Set wshnet = CreateObject("Wscript.network")
'LOGONOK = objSession.logon("", "", False, True, 0, True, emailserver & vbLf & wshnet.UserName)
'Set objMessage = objSession.Outbox.Messages.Add
'Change below to Change subject line of email
'objMessage.Subject = "Intrastat Response"
' Change below to change body text of message
'strSetMessage = "There were " & tblData.RowCount & " records returned by your query"
'objMessage.Text = strSetMessage
' Change below to Change attachment title
'Set objOneAttach = objMessage.Attachments.Add("Intrastat", , 1, attachpath)
'objOneAttach.ReadFromFile (attachpath)
' Repeat block between /** markers for each recipient
' /**
'Set objOneRecip = objMessage.Recipients.Add
' Change name below for different recipients
'objOneRecip.Name = MailRecipient
'objOneRecip.Type = 1
'objOneRecip.Resolve
' **
'objMessage.Importance = 1
'objMessage.Update
'objMessage.Send
'objSession.Logoff
'objConnection.Logoff
End Sub
Sub escrever(linha, valor)
'
' escrever Macro
' Macro recorded 6/21/2007 by nascimm
'
Range(linha).Select
ActiveCell.FormulaR1C1 = valor
End Sub
Existem mil maneiras de preparar Neston. Invente a sua!
http://www.youtube.com/ExpressoExcel
Postado : 11/08/2009 4:41 am