I'm able to unblock the outlets by utilizing a SAP GUI script and VBA macros. I need to keep the outlet code(Column a) and status 0(Column B) on an Excel sheet, and then when I click start script, sap will perform the unblocking activities.
Excel template looks as follows,
This VBA code works.
Option Explicit
Public SapGuiAuto, WScript, msgcol
Public objGui As GuiApplication
Public objConn As GuiConnection
Public objSess As GuiSession
Public objSBar As GuiStatusbar
Public objSheet As Worksheet
Dim W_System
Dim iCtr As Integer
Const tcode = "XD05"
Function Attach_Session(iRow, Optional mysystem As String) As Boolean
Dim il, it
Dim W_conn, W_Sess
' Unless a system is provided (XXXYYY where XXX is SID and YYY client)
' get the system from the sheet (in this case it is in cell A8)
If mysystem = "" Then
W_System = ActiveSheet.Cells(iRow, 1)
Else
W_System = mysystem
End If
' If we are already connected to a session, exit do not try again
If W_System = "" Then
Attach_Session = False
Exit Function
End If
' If the session object is not nil, use that session (assume connected to the correct session)
If Not objSess Is Nothing Then
If objSess.Info.SystemName & objSess.Info.Client = W_System Then
Attach_Session = True
Exit Function
End If
End If
' If not connected to anything, set up the objects
If objGui Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
End If
' Cycle through the open SAP GUI sessions and check which is in the same system running the matching transaction
For il = 0 To objGui.Children.Count - 1
Set W_conn = objGui.Children(il + 0)
For it = 0 To W_conn.Children.Count - 1
Set W_Sess = W_conn.Children(it + 0)
If W_Sess.Info.SystemName & W_Sess.Info.Client = W_System Then
Set objConn = objGui.Children(il + 0)
Set objSess = objConn.Children(it + 0)
Exit For
End If
Next
Next
' If nothing is found, display and error message
If objSess Is Nothing Then
MsgBox "No active session to system " + W_System + " with transaction " + tcode + ", or scripting is not enabled.", vbCritical + vbOKOnly
Attach_Session = False
Exit Function
End If
' Turn on scripting
If IsObject(WScript) Then
WScript.ConnectObject objSess, "on"
WScript.ConnectObject objGui, "on"
End If
' Maximize the window of the connected session
Set objSBar = objSess.findById("wnd[0]/sbar")
objSess.findById("wnd[0]").Iconify
Attach_Session = True
End Function
Public Sub StartProcessing()
Dim W_Obj1, W_Obj2, W_Obj3, W_Obj4, iRow
Dim W_Func
Dim W_Src_Ord
Dim W_Ret As Boolean
Dim itemcount As Integer
Dim itemmax As Integer
Const startrow As Integer = 5 'First row with actual data
Set objSheet = ActiveWorkbook.ActiveSheet
' Connect to a system stored in cell A8
W_Ret = Attach_Session(2)
If Not W_Ret Then
MsgBox "Not connected to client"
GoTo MyEnd
End If
itemcount = 0
itemmax = 0
' Determine the number of items to be processed: where the status is zero
For iRow = startrow To objSheet.UsedRange.Rows.Count
If objSheet.Cells(iRow, 2) = "0" Then
itemmax = itemmax + 1
End If
Next
' Update the counter in cell A9
objSheet.Cells(3, 1) = itemcount & "/" & itemmax
' Cycle through the rows with status 0 and call the ProcessRow function to process them
For iRow = startrow To objSheet.UsedRange.Rows.Count
If objSheet.Cells(iRow, 2) = "0" Then
Call ProcessRow(iRow)
itemcount = itemcount + 1
objSheet.Cells(3, 1) = itemcount & "/" & itemmax
End If
Next
MyEnd:
' destory the objects, free up memory
Set objSess = Nothing
Set objGui = Nothing
Set SapGuiAuto = Nothing
MsgBox "Script completed.", vbInformation + vbOKOnly
End Sub
Function ProcessRow(iRow)
Dim W_BPNumber, W_Blockcode, W_Companycode, W_Salesorg
Dim lineitems As Long
' Set the line status to "processing..."
objSheet.Cells(iRow, 2) = 1
'BP Number
If objSheet.Cells(iRow, 1) <> "" Then
W_BPNumber = objSheet.Cells(iRow, 1)
Else
W_BPNumber = "xxxxxx"
End If
'Company Code
If objSheet.Cells(iRow, 2) <> "03" Then
W_Companycode = objSheet.Cells(iRow, 2)
Else
W_Companycode = "xxxxxx"
End If
'Sales Org
If objSheet.Cells(iRow, 4) <> "" Then
W_Salesorg = objSheet.Cells(iRow, 4)
Else
W_Salesorg = "xxxxxx"
End If
'Block Code
If objSheet.Cells(iRow, 5) <> "" Then
W_Blockcode = objSheet.Cells(iRow, 5)
Else
W_Blockcode = ""
End If
' Set error handling to the below code where we can capture if a line had failed in the GUI script
On Error GoTo myerr
' SAP GUI Script starts here
objSess.findById("wnd[0]").Iconify
'objSess.findById("wnd[0]").Maximize
objSess.findById("wnd[0]/tbar[0]/okcd").Text = "/nXD05"
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/usr/ctxtRF02D-KUNNR").Text = W_BPNumber
objSess.findById("wnd[0]/usr/ctxtRF02D-BUKRS").Text = "1172"
objSess.findById("wnd[0]/usr/ctxtRF02D-VKORG").Text = "1172"
objSess.findById("wnd[0]/usr/ctxtRF02D-VTWEG").Text = "10"
objSess.findById("wnd[0]/usr/ctxtRF02D-SPART").Text = "00"
objSess.findById("wnd[0]/usr/ctxtRF02D-SPART").SetFocus
objSess.findById("wnd[0]/usr/ctxtRF02D-SPART").caretPosition = 2
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/usr/ctxtKNA1-AUFSD").Text = ""
objSess.findById("wnd[0]/usr/ctxtKNVV-AUFSD").Text = ""
objSess.findById("wnd[0]/usr/ctxtKNVV-AUFSD").SetFocus
objSess.findById("wnd[0]/usr/ctxtKNVV-AUFSD").caretPosition = 0
objSess.findById("wnd[0]/tbar[0]/btn[11]").press
objSess.findById("wnd[0]").sendVKey 12
' Gets the message from the status bar and save it in column B
objSheet.Cells(iRow, 5) = objSBar.Text
' Update the Status to "Completed" and exit
objSheet.Cells(iRow, 2) = 2
Exit Function
myerr:
' Update the status to "Error"
objSheet.Cells(iRow, 2) = 3
End Function
My requirements
If the following field in sap has the value 03, the outlet should be unblocked; otherwise, an error message should show in the excel file for that particular outlet.
objSess.findById("wnd[0]/usr/ctxtKNA1-AUFSD").Text = ""
objSess.findById("wnd[0]/usr/ctxtKNVV-AUFSD").Text = ""
How to achieve this with VBA?
not sure I fully understand what you are asking here but maybe something like this:
Const cBlockText As String = "03"
objSess.findById("wnd[0]").Iconify
objSess.findById("wnd[0]/tbar[0]/okcd").Text = "/nXD05"
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/usr/ctxtRF02D-KUNNR").Text = W_BPNumber
objSess.findById("wnd[0]/usr/ctxtRF02D-BUKRS").Text = "1172"
objSess.findById("wnd[0]/usr/ctxtRF02D-VKORG").Text = "1172"
objSess.findById("wnd[0]/usr/ctxtRF02D-VTWEG").Text = "10"
objSess.findById("wnd[0]/usr/ctxtRF02D-SPART").Text = "00"
objSess.findById("wnd[0]").sendVKey 0
' New code here:
' Not sure of and/or rules?
If objSess.findById("wnd[0]/usr/ctxtKNA1-AUFSD").Text = cBlockText _
Or objSess.findById("wnd[0]/usr/ctxtKNVV-AUFSD").Text = cBlockText Then
objSheet.Cells(iRow, 5) = "Unable to block: Dispute with customer"
objSess.findById("wnd[0]/tbar[0]/okcd").Text = "/n"
objSess.findById("wnd[0]").sendVKey 0
Else
objSess.findById("wnd[0]/usr/ctxtKNA1-AUFSD").Text = ""
objSess.findById("wnd[0]/usr/ctxtKNVV-AUFSD").Text = ""
objSess.findById("wnd[0]/tbar[0]/btn[11]").press
objSess.findById("wnd[0]").sendVKey 12
objSheet.Cells(iRow, 5) = objSBar.Text
' Update the Status to "Completed" and exit
objSheet.Cells(iRow, 2) = 2
End If
Related
I've got a problem with a VBA SAP script. I have data in excel and I want to execute the transaction code IW41 by copying and pasting data from Excel. I have all of the data like dates, number of orders, who did it etc in Excel and I want to automate it. I did a loop for variable i but I get an error and I cannot fix it.
Error :
Run-time error '619': Application-defined or object-defined error
Code provided below.
Can you give me some pro tips or help me fix it?
Screen in IW41 where the error occurs:
Code :
Sub ConfirmPM_Nots()
SystemName = "CCP" 'change as needed or use a variable
Transaction = "SESSION_MANAGER" 'change as needed or use a variable
On Error GoTo ErrorHandler:
If Not IsObject(Sap_Applic) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Sap_Applic = SapGuiAuto.GetScriptingEngine
End If
On Error GoTo 0
koniec:
qConnections = Sap_Applic.Connections.Count
If qConnections = 0 Then
MsgBox "No connection to SAP"
End
End If
bSession = False
For iConnectionCounter = 0 To qConnections - 1
Set Connection = Sap_Applic.Children(Int(iConnectionCounter))
If Not Connection.Description = "" Then
qSessions = Connection.Children.Count
For iSessionCounter = 0 To qSessions - 1
Set session = Connection.Children(Int(iSessionCounter))
If session.info.SystemName <> SystemName Then Exit For
If session.info.Transaction = Transaction Then
bSession = True
Exit For
End If
Next
End If
If bSession Then Exit For
Next
If Not bSession Then
MsgBox SystemName & " not available or free session not available"
End
End If
Do
i = 1
session.findById("wnd[0]").resizeWorkingPane 128, 37, False
session.findById("wnd[0]/tbar[0]/okcd").Text = "iw41"
session.findById("wnd[0]").sendVKey 0
Order = Cells(i, 1)
b = Cells(i, 2)
c = Cells(i, 3)
d = Cells(i, 4)
session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").Text = Order
session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").caretPosition = 7
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").SetFocus
session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").caretPosition = 2
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/chkAFRUD-AUERU").Selected = True
session.findById("wnd[0]/usr/chkAFRUD-LEKNW").Selected = True
session.findById("wnd[0]/usr/ctxtAFRUD-ISDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-IDAUR").Text = b
session.findById("wnd[0]/usr/ctxtAFRUD-IEDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").Text = d
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").SetFocus
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").caretPosition = 10
session.findById("wnd[0]/tbar[0]/btn[11]").press
i = i + 1
Loop
Exit Sub
ErrorHandler:
MsgBox "No connection to SAP"
End
End Sub
"iw41" (from session.findById("wnd[0]/tbar[0]/okcd").Text = "iw41") only works if the current screen is the start menu. OK-Code "/niw41" will always work! So this code might work (untested):
Do
i = 1
' session.findById("wnd[0]").resizeWorkingPane 128, 37, False
' iw41 only works in the start menu. OK-Code /niw41 will always work!
session.findById("wnd[0]/tbar[0]/okcd").Text = "/niw41"
session.findById("wnd[0]").sendVKey 0
Order = Cells(i, 1)
b = Cells(i, 2).value
c = Cells(i, 3).value
d = Cells(i, 4).value
session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").Text = Order
' session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").caretPosition = 7
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").SetFocus
' session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").caretPosition = 2
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/chkAFRUD-AUERU").Selected = True
session.findById("wnd[0]/usr/chkAFRUD-LEKNW").Selected = True
session.findById("wnd[0]/usr/ctxtAFRUD-ISDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-IDAUR").Text = b
session.findById("wnd[0]/usr/ctxtAFRUD-IEDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").Text = d
' session.findById("wnd[0]/usr/txtAFRUD-LTXA1").SetFocus
' session.findById("wnd[0]/usr/txtAFRUD-LTXA1").caretPosition = 10
session.findById("wnd[0]/tbar[0]/btn[11]").press
i = i + 1
Loop
I also commented the lines with resizeWorkingPane, caretPosition and SetFocus because this is usually not needed. resizeWorkingPane will resize the SAPGUI screen and caretPosition is the position of a cursor within a textbox. Sometimes needed if you want to replace text for example. But in this case certainly not.
NOTE: Your need to exit our Do Loop and place the record pointer i out of the loop. Otherwise i = 1. To exit the loop I often use first blank cell value.
This way you can set the first record to start with, and in most cases i=2 as most sheets have used first row as headers.
i = 1
Do Until Cells(i, 1) = ""
' code
i = i + 1
Loop
When I debugged the code to use it in my own update of Equipment in SAP, I found that our SAP does not have Connection.Description so I just pick up the first session like this.
This worked just fine for me:
Sub SetEQLocations()
' Script written by Svein Aren Hylland 02.12.2022
' Use of VBScript recording from SAP to work with SAP transaction IE02 - Change Equipment.
' The sub will transfere new Location data found in sheet on all visible rows with filter and headers in first row.
' Code will show progress in first column while it updates each EQ in SAP.
'
Sv = MsgBox("This routine work towards SAP IE02 from row 2 - and will update all EQ locations as shown in this sheet.", vbOKCancel)
If Sv = vbCancel Then Exit Sub
SystemName = "KO3" 'change as needed or use a variable
Transaction = "SESSION_MANAGER" 'change as needed or use a variable
On Error GoTo ErrorHandler:
If Not IsObject(Sap_Applic) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Sap_Applic = SapGuiAuto.GetScriptingEngine
End If
On Error GoTo 0
koniec:
qConnections = Sap_Applic.Connections.Count
If qConnections = 0 Then
MsgBox "No connection to SAP"
End
End If
'MsgBox Sap_Applic.Children(0).info.SystemName
bSession = False
For iConnectionCounter = 0 To qConnections - 1
Set Connection = Sap_Applic.Children(Int(iConnectionCounter))
'MsgBox Connection.Description
'If Not Connection.Description = "" Then
qSessions = Connection.Children.Count
For iSessionCounter = 0 To qSessions - 1
Set Session = Connection.Children(Int(iSessionCounter))
'MsgBox Session.info.SystemName
If Session.info.SystemName <> SystemName Then Exit For
If Session.info.Transaction = Transaction Then
bSession = True
Exit For
End If
Next
'End If
If bSession Then Exit For
Next
If Not bSession Then
MsgBox SystemName & " not available or free session not available"
End
End If
'Stop
Session.findById("wnd[0]").resizeWorkingPane 154, 24, False
Session.findById("wnd[0]/tbar[0]/okcd").Text = "ie02"
Session.findById("wnd[0]").sendVKey 0
i = 2
Do Until Cells(i, 1) = ""
If Cells(i, 1).Rows.Hidden = False Then
EQ = Cells(i, 1)
' Display progress
Cells(i, 1).Select
Cells(i, 1).Interior.Color = vbYellow
' Get data from sheet to be poulated in SAP fields
CostCenter = Cells(i, 14) ' Organization/Cost Center
MainWC = Cells(i, 15) ' Organization/Main Work Center
LocWorkCenter = Cells(i, 16) ' Location/Work Center
LocRoom = Cells(i, 17) ' Location/Room
Session.findById("wnd[0]/usr/ctxtRM63E-EQUNR").Text = EQ
Session.findById("wnd[0]/tbar[0]/btn[0]").press
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02").Select
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/txtITOB-MSGRP").Text = LocRoom
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/txtITOB-MSGRP").SetFocus
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/txtITOB-MSGRP").caretPosition = 6
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOBATTR-ARBPL").Text = LocWorkCenter
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOBATTR-ARBPL").SetFocus
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\02/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1050/ctxtITOBATTR-ARBPL").caretPosition = 6
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03").Select
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102A:SAPLITO0:1052/ctxtITOB-KOSTL").Text = CostCenter
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102B:SAPLITO0:1062/ctxtITOBATTR-GEWRK").Text = MainWC
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102B:SAPLITO0:1062/ctxtITOBATTR-GEWRK").SetFocus
Session.findById("wnd[0]/usr/tabsTABSTRIP/tabpT\03/ssubSUB_DATA:SAPLITO0:0102/subSUB_0102B:SAPLITO0:1062/ctxtITOBATTR-GEWRK").caretPosition = 6
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]/tbar[0]/btn[11]").press
End If
i = i + 1
Loop
Exit Sub
ErrorHandler:
MsgBox "No connection to SAP"
End
End Sub
I'm trying to use VBA code to extract data from a master Excel sheet into a Word document that will have a drop down list of customers at the start, and then have certain parts of the rest of it change to data dependent on that customer.
I found an example one that I copied and altered to suit my needs that is working (sort of), but I don't understand enough about why it works to get it right. When I started making it I only had data on one customer so used that info to test it as I went. However, when I filled in some data for other customers I found that it didn't work for them, only the first one. I also noticed that when I added another column to the Excel sheet, it tells me the subscript is out of range. That's lead me to believe that the code is getting the data from an out-of-date Excel sheet, and therefore not including the updated version, despite my best efforts to get it to use a new one. I hope this could be helped with some minor tweaking to my code where perhaps there is a specified range of columns that I've exceeded.
The code I used:
Option Explicit
Private Sub Document_ContentControlOnExit(ByVal oCC As ContentControl, Cancel As Boolean)
Dim arrData() As String
Dim strData As String
Dim lngIndex As Long
Select Case oCC.Title
Case "CC Conditional Dropdown List"
With oCC
If Not .ShowingPlaceholderText Then
For lngIndex = 1 To .DropdownListEntries.Count
If .Range.Text = .DropdownListEntries.Item(lngIndex) Then
strData = .DropdownListEntries.Item(lngIndex).Value
.Type = wdContentControlText
.Range.Text = strData
.Type = wdContentControlDropdownList
Exit For
End If
Next lngIndex
End If
End With
Case "Name"
If Not oCC.ShowingPlaceholderText Then
For lngIndex = 1 To oCC.DropdownListEntries.Count
If oCC.Range.Text = oCC.DropdownListEntries.Item(lngIndex) Then
arrData = Split(oCC.DropdownListEntries.Item(lngIndex).Value, "|")
Exit For
End If
Next lngIndex
With oCC
.Type = wdContentControlText
.Range.Text = arrData(0)
.Type = wdContentControlDropdownList
End With
ActiveDocument.SelectContentControlsByTag("AM").Item(1).Range.Text = Replace(arrData(1), "~", Chr(11))
ActiveDocument.SelectContentControlsByTag("CSA").Item(1).Range.Text = arrData(2)
ActiveDocument.SelectContentControlsByTag("Contract").Item(1).Range.Text = arrData(3)
ActiveDocument.SelectContentControlsByTag("Renewal").Item(1).Range.Text = arrData(4)
ActiveDocument.SelectContentControlsByTag("CurrentHR").Item(1).Range.Text = arrData(5)
ActiveDocument.SelectContentControlsByTag("RUG").Item(1).Range.Text = arrData(6)
ActiveDocument.SelectContentControlsByTag("eRMI").Item(1).Range.Text = arrData(7)
ActiveDocument.SelectContentControlsByTag("PurchasedHRSCBS").Item(1).Range.Text = arrData(8)
ActiveDocument.SelectContentControlsByTag("PurchasedMODAMEJpeRMALOD").Item(1).Range.Text = arrData(9)
ActiveDocument.SelectContentControlsByTag("ActualHRSCBS").Item(1).Range.Text = arrData(10)
ActiveDocument.SelectContentControlsByTag("ActualMODAM").Item(1).Range.Text = arrData(11)
ActiveDocument.SelectContentControlsByTag("ActualER").Item(1).Range.Text = arrData(12)
ActiveDocument.SelectContentControlsByTag("ActualEJp").Item(1).Range.Text = arrData(13)
ActiveDocument.SelectContentControlsByTag("ActualMedApp").Item(1).Range.Text = arrData(14)
ActiveDocument.SelectContentControlsByTag("ActualLoD").Item(1).Range.Text = arrData(15)
ActiveDocument.SelectContentControlsByTag("ERattainmentCons").Item(1).Range.Text = arrData(16)
ActiveDocument.SelectContentControlsByTag("ERattainmentnon-Cons").Item(1).Range.Text = arrData(17)
ActiveDocument.SelectContentControlsByTag("ERattainmentNwBN").Item(1).Range.Text = arrData(18)
ActiveDocument.SelectContentControlsByTag("ERattainmentWBN").Item(1).Range.Text = arrData(19)
ActiveDocument.SelectContentControlsByTag("ERattainmentAHPs").Item(1).Range.Text = arrData(20)
ActiveDocument.SelectContentControlsByTag("ERattainmentPharm").Item(1).Range.Text = arrData(21)
ActiveDocument.SelectContentControlsByTag("eJPAttainmentCons").Item(1).Range.Text = arrData(22)
ActiveDocument.SelectContentControlsByTag("eJPAttainmentNonCons").Item(1).Range.Text = arrData(23)
ActiveDocument.SelectContentControlsByTag("eJPAttainmentNWBN").Item(1).Range.Text = arrData(24)
ActiveDocument.SelectContentControlsByTag("eJPAttainmentAHPs").Item(1).Range.Text = arrData(25)
ActiveDocument.SelectContentControlsByTag("eJPAttainmentPharma").Item(1).Range.Text = arrData(26)
ActiveDocument.SelectContentControlsByTag("AcademyHRPropSent").Item(1).Range.Text = arrData(27)
ActiveDocument.SelectContentControlsByTag("AcademyHmPropSent").Item(1).Range.Text = arrData(28)
ActiveDocument.SelectContentControlsByTag("AcademyHRPropReturned").Item(1).Range.Text = arrData(29)
ActiveDocument.SelectContentControlsByTag("AcademyHMPropReturned").Item(1).Range.Text = arrData(30)
ActiveDocument.SelectContentControlsByTag("AcademyHRcourses").Item(1).Range.Text = arrData(31)
ActiveDocument.SelectContentControlsByTag("AcademyHMcourses").Item(1).Range.Text = arrData(32)
ActiveDocument.SelectContentControlsByTag("AcademyHREntit").Item(1).Range.Text = arrData(33)
ActiveDocument.SelectContentControlsByTag("AcademyHMEntit").Item(1).Range.Text = arrData(34)
Else
ActiveDocument.SelectContentControlsByTag("AM").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("CSA").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("Contract").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("Renewal").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTitle("CurrentHR").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("RUG").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("eRMI").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("PurchasedHRSCBS").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("PurchasedMODAMEJpeRMALOD").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("ActualHRSCBS").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("ActualMODAM").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("ActualER").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("ActualEJp").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("ActualMedApp").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("ActualLoD").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("ERattainmentCons").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("ERattainmentnon-Cons").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("ERattainmentNwBN").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("ERattainmentWBN").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("ERattainmentAHPs").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("ERattainmentPharm").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("eJPAttainmentCons").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("eJPAttainmentNonCons").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("eJPAttainmentNWBN").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("eJPAttainmentAHPs").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("eJPAttainmentPharma").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("AcademyHRPropSent").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("AcademyHmPropSent").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("AcademyHRPropReturned").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("AcademyHMPropReturned").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("AcademyHRcourses").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("AcademyHMcourses").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("AcademyHMEntit").Item(1).Range.Text = vbNullString
ActiveDocument.SelectContentControlsByTag("AcademyHREntit").Item(1).Range.Text = vbNullString
End If
Case Else
End Select
lbl_Exit:
Exit Sub
End Sub
Sub Document_Open()
Dim strWorkbook As String, strColumnData As String
Dim lngIndex As Long, lngRowIndex As Long, lngColIndex As Long
Dim arrData As Variant
Dim oCC As ContentControl, oFF As FormField, oCtrl As Control
Dim bReprotect As Boolean
Application.ScreenUpdating = False
strWorkbook = ThisDocument.Path & "\Excel Data Store.xlsx"
If Dir(strWorkbook) = "" Then
MsgBox "Cannot find the designated workbook: " & strWorkbook, vbExclamation
Exit Sub
End If
arrData = fcnExcelDataToArray(strWorkbook, "Simple List")
Set oCC = ActiveDocument.SelectContentControlsByTitle("CC Dropdown List").Item(1)
If oCC.DropdownListEntries.Item(1).Value = vbNullString Then
For lngIndex = oCC.DropdownListEntries.Count To 2 Step -1
oCC.DropdownListEntries.Item(lngIndex).Delete
Next lngIndex
Else
oCC.DropdownListEntries.Clear
End If
For lngRowIndex = 0 To UBound(arrData, 2)
oCC.DropdownListEntries.Add arrData(0, lngRowIndex), arrData(0, lngRowIndex)
Next
Set oFF = ActiveDocument.FormFields("Formfield_DD_List")
bReprotect = False
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
bReprotect = True
End If
oFF.DropDown.ListEntries.Clear
For lngRowIndex = 0 To UBound(arrData, 2)
oFF.DropDown.ListEntries.Add arrData(0, lngRowIndex)
Next
If bReprotect Then ActiveDocument.Protect wdAllowOnlyFormFields, True
With ActiveX_ComboBox
.Clear
.AddItem " "
For lngRowIndex = 0 To UBound(arrData, 2)
.AddItem arrData(0, lngRowIndex)
Next
.MatchRequired = True
.Style = fmStyleDropDownList
End With
arrData = fcnExcelDataToArray(strWorkbook, "Simple Conditional List")
Set oCC = ActiveDocument.SelectContentControlsByTitle("CC Conditional Dropdown List").Item(1)
If oCC.DropdownListEntries.Item(1).Value = vbNullString Then
For lngIndex = oCC.DropdownListEntries.Count To 2 Step -1
oCC.DropdownListEntries.Item(lngIndex).Delete
Next lngIndex
Else
oCC.DropdownListEntries.Clear
End If
For lngIndex = 0 To UBound(arrData, 2)
oCC.DropdownListEntries.Add arrData(0, lngIndex), arrData(1, lngIndex)
Next
arrData = fcnExcelDataToArray(strWorkbook, "Advanced Conditional List")
Set oCC = ActiveDocument.SelectContentControlsByTitle("Name").Item(1)
If oCC.DropdownListEntries.Item(1).Value = vbNullString Then
For lngIndex = oCC.DropdownListEntries.Count To 2 Step -1
oCC.DropdownListEntries.Item(lngIndex).Delete
Next lngIndex
Else
oCC.DropdownListEntries.Clear
End If
For lngRowIndex = 0 To UBound(arrData, 2)
strColumnData = vbNullString
For lngColIndex = 1 To UBound(arrData, 1)
strColumnData = strColumnData & "|" & arrData(lngColIndex, lngRowIndex)
Next lngColIndex
strColumnData = Right(strColumnData, Len(strColumnData) - 1)
oCC.DropdownListEntries.Add arrData(0, lngRowIndex), strColumnData
Next
lbl_Exit:
Application.ScreenUpdating = True
Exit Sub
End Sub
Private Function fcnExcelDataToArray(strWorkbook As String, _
Optional strRange As String = "Sheet1", _
Optional bIsSheet As Boolean = True, _
Optional bHeaderRow As Boolean = True) As Variant
Dim oRS As Object, oConn As Object
Dim lngRows As Long
Dim strHeaderYES_NO As String
strHeaderYES_NO = "YES"
If Not bHeaderRow Then strHeaderYES_NO = "NO"
If bIsSheet Then strRange = strRange & "$]" Else strRange = strRange & "]"
Set oConn = CreateObject("ADODB.Connection")
oConn.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & strWorkbook & ";" & _
"Extended Properties=""Excel 12.0 Xml;HDR=" & strHeaderYES_NO & """;"
Set oRS = CreateObject("ADODB.Recordset")
oRS.Open "SELECT * FROM [" & strRange, oConn, 2, 1
With oRS
.MoveLast
lngRows = .RecordCount
.MoveFirst
End With
fcnExcelDataToArray = oRS.GetRows(lngRows)
lbl_Exit:
If oRS.State = 1 Then oRS.Close
Set oRS = Nothing
If oConn.State = 1 Then oConn.Close
Set oConn = Nothing
Exit Function
End Function
I expect data dependant on a customer selected from a dropdown list to be extracted from an Excel document and filled into a Word document.
Am getting the error code:
Run-time error '9': Subscript out of range
on the line:
ActiveDocument.SelectContentControlsByTag("AcademyHMEntit").Item(1).Range.Text = arrData(34)
after adding in a column to the master Excel sheet so believe the VBA is trying to use an out of date version of the master Excel sheet.
Screenshot of the content controls below with the customer name being the dropdown list and the bits in yellow as content dependant on it
I'm using Excel 365 in Windows and I'm trying to have the contents of certain cells copied to the sheet header (or footer) when the cell contents change. For example, I'm creating a series of data sheets and on page one I have a cell where the user types in the purchase order (PO) used by the customer. I would like to have the user enter the PO number once and then have the PO number placed in all subsequent footers. When the stack of data sheets is printed the PO (and other key info) will be on all pages.
I know that Excel does not do this naturally and I've found some rather ugly macro and VB code to do this programatically via Google but I had trouble making it work. so....I'm hoping for some simpler magic.
Private HeaderSave() As Variant
Private HeaderChanged() As Variant
Public Sub SaveAndSetHeaders()
Dim Index As Long
' Save and set header and footer text Application.ScreenUpdating = False
ReDim HeaderSave(1 To ThisWorkbook.Sheets.Count) ReDim HeaderChanged(1 To
ThisWorkbook.Sheets.Count, 0 To 5)'
For Index = 1 To ThisWorkbook.Sheets.Count
With ThisWorkbook.Sheets(Index).PageSetup
HeaderSave(Index) = Array(.LeftHeader, .CenterHeader, .RightHeader,
.LeftFooter, .CenterFooter, .RightFooter)
If InStr(HeaderSave(Index)(0), "^[Cell:") > 0 Then
.LeftHeader = SubstituteCellValues(ThisWorkbook.Sheets(Index),
HeaderSave(Index)(0))
HeaderChanged(Index, 0) = True
End If
If InStr(HeaderSave(Index)(1), "^[Cell:") > 0 Then
.CenterHeader = SubstituteCellValues(ThisWorkbook.Sheets(Index),
HeaderSave(Index)(1))
HeaderChanged(Index, 1) = True
End If
If InStr(HeaderSave(Index)(2), "^[Cell:") > 0 Then
.RightHeader = SubstituteCellValues(ThisWorkbook.Sheets(Index),
HeaderSave(Index)(2))
HeaderChanged(Index, 2) = True
End If
If InStr(HeaderSave(Index)(3), "^[Cell:") > 0 Then
.LeftFooter = SubstituteCellValues(ThisWorkbook.Sheets(Index),
HeaderSave(Index)(3))
HeaderChanged(Index, 3) = True
End If
If InStr(HeaderSave(Index)(4), "^[Cell:") > 0 Then
.CenterFooter = SubstituteCellValues(ThisWorkbook.Sheets(Index),
HeaderSave(Index)(4))
HeaderChanged(Index, 4) = True
End If
If InStr(HeaderSave(Index)(5), "^[Cell:") > 0 Then
.RightFooter = SubstituteCellValues(ThisWorkbook.Sheets(Index),
HeaderSave(Index)(5))
HeaderChanged(Index, 5) = True
End If
End With
Next Index
Application.ScreenUpdating = True
End Sub
Public Sub RestoreHeaders()
Dim Index As Long
' Restore header and footer text'
Application.ScreenUpdating = False
For Index = 1 To ThisWorkbook.Sheets.Count
With ThisWorkbook.Sheets(Index).PageSetup
If HeaderChanged(Index, 0) Then .LeftHeader = HeaderSave(Index)(0)
If HeaderChanged(Index, 1) Then .CenterHeader = HeaderSave(Index)(1)
If HeaderChanged(Index, 2) Then .RightHeader = HeaderSave(Index)(2)
If HeaderChanged(Index, 3) Then .LeftFooter = HeaderSave(Index)(3)
If HeaderChanged(Index, 4) Then .CenterFooter = HeaderSave(Index)(4)
If HeaderChanged(Index, 5) Then .RightFooter = HeaderSave(Index)(5)
End With
Next Index
Application.ScreenUpdating = True
End Sub
Private Function SubstituteCellValues( _
ByVal FocusSheet As Worksheet, _
ByVal Text As String _
) As String
' Look for the text "^[Cell:A1]" and replace it with the cell's value. The
cell reference can be any valid cell reference with or without a sheet
name. If no sheet name is included the sheet for which the header or
footer text is defined is assumed.'
Dim StartPos As Long
Dim EndPos As Long
Dim FindText As String
Dim ReplaceText As String
Dim CellReference As String
Do
StartPos = InStr(Text, "^[Cell:")
If StartPos > 0 Then
EndPos = InStr(StartPos, Text, "]")
If EndPos = 0 Then Exit Do
FindText = Mid(Text, StartPos, EndPos - StartPos + 1)
CellReference = Mid(FindText, 8, Len(FindText) - 8)
If InStr(CellReference, "!") = 0 Then
CellReference = "'" & FocusSheet.Name & "'!" & CellReference
End If
On Error Resume Next
ReplaceText = Range(CellReference).Value
On Error GoTo 0
Text = Replace(Text, FindText, ReplaceText)
Else
Exit Do
End If
Loop
SubstituteCellValues = Text
End Function
Private Sub Workbook_BeforePrint(Cancel As Boolean)
SaveAndSetHeaders
Application.OnTime Now, "ThisWorkbook.Workbook_AfterPrint"
End Sub
Private Sub Workbook_AfterPrint()
RestoreHeaders
End Sub
I want to fetch all the Outlook inbox emails into an Excel sheet with additional columns having the data like This mail was replied on or This mail was forwarded to
Here is the code that I have done so far
Dim Folder As Outlook.MAPIFolder
Dim iRow As Integer
MailBoxName = 'Mailbox Name Goes Here
Pst_Folder_Name = "Inbox"
Set Folder = Outlook.Session.PickFolder 'Folders(MailBoxName).Folders(Pst_Folder_Name)
If Folder = "" Then
MsgBox "Invalid Data in Input"
GoTo end_lbl1:
End If
Folder.Items.Sort "[ReceivedTime]", False
LimitDateTimeValue = 'Date Limit
CellNo = 2
For iRow = 1 To Folder.Items.Count
On Error Resume Next
If Folder.Items.Item(iRow).ReceivedTime > LimitDateTimeValue Then
'CellNo = 2
On Error Resume Next
ThisWorkbook.Sheets("Inbox").Range("A2").Select
FullSubjectLine = Folder.Items.Item(iRow).Subject
If InStr(1, FullSubjectLine, "FE:", vbTextCompare) > 0 Or InStr(1, FullSubjectLine, "FW:", vbTextCompare) > 0 Or InStr(1, FullSubjectLine, "RE:", vbTextCompare) Then
FilteredSubjectLine = Mid(FullSubjectLine, 5)
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 2) = FilteredSubjectLine
Else
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 2) = Folder.Items.Item(iRow).Subject
End If
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 4) = Left(Folder.Items.Item(iRow).Body, 1024)
If Folder.Items.Item(iRow).UnRead Then
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 6) = "UnRead"
Else
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 6) = "Read"
End If
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 1) = Folder.Items.Item(iRow).SenderName
ThisWorkbook.Sheets("Inbox").Cells(CellNo, 3) = Folder.Items.Item(iRow).ReceivedTime
CellNo = CellNo + 1
End If
Next iRow
The code is extremely inefficient, this is multiple dot notation taken to its extreme. Cache the Items collection before entering the loop and retrieve the item only once on each iteration - otherwise OOM will have to return a brand new COM object for each ".".
On Error Resume Next
set vItems = Folder.Items
For iRow = 1 To vItems.Count
set vItem = vItems.Item(iRow)
FullSubjectLine = vItem.Subject
lastVerbExecuted = vItem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x10810003")
if Err.Number <> 0 Then
lastVerbExecuted = 0
Err.Clear
End If
...
next
In my organization we have one old project/application which was build on Visual Basic 6.0
In that application we have export to Excel "button" where data gets populated into different tabs in spreadsheet with click. It was working very well with Excel 2010 and later until we moved to EXCEL 2013.
Issue: We need data to get exported into 2 tabs in excel 2013 whereas its coming in 1 tab only. I tried using package and deployment wizard and all possible help available. So far no luck. Please let me know if you have any questions or if I am not enough clear. Please find below my code.
Dim uprev As Integer
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlsheet2 As Excel.Worksheet
Dim ExcelWasNotRunning As Boolean ' Flag for final release.
Dim n As Integer
Dim n1 As Integer
Dim n2 As Integer
Dim i As Integer
Dim lastrevdate As String
Dim lastrevrow As Integer
Dim lastrow As Integer
Dim previouspcno As Integer
Dim xlcol As String
Dim j As Integer
Dim k As Integer
Dim dc As Adodc
Dim mrc As Recordset
Dim qpa As New QPArray
Dim Found As Long
Dim StartInd As Long
Dim bFound As Boolean
Dim crlf As String
On Error GoTo errorhandler1
crlf = Chr(13) & Chr(10)
ReDim qs(10) As String
ReDim q(10) As Integer
ReDim hdr(15) As Integer
ReDim rev(10, 0) As String
ReDim part(0) As String
ReDim sl(nof) As String
ReDim cmpsql2(0) As String
ReDim deletedfromsql(3, 0) As String
Dim doThis As Integer
Dim iReturn As Integer
Dim revlev As String
Dim Date_Engr As String
Dim Date_Checker As String
'On Error Resume Next ' Defer error trapping.
'Removed, not checking to see if excel is open properly
'Bert - 6/5/07
'Set xlApp = GetObject(, "Excel.Application")
'If Err.Number <> 0 Then
' ExcelWasNotRunning = True
'Else
' MsgBox ("Please Close Excel before continuing")
' Exit Sub
'End If
Err.Clear ' Clear Err object in case error occurred.
iReturn = MsgBox("Please Close ALL Excel applications before continuing", vbOKOnly, "WARNING")
ExcelWasNotRunning = True
'fixwidth
Screen.MousePointer = vbHourglass
'DetectExcel
Set xlApp = Excel.Application
'path(8) = "C:\SwitchGear\Files1\eng_prod\Jobs\cs01157\medt\"
If Dir(Defaults.medt & "\" & cs & sos & "mbom.xls", vbNormal) <> "" Then
mbomflag = 1
FileCopy Defaults.medt & "\" & cs & sos & "mbom.xls", Defaults.medt & "\" & cs & sos & "mbom.bak"
Set xlBook = GetObject(Defaults.medt & "\" & cs & sos & "mbom.xls")
Set xlSheet = xlBook.Worksheets(1)
Set xlsheet2 = xlBook.Worksheets(2)
Do
qs(1) = "1. Do not list changes on rev sheet" & crlf
qs(1) = qs(1) & "2. list changes on rev sheet but do not increase rev level" & crlf
qs(1) = qs(1) & "3. list changes on rev sheet and increase rev level"
qs(0) = InputBox(qs(1))
If qs(0) = "" Then Exit Sub
Loop Until qs(0) > "0" And qs(0) < "4"
If qs(0) = "3" Then ' up the revision
uprev = 2
revlev = xlsheet2.Cells(5, 3) + 1
Date_Engr = Date
Date_Checker = Date
Else
uprev = 1
revlev = xlsheet2.Cells(5, 3)
Date_Engr = xlSheet.Cells(16, 2) ' get the old rev number
Date_Checker = xlSheet.Cells(16, 3)
End If
lastrow = xlSheet.Cells.Range("E20").End(xlDown).Row
ReDim cmpxl2(0) As String
ReDim cmpxl3(0) As String
ReDim cmpxl4(0) As String
n = 0
For i = 20 To lastrow
If xlSheet.Cells(i, 2) <> "" Then
n = n + 1
ReDim Preserve cmpxl2(n) As String
ReDim Preserve cmpxl3(n) As String
ReDim Preserve cmpxl4(n) As String
cmpxl2(n) = xlSheet.Cells(i, 2) & " " & Format(i)
cmpxl3(n) = xlSheet.Cells(i, 3)
cmpxl4(n) = xlSheet.Cells(i, 4)
End If
Next i
n1records = Adodc1.Recordset.RecordCount
'If n > n1records Then 'it's been deleted from sql so find the part and add to xl revision sheet
n1 = 0
ReDim cmpsql2(n1records) As String
With Adodc1.Recordset
For i = 1 To n1records
If i = 1 Then
Adodc1.Recordset.MoveFirst
Else
Adodc1.Recordset.MoveNext
End If
cmpsql2(i) = !pcno
Next i
End With
For i = 1 To n
bFound = qpa.Find(cmpsql2(), Left$(cmpxl2(i), 4), Found, , 1)
If bFound = False Then
q(1) = Val(Mid$(cmpxl2(i), 6))
n1 = n1 + 1
ReDim Preserve deletedfromsql(3, n1)
deletedfromsql(1, n1) = xlSheet.Cells(q(1), 2)
deletedfromsql(2, n1) = xlSheet.Cells(q(1), 3)
deletedfromsql(3, n1) = xlSheet.Cells(q(1), 4)
End If
Next i
'End If
n = 0
Do
n = n + 1
If xlsheet2.Cells(n + 13, 1) > " " Then
ReDim Preserve rev(10, n)
ReDim Preserve part(n)
'part(n) = xlSheet.Cells(n + 13, 3) & "*" & xlSheet.Cells(n + 13, 1)
If xlsheet2.Cells(n + 13, > CDate(lastrevdate) Then
lastrevdate = xlsheet2.Cells(n + 13, 8-)
End If
For i = 1 To 10
rev(i, n) = xlsheet2.Cells(n + 13, i)
Next i
Else
Exit Do
End If
Loop
If engr = "" Then
engr = xlSheet.Cells(14, 2)
chcked = xlSheet.Cells(14, 3)
End If
Else
mbomflag = 0
revlev = 0
If engr = "" Then
engr = UCase$(InputBox("Enter Mechanical drafter's Initials:", "Enter Initials"))
'If engr = "" Then Exit Sub
chcked = UCase$(InputBox("Enter Checker's Initials:", "Enter Initials"))
'If chcked = "" Then Exit Sub
End If
End If
'Set xlBook = GetObject(path(2) & "vb\sql\ebomtemplate.xls")
Set xlBook = GetObject(Defaults.ApplicationPath & "\mbomTemplate.xls")
Set xlSheet = xlBook.Worksheets(1)
Set xlsheet2 = xlBook.Worksheets(2)
If revlev = 0 Then
xlsheet2.Cells(14, 8= Date
End If
'xlSheet.PageSetup.Zoom = 50
If UBound(rev, 2) > 0 Then
lastrevrow = UBound(rev, 2) + 13
For i = 14 To UBound(rev, 2) + 13
For j = 1 To 10
xlsheet2.Cells(i, j) = rev(j, i - 13)
Next j
Next i
Else
lastrevrow = 13
End If
'If uprev = 1 Then
' xlBook.Application.Visible = True
' xlBook.Parent.Windows(2).Visible = True
' xlBook.Parent.Windows(2).Activate
' xlSheet.Activate
'bFound = bringwindowtotop(hwnd)
'xlBook.Sheets(1).Select
'ActiveSheet.Visible = True
'xlBook.Application.DoubleClick
'Else
xlBook.Application.Visible = True
xlBook.Parent.Windows(1).Visible = True
xlBook.Parent.Windows(1).Activate
xlSheet.Activate
'DetectExcel
'bFound = bringwindowtotop(hwnd)
'End If
'DetectVB
'Found = apiShowWindow(hwnd, SW_SHOWMINIMIZED)
'DetectExcel
'Found = apiShowWindow(hwnd, SW_SHOWMAXIMIZED)
Me.Visible = False
Screen.MousePointer = vbDefault
'If uprev = 1 Then
' xlBook.NewWindow.Activate
' With xlBook.NewWindow
' .ActiveSheet = 2
' .Zoom = 50
' End With
'End If
'xlBook.Application.Visible = True
'xlBook.Parent.Windows(1).Visible = True
'xlSheet.Activate
'qs(1) = "03040609121314151617181920212223242526272829303132333435"
cs = UCase$(cs)
sos = UCase$(sos)
xlSheet.Cells(10, 2) = cs & Left$(sos, 5)
If Val(framestr(0, 0, 15)) < 8 Then qs(1) = "2" Else qs(1) = "4"
xlSheet.Cells(10, 3) = "-" & Mid$(sos, 6, 1) & Right$(sos, 1) & "B" & qs(1) & "004"
xlSheet.Cells(12, 2) = Right$(sos, 3)
xlSheet.Cells(10, 6) = framestr(0, 0, 3)
'xlSheet.Cells(12, 3) = "0"
'xlSheet.Cells(16, 2) = Date
'xlSheet.Cells(16, 3) = Date
xlSheet.Cells(10, 4) = framestr(0, 0, 658) 'sold to
xlSheet.Cells(11, 4) = framestr(0, 0, 657)
xlSheet.Cells(12, 4) = framestr(0, 0, 656)
xlSheet.Cells(14, 2) = engr
xlSheet.Cells(14, 3) = chcked
xlSheet.Cells(14, 4) = framestr(0, 0, 655) 'for
xlSheet.Cells(14, 6) = framestr(0, 0, 661) 'purchase order
xlSheet.Cells(15, 4) = framestr(0, 0, 654)
xlSheet.Cells(16, 4) = framestr(0, 0, 653)
xlcol = "L M N O P Q R S T U V W X Y Z AAABACADAEAFAGAHAIAJ"
qs(1) = "L12:" & Trim$(Mid$(xlcol, (nof + 1) * 2 - 1, 2)) & "16"
xlSheet.Cells.Range(qs(1)).Value = " "
For i = 1 To nof
xlSheet.Cells(19, i + 11) = i
Next i
For i = 1 To nof + 1
qs(1) = Trim$(Mid$(xlcol, i * 2 - 1, 2)) & "12:" & Trim$(Mid$(xlcol, i * 2 - 1, 2)) & "16"
With xlSheet.Cells.Range(qs(1)).Borders(xlLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
Next i
qs(1) = Chr(76) & "12:" & Trim$(Mid$(xlcol, nof * 2 - 1, 2)) & "12"
With xlSheet.Cells.Range(qs(1)).Borders(xlTop)
'.LineStyle = xlContinuous
.Weight = xlMedium
End With
qs(1) = Chr(76) & "16:" & Trim$(Mid$(xlcol, nof * 2 - 1, 2)) & "16"
With xlSheet.Cells.Range(qs(1)).Borders(xlBottom)
'.LineStyle = xlContinuous
.Weight
I am aware VB 6 is outdated and not sure why they don't move to VB.NET. I would really appreciate if anyone can help. Thanks in advance :)
Your problem is nothing to do with VB6 being outdated. The problem is that this code is unrunnable. I can only make a guess that this is some hacked version based on the real running code. I will make some guesses based on approximately what this code should really look like. However, it would be a good idea to provide the actual code.
By "tabs", I take it you mean "worksheets". I am guessing that they are called "Sheet1" and "Sheet2". So basically, only "Sheet1" is actually getting re-populated. "Sheet2" remains as it previously looked.
I would suggest that you put a breakpoint on the line:
Set xlsheet2 = xlBook.Worksheets(2)
See whether xlsheet2.Cells(14,8) evaluates to the date you expect to see on that worksheet.
After stepping through this line, ensure that xlsheet2 actually points to the worksheet you expect it to. I would also put breakpoints on every line which reads or writes xlsheet2.Cells(x,y) evaluate it, and look at sheet2, ensuring that the value read or written back is correct.