2 years ago

#39053

test-img

scoulter

Using an XLSM to capture and output from a database

I have a XLSM that I am writing VBA for. The original workbook has a button that pulls records from a database has the following code involved:

Option Explicit
Public SapGuiAuto, WScript, msgcol
Public objGui  As GuiApplication
Public objConn As GuiConnection
Public session As GuiSession

Sub SAPDownloadAttachment()

Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
Set objConn = objGui.Children(0)
Set session = objConn.Children(0)

session.FindById("wnd[0]").Maximize
session.FindById("wnd[0]/tbar[0]/okcd").Text = "IW37N"
session.FindById("wnd[0]").SendVKey 0
session.FindById("wnd[0]/tbar[1]/btn[8]").Press
session.FindById("wnd[0]/mbar/menu[0]/menu[6]").Select
session.FindById("wnd[1]/tbar[0]/btn[0]").Press
session.FindById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").Select
session.FindById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").SetFocus
session.FindById("wnd[1]/tbar[0]/btn[0]").Press
session.FindById("wnd[1]/tbar[0]/btn[0]").Press
Worksheets("Sheet1").Range("A:Z").Columns.AutoFit
Worksheets("Sheet1").Range("L:L").Columns.AutoFit
Worksheets("Sheet1").Range("B:B").Columns.AutoFit
Rows(1).Insert shift:=xlShiftDown
Rows(1).Insert shift:=xlShiftDown
Rows(1).Insert shift:=xlShiftDown
Range("A4:Z4").AutoFilter
Selection.HorizontalAlignment = xlCenter
Dim oOLE As Excel.Button
Set oOLE = ActiveSheet.Buttons.Add(Left:=0, Top:=0, Height:=45, Width:=200)
oOLE.Caption = "Return to Original"
oOLE.OnAction = ""

The issue I am having is when the program gathers what it needs to from the database it prints a new XLSX workbook... I need it to print an XLSM so that I can add an action to the button to close the newly printed workbook and return to the original workbook where more buttons will be placed. Is it possible to have a macro workbook print another macro workbook?

excel

vba

xlsx

xlsm

0 Answers

Your Answer

Accepted video resources