I have a Excel to Word Macro specified as follows:
Sub CopyToWordDoc()
Dim objWord
Dim objDoc
Dim objSel
Dim sht As Worksheet
Dim p As Integer
Set objWord = CreateObject("Word.Application") 'open new word document
Set objDoc = objWord.Documents.Add
Set objSel = objWord.Selection
objWord.Visible = True
For x = 1 To Worksheets.Count - 1 'loop through data sheets and export contents to Word
On Error Resume Next
Set sht = Sheets("X" & x)
On Error GoTo 0
If sht Is Nothing Then Exit Sub
With sht
If x = 1 Then 'add version, date, userinfo, projectinfo etc. to first page of Word
objSel.Style = objDoc.Styles("Heading 1")
objSel.TypeText (Range("Client").Value2)
objSel.TypeParagraph
objSel.Style = objDoc.Styles("Heading 1")
objSel.TypeText ("Scope of Tax Due Diligence")
objSel.TypeParagraph
objSel.Style = objDoc.Styles("Normal")
objSel.TypeText ("Review Period: " & Range("Period").Value2)
objSel.TypeParagraph
If .Range("C3").Value2 = True Then 'check if Level 1 titel has to be added
objSel.Style = objDoc.Styles("Heading 2")
objSel.TypeText (.Range("B2").Value2)
objSel.TypeParagraph
Else
p = 1
End If
Else
If p = 1 And .Range("C3").Value2 = True Then
objSel.Style = objDoc.Styles("Heading 2")
objSel.TypeText (.Range("B2").Value2)
objSel.TypeParagraph
p = 0
ElseIf p = 0 And .Range("C3").Value2 = True Then
If .Range("B2").Value2 <> Sheets("X" & x - 1).Range("B2").Value2 Then
objSel.Style = objDoc.Styles("Heading 2")
objSel.TypeText (.Range("B2").Value2)
objSel.TypeParagraph
End If
ElseIf p = 0 And .Range("C3").Value2 = False Then
If .Range("B2").Value2 <> Sheets("X" & x - 1).Range("B2").Value2 Then p = 1
End If
End If
objWord.Run MacroName:="sdWordEngine.mBulletsAndNumbering.FormatBulletDefault"
If .Range("C3").Value2 = True Then 'add level 2 title
objSel.Style = objDoc.Styles("Heading 3")
objSel.TypeText (.Range("B3").Value2)
objSel.TypeParagraph
End If
objWord.Run MacroName:="sdWordEngine.mBulletsAndNumbering.FormatBulletDefault"
For y = 4 To Application.WorksheetFunction.CountA(.Range("B1:B50")) 'loop through data sheet and add info if in scope
If .Range("C" & y).Value2 = True Then
If .Range("A" & y).Value2 = 3 Then
objSel.Range.SetListLevel Level:=1
objSel.TypeText (.Range("B" & y).Value2)
objSel.TypeParagraph
Else
objSel.Range.SetListLevel Level:=2
objSel.TypeText (.Range("B" & y).Value2)
objSel.TypeParagraph
End If
End If
Next
End With
Next
objWord.Run MacroName:="sdWordEngine.mBulletsAndNumbering.FormatBulletDefault"
objSel.InsertBreak Type:=wdPageBreak
For x = 1 To Worksheets.Count - 1 'same as above but for info request instead
On Error Resume Next
Set sht = Sheets("X" & x)
On Error GoTo 0
If sht Is Nothing Then Exit Sub
With sht
If x = 1 Then
objSel.Style = objDoc.Styles("Heading 1")
objSel.TypeText ("Information Request for Tax Due Diligence")
objSel.TypeParagraph
If .Range("C3").Value2 = True Then
objSel.Style = objDoc.Styles("Heading 2")
objSel.TypeText (.Range("B2").Value2)
objSel.TypeParagraph
Else
p = 1
End If
Else
If p = 1 And .Range("C3").Value2 = True Then
objSel.Style = objDoc.Styles("Heading 2")
objSel.TypeText (.Range("B2").Value2)
objSel.TypeParagraph
p = 0
ElseIf p = 0 And .Range("C3").Value2 = True Then
If .Range("B2").Value2 <> Sheets("X" & x - 1).Range("B2").Value2 Then
objSel.Style = objDoc.Styles("Heading 2")
objSel.TypeText (.Range("B2").Value2)
objSel.TypeParagraph
End If
ElseIf p = 0 And .Range("C3").Value2 = False Then
If .Range("B2").Value2 <> Sheets("X" & x - 1).Range("B2").Value2 Then p = 1
End If
End If
objWord.Run MacroName:="sdWordEngine.mBulletsAndNumbering.FormatBulletDefault"
If .Range("C3").Value2 = True And Application.WorksheetFunction.CountIf(.Range("G2:G50"), True) <> 0 Then
objSel.Style = objDoc.Styles("Heading 3")
objSel.TypeText (.Range("B3").Value2)
objSel.TypeParagraph
End If
objWord.Run MacroName:="sdWordEngine.mBulletsAndNumbering.FormatBulletDefault"
For y = 2 To Application.WorksheetFunction.CountA(.Range("F1:F50"))
If .Range("C3").Value2 = True Then
If .Range("G" & y).Value2 = True Then
If .Range("E" & y).Value2 = 1 Then
objSel.Range.SetListLevel Level:=1
objSel.TypeText (.Range("F" & y).Value2)
objSel.TypeParagraph
Else
objSel.Range.SetListLevel Level:=2
objSel.TypeText (.Range("F" & y).Value2)
objSel.TypeParagraph
End If
End If
End If
Next
End With
Next
objSel.TypeBackspace
objSel.WholeStory
objSel.Font.Name = "Arial"
End Sub
In the Excelfile I have all the infos that are put together into a Word document:
Excel File picture
How can I make sure that all level 4 text, once generated in Word, has a text indent of about 2cm?
In advance, many thanks for your support!
So I put the following code in:
For y = 4 To Application.WorksheetFunction.CountA(.Range("B1:B50")) 'loop through data sheet and add info if in scope
If .Range("C" & y).Value2 = True Then
If .Range("A" & y).Value2 = 4 Then
objSel.Range.SetListLevel Level:=1
objSel.TypeText (.Range("B" & y).Value2)
objSel.TypeParagraph
objSel.Paragraphs.LeftIndent = 72
Else
objSel.Range.SetListLevel Level:=2
objSel.TypeText (.Range("B" & y).Value2)
objSel.TypeParagraph
End If
End If
When I compile the Word document it sets level 4 lines with left indent 72. However, only from the second level 4 line it starts formatting the lines that way. It always leaves out the first level 4 line (keeps it without left indent). Does someone know why that is? Many thanks for your help!
How about:
Selection.Paragraphs.LeftIndent = 72
Change the 72 to fit want you want.
Select the whole document, then put that line of code after it.
http://word.tips.net/T001468_Setting_the_Left_Indent_of_a_Paragraph_in_a_Macro.html
Related
I have below code.
In first loop the excel file is extracted from the SAP - loop with variable a.
In the second loop (loop with variable k) the invoice is extracted from SAP (takes the order number from earlier extracted excel file).
The number of the order is taken from the excel file and paste to order in SAP.
Sometimes it happens, that order is either not taken from excel or not paste in SAP and the field for the order is empty.
This situation try to generates all the orders for the Controlling Area, which is very time-consuming (in fact it lasts hours).
I tried to add this line of code before the paste to the order, but of no result
Application.Wait Now + TimeValue("0:00:05")
Have you met this in your coding and could help with this?
extracting the excel file from SAP
Sub invoice_extr()
'##########################
'zapisuje pliki xlsx ordery
'##########################
Application.ScreenUpdating = False
SheetSrc = "Input data"
On Error Resume Next
If Not IsObject(SAPApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
If Err.Number <> 0 Then Exit Sub
Set SAPApplication = SapGuiAuto.GetScriptingEngine
If Err.Number <> 0 Then Exit Sub
End If
If Not IsObject(Connection) Then
Set Connection = SAPApplication.Children(0)
If Err.Number <> 0 Then
MsgBox ("Please, open SAP!")
Exit Sub
Else
End If
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
On Error GoTo 0
Application.Wait (Now + TimeValue("0:00:01") / 1.5)
Dim a As Double
Dim last_row As Double
last_row = Cells(Rows.Count, 1).End(xlUp).Row
Range("b2:c" & last_row).Select
Selection.ClearContents
Dim path As String
path = Cells(2, 6)
For a = 2 To last_row
'###########################################################
'####SPRAWDZA CZY JEST JUZ PLIK XLSX O TAKIEJ NAZWIE########
'###########################################################
Dim objFSO_november1 As Object
Dim objFolder_november1 As Object
Dim objFile_november1 As Object
Dim objFile1_november1 As Object
Dim aa_november1 As Integer
Dim bb_november1 As Integer
Set objFSO_november1 = CreateObject("Scripting.FileSystemObject")
Set objFolder_november1 = objFSO_november1.GetFolder(path)
bb_november1 = 0
For Each objFile1_november1 In objFolder_november1.Files
bb_november1 = bb_november1 + 1
Next objFile1_november1
Dim myArray_november1() As Variant
ReDim Preserve myArray_november1(bb_november1, 1)
aa_november1 = 0
For Each objFile_november1 In objFolder_november1.Files
myArray_november1(aa_november1, 1) = objFile_november1.name
aa_november1 = aa_november1 + 1
Next objFile_november1
Dim aa As Double
Dim zz As Double
zz = 0
For aa = 0 To aa_november1 - 1
Dim how_many As Double
how_many = Len(Cells(a, 1))
'MsgBox (Cells(a, 1))
'MsgBox (Left(myArray_november1(aa, 1), how_many))
'MsgBox (aa)
If (Cells(a, 1) * 1) = (Left(myArray_november1(aa, 1), how_many) * 1) Then
Cells(a, 2) = "Done"
zz = zz + 1
'MsgBox (zz)
End If
If zz <> 0 Then
GoTo line1
End If
Next aa
Erase myArray_november1
If zz <> 0 Then
GoTo line1
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/Ns_alr_87013019"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/txt$6-KOKRS").Text = Cells(a, 4)
Workbooks("Saving_invoice.xlsm").Activate
session.findById("wnd[0]/usr/ctxt_6ORDGRP-LOW").Text = Cells(a, 1)
session.findById("wnd[0]/usr/ctxt_6ORDGRP-LOW").SetFocus
session.findById("wnd[0]/usr/ctxt_6ORDGRP-LOW").caretPosition = 6
session.findById("wnd[0]/tbar[1]/btn[8]").press
'On Error GoTo line1
On Error Resume Next
session.findById("wnd[0]/shellcont/shell/shellcont[2]/shell").hierarchyHeaderWidth = 453
session.findById("wnd[0]/usr/lbl[62,8]").SetFocus
session.findById("wnd[0]/usr/lbl[62,8]").caretPosition = 9
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[1]/usr/lbl[1,2]").SetFocus
session.findById("wnd[1]/usr/lbl[1,2]").caretPosition = 4
session.findById("wnd[1]").sendVKey 2
'################################################################
'#############WYBIERA LAYOUT /MACRO##############################
'################################################################
session.findById("wnd[0]/tbar[1]/btn[33]").press
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").currentCellRow = -1
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").selectColumn "VARIANT"
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").contextMenu
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").selectContextMenuItem "&FILTER"
session.findById("wnd[2]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").Text = "/MACRO"
session.findById("wnd[2]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").caretPosition = 6
session.findById("wnd[2]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").selectedRows = "0"
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").clickCurrentCell
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").currentCellColumn = "BELNR"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectedRows = "0"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").contextMenu
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectContextMenuItem "&XXL"
session.findById("wnd[1]/usr/cmbG_LISTBOX").SetFocus
session.findById("wnd[1]/usr/cmbG_LISTBOX").Key = "31"
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = path
Dim name As String
name = Cells(a, 1) & ".xlsx"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = name
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 10
session.findById("wnd[1]/tbar[0]/btn[0]").press
'line1:
Application.Wait Now + TimeValue("0:00:05")
If Not Dir(path & name, vbDirectory) = vbNullString Then
Dim wB1 As Workbook
Dim ws1 As Worksheet
With Application
.DisplayAlerts = False
.EnableEvents = False
.ScreenUpdating = False
End With
Set wB1 = Workbooks.Open(path & name)
Set ws1 = wB.Sheets(1)
Application.Wait Now + TimeValue("0:00:05")
Workbooks(name).Activate
Workbooks(name).Close
Cells(a, 2) = "Done"
Else
Cells(a, 2) = "Please, check the order!"
End If
'Dim wB1 As Workbook
'Dim ws1 As Worksheet
'
' With Application
' .DisplayAlerts = False
' .EnableEvents = False
' .ScreenUpdating = False
' End With
'
' Set wB1 = Workbooks.Open(path & name)
' Set ws1 = wB.Sheets(1)
'
' Application.Wait Now + TimeValue("0:00:05")
'Workbooks(name).Activate
'
'Workbooks(name).Close
'
'Cells(a, 2) = "Checked"
line1:
Erase myArray_november1
Next a
Call invoice_extr_2
End Sub
extracting the invoice from SAP
Application.ScreenUpdating = False
SheetSrc = "Input data"
On Error Resume Next
If Not IsObject(SAPApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
If Err.Number <> 0 Then Exit Sub
Set SAPApplication = SapGuiAuto.GetScriptingEngine
If Err.Number <> 0 Then Exit Sub
End If
If Not IsObject(Connection) Then
Set Connection = SAPApplication.Children(0)
If Err.Number <> 0 Then
MsgBox ("Please, open SAP!")
Exit Sub
Else
End If
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
On Error GoTo 0
Application.Wait (Now + TimeValue("0:00:01") / 1.5)
'####################################
'otwiera ordery i zapisuje faktury
'####################################
Workbooks("Saving_invoice.xlsm").Activate
Dim path As String
path = Cells(2, 6).Value
Dim last_row As Double
last_row = Cells(Rows.Count, 1).End(xlUp).Row
Dim i As Double
For i = 2 To last_row
Dim name2 As String
name2 = Cells(i, 1) & ".xlsx"
Dim wB2 As Workbook
Dim ws2 As Worksheet
If Not Dir(path & name2, vbDirectory) = vbNullString Then
With Application
.DisplayAlerts = False
.EnableEvents = False
.ScreenUpdating = False
End With
Set wB2 = Workbooks.Open(path & name2)
'Set ws2 = wB.Sheets(1)
Application.Wait Now + TimeValue("0:00:05")
Workbooks(name2).Activate
Else
GoTo line999999999
End If
Dim last_column As Integer
last_column = Cells(1, Columns.Count).End(xlToLeft).Column
'MsgBox (last_column)
Dim nr_kolumny As Long
nr_kolumny = Cells.Find(What:="Ref Document Number", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
If Cells(1, last_column) = "action" Then
Dim ostatnia_kolumna As Integer
ostatnia_kolumna = Cells.Find(What:="action", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
Else
ostatnia_kolumna = last_column + 1
End If
If Cells(1, last_column) = "action" Then
GoTo line2
Else
Cells(1, last_column + 1).Select
ActiveCell.FormulaR1C1 = "action"
End If
line2:
Dim k As Double
Dim last_row_document
last_row_document = Cells(Rows.Count, nr_kolumny).End(xlUp).Row - 2
For k = 2 To last_row_document
If Cells(k, ostatnia_kolumna) <> "Checked" Then
Workbooks("Saving_invoice").Activate
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/Ns_alr_87013019"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/txt$6-KOKRS").Text = Cells(i, 4)
session.findById("wnd[0]/usr/ctxt_6ORDGRP-LOW").Text = Cells(i, 1)
session.findById("wnd[0]/usr/ctxt_6ORDGRP-LOW").SetFocus
session.findById("wnd[0]/usr/ctxt_6ORDGRP-LOW").caretPosition = 6
session.findById("wnd[0]/tbar[1]/btn[8]").press
On Error Resume Next
session.findById("wnd[0]/shellcont/shell/shellcont[2]/shell").hierarchyHeaderWidth = 453
session.findById("wnd[0]/usr/lbl[62,8]").SetFocus
session.findById("wnd[0]/usr/lbl[62,8]").caretPosition = 9
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[1]/usr/lbl[1,2]").SetFocus
session.findById("wnd[1]/usr/lbl[1,2]").caretPosition = 4
session.findById("wnd[1]").sendVKey 2
'#####################################################
'##############WYBIERA LAYOUT /MACRO##################
'#####################################################
session.findById("wnd[0]/tbar[1]/btn[33]").press
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").currentCellRow = -1
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").selectColumn "VARIANT"
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").contextMenu
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").selectContextMenuItem "&FILTER"
session.findById("wnd[2]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").Text = "/MACRO"
session.findById("wnd[2]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").caretPosition = 6
session.findById("wnd[2]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").selectedRows = "0"
session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell").clickCurrentCell
'######################################################
'############TUTAJ FILTROWANIE PO DOKUMENCIE###########
'######################################################
Workbooks(name2).Activate
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").setCurrentCell -1, "REFBN"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectColumn "REFBN"
session.findById("wnd[0]/tbar[1]/btn[29]").press
session.findById("wnd[1]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").Text = Cells(k, nr_kolumny)
session.findById("wnd[1]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").caretPosition = 8
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").currentCellColumn = "REFBN"
'session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectedRows = "0"
'session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").doubleClickCurrentCell
'###########################################################
'####SPRAWDZA CZY JEST JUZ PLIK O TAKIEJ NAZWIE#############
'###########################################################
Dim objFSO_november As Object
Dim objFolder_november As Object
Dim objFile_november As Object
Dim objFile1_november As Object
Dim aa_november As Integer
Dim bb_november As Integer
Set objFSO_november = CreateObject("Scripting.FileSystemObject")
Set objFolder_november = objFSO_november.GetFolder(path)
bb_november = 0
For Each objFile1_november In objFolder_november.Files
bb_november = bb_november + 1
Next objFile1_november
Dim myArray_november() As Variant
ReDim Preserve myArray_november(bb_november, 1)
aa_november = 0
For Each objFile_november In objFolder_november.Files
myArray_november(aa_november, 1) = objFile_november.name
aa_november = aa_november + 1
Next objFile_november
Dim z As Double
Dim how_digits As Double
how_digits = Len(Cells(k, nr_kolumny))
Dim s As Double
s = 0
For z = 0 To aa_november
If Left(myArray_november(z, 1), how_digits) = Cells(k, nr_kolumny) Then
s = s + 1
End If
Next z
Erase myArray_november
Dim h As Double
Dim o As Double
o = 0
For h = 2 To k
If Cells(h, nr_kolumny) = Cells(k, nr_kolumny) Then
o = o + 1
End If
Next h
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectedRows = o - 1 'tutaj nr linii po filtrowaniu
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").doubleClickCurrentCell
session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_VIEW_ATTA"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").pressToolbarContextButton "&MB_FILTER"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").selectContextMenuItem "&FILTER"
session.findById("wnd[2]/usr/subSUB_DYN0500:SAPLSKBH:0600/cntlCONTAINER1_FILT/shellcont/shell").currentCellRow = 2
session.findById("wnd[2]/usr/subSUB_DYN0500:SAPLSKBH:0600/cntlCONTAINER1_FILT/shellcont/shell").selectedRows = "2"
session.findById("wnd[2]/usr/subSUB_DYN0500:SAPLSKBH:0600/btnAPP_WL_SING").press
session.findById("wnd[2]/usr/subSUB_DYN0500:SAPLSKBH:0600/btn600_BUTTON").press
session.findById("wnd[3]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").Text = "Invoice"
session.findById("wnd[3]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").caretPosition = 7
session.findById("wnd[3]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").currentCellColumn = "BITM_DESCR"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").selectedRows = "0"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").pressToolbarButton "%ATTA_EXPORT"
session.findById("wnd[2]/usr/ctxtDY_PATH").Text = path
If s = 0 Then
session.findById("wnd[2]/usr/ctxtDY_FILENAME").Text = Cells(k, nr_kolumny).Value & ".PDF"
Else
session.findById("wnd[2]/usr/ctxtDY_FILENAME").Text = Cells(k, nr_kolumny).Value & "-" & s + 1 & ".PDF"
End If
session.findById("wnd[2]/usr/ctxtDY_FILENAME").caretPosition = 5
session.findById("wnd[2]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[12]").press
Application.Wait Now + TimeValue("0:00:05")
Workbooks(name2).Activate
Cells(k, ostatnia_kolumna) = "Checked"
Workbooks(name2).Save
End If
Next k
Workbooks("Saving_invoice").Activate
Cells(i, 3) = "Checked"
Workbooks("Saving_invoice.xlsm").Save
Workbooks(name2).Save
Workbooks(name2).Close
line999999999:
Next i
Workbooks("Saving_invoice.xlsm").Activate
If Cells(1, 11) = "action" Then
Columns("K:K").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Delete Shift:=xlToLeft
End If
Cells(1, 1).Activate
Workbooks("Saving_invoice.xlsm").Save
MsgBox ("Done")
End Sub
Well Application.Wait "The time at which you want the macro to resume, in Microsoft Excel date format." So it only pauses your VBA code but not "lets wait till some lines finishes work and we will continue" not sure how to write correctly :D bad in English. But hope you understood.
I suggest you to use Immediate window, and check everything is in place(values) with Debug.Print "My value: " & variable, also VBA is tricky and trying to make people less code while it automatically converts string, int and other stuff which is not declared or misspelled. So make on top "Option Explicit", then declare everything what is not declared.
And delete "On Error Resume Next", cause you cant see first error.
So I did a very simple loop VBA to consolidate data from different workbook into single workbook. I got the out of range error keep promting me and I've tried my best to think but it's a dead end for me. Appreciate if can get some input from the seniors.
Sub consolidate()
Application.ScreenUpdating = False
Set mb = ThisWorkbook
myfdr = ThisWorkbook.Path
fname = Dir(myfdr & "\*.csv")
Do Until fname = Empty
If fname <> mb.Name Then
Set wb = Workbooks.Open(myfdr & "\" & fname)
mg = Range("A1").End(xlDown).Row
Range("M4").Value = mg
wb.Worksheets.Copy After:=mb.Sheets(mb.Sheets.Count)
wb.Close SaveChanges:=False
n = n + 1
End If
fname = Dir
Loop
Application.ScreenUpdating = True
MsgBox n & "Done"
End Sub
Sub Union()
Application.ScreenUpdating = False
Set ms = Worksheets("Sheet1")
fsn = 1
k = 0
Set mb = ThisWorkbook
myfdr = ThisWorkbook.Path
fname = Dir(myfdr & "\*.csv")
Do Until fname = Empty
If fname <> mb.Name Then
sn = Mid(fname, 1, Len(fname) - 4)
Set cs = Worksheets(sn) '<<<<The subscript out of range error happened here
If fsn = 1 Then
fsn = 0
For g = 1 To cs.Cells(4, 13)
k = k + 1
For r = 1 To 10
ms.Cells(k, r) = cs.Cells(g, r)
Next r
Next g
Else
For g = 9 To cs.Cells(4, 13)
k = k + 1
For r = 1 To 10
ms.Cells(k, r) = cs.Cells(g, r)
Next r
Next g
End If
End If
fname = Dir
Loop
End Sub
I know this has been asked several times but I'm quite confused on how to put the negative values for my column L:L in a loop. I can't get it to work. I've tried everything I researched. I'd appreciate any help.
Option Explicit
Sub Importpaymentsales()
Dim fpath As Variant
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
Dim Text As String
On Error GoTo terminatemsg
Set wb = Excel.ActiveWorkbook
Set ws = Excel.ActiveSheet
fpath = Application.GetOpenFilename(Filefilter:="text Files(*.txt; *.txt), *.txt; *.txt", Title:="Open Prepayment Sales Report")
If fpath = False Then Exit Sub
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Text = getTextfileData(fpath)
If Len(Text) Then
ProcessData Text
AdjustDates
Else
MsgBox fpath & " is empty", vbInformation, "Import Cancelled"
Exit Sub
End If
ws.Range("J:L").Value = ws.Range("J:L").Value
ws.Range("J:L").numberformat = "#,##0.00"
ws.Range("O:Q").Value = ws.Range("O:Q").Value
ws.Range("O:Q").numberformat = "#,##0.00"
Columns.EntireColumn.AutoFit
Sheets(1).Move Before:=wb.Sheets(1)
terminatemsg:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
If Err.Number <> 0 Then MsgBox Err.Number & " " & Err.Description
End Sub
Sub ProcessData(Text As String)
Dim x As Long, y As Long, z As Long
Dim data, vLine
data = Split(Text, vbCrLf)
x = 2
Range("A1:R1").Value = Array("Supplier Name", "Supplier Number", "Inv Curr Code CurCode", "Payment CurCode", "Invoice Type", "Invoice Number", "Voucher Number", "Invoice Date", "GL Date", "Invoice Amount", "Withheld Amount", "Amount Remaining", "Description", "Account Number", "Invoice in USD", "Withheld in USD", "Amt in USD", "User Name")
For y = 0 To UBound(data)
If InStr(data(y), "|") Then
vLine = Split(data(y), "|")
If Not Trim(vLine(0)) = "Supplier" Then
For z = 0 To UBound(vLine)
vLine(z) = Trim(vLine(z))
If vLine(z) Like "*.*.*.*.*.*.*.*.*.*.*.*.*.*.*" Then vLine(z) = Left(vLine(z), InStr(vLine(z), ".") + 2)
Next
Cells(x, 1).Resize(1, UBound(vLine) + 1).Value = vLine
x = x + 1
'Range("L2:L").Value = Range("L2:L").Value * (-1)
Range("L2:L").Value = Abs(rng.Offset(teller - 1, -2).Value) * -1
End If
End If
Next
End Sub
Try this:
Dim r As Range
For Each r In Range(Range("L2"), Range("L2").End(xlDown))
If IsNumeric(r.Value) Then r.Value = -Abs(r.Value)
Next
ps: I suppose you don't have blank cells in-between in column L, if you do then a slight modification is needed.
Here it is:
Dim r As Range
For Each r In Range(Range("L2"), Range("L" & Rows.Count).End(xlUp))
If Not IsEmpty(r.Value) Then If IsNumeric(r.Value) Then r.Value = -Abs(r.Value)
Next
I have a Word table containing Legacy DropDown List. There is 11 rows and let's say 4 columns. Each cell contains a Legacy DropDown List which puts the count to 11 per column (44 total).
I have to take the data from these Legacy DropDown List and put it into Labels in another table on another page of the same document.
No problem so far, I made it work though I had to do write it all down instead of using a loop because I couldn't find a way to put a variable in my label name.
I shortened my code to only the first row of the first week (therefore 4 labels) because otherwise it would have been too long and there is no need for it to be that way.
Current code :
Sub Week1()
'Week 1
If ActiveDocument.FormFields("Dom1").DropDown.ListEntries.Count <> 0 And ActiveDocument.FormFields("Dom1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Dom1").DropDown.Value).Name <> "Choose a DOM." Then
lblDom1W1.Caption = ActiveDocument.FormFields("Dom1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Dom1").DropDown.Value).Name
End If
If ActiveDocument.FormFields("Sit1").DropDown.ListEntries.Count <> 0 Then
If ActiveDocument.FormFields("Sit1").DropDown.Value <> 0 Then
If ActiveDocument.FormFields("Sit1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Sit1").DropDown.Value).Name <> "Choose a SIT" Then
lblSit1W1.Caption = ActiveDocument.FormFields("Sit1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Sit1").DropDown.Value).Name
End If
End If
End If
If ActiveDocument.FormFields("Int1").DropDown.ListEntries.Count <> 0 Then
If ActiveDocument.FormFields("Int1").DropDown.Value <> 0 Then
lblInt1W1.Caption = ActiveDocument.FormFields("Int1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Int1").DropDown.Value).Name
End If
End If
If ActiveDocument.FormFields("Gram1").DropDown.ListEntries.Count <> 0 And ActiveDocument.FormFields("Gram1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Gram1").DropDown.Value).Name <> "Choose a GRAM." Then
lblGram1W1.Caption = ActiveDocument.FormFields("Gram1").DropDown.ListEntries.Item(ActiveDocument.FormFields("Gram1").DropDown.Value).Name
End If
So this works. That being said, I would like to loop it so I could have this much code only once instead of having it repeated 11 times per week for 11 weeks.
I already named the labels for them to be easy with a loop. Therefore, their names are all lblDom1W1 down to lblDom11W1 for the first week and so on for the other labels (only the last digit changes ( e.i. lblDom1W2 down to lblDom11W2)).
Furthermore, I thought and tried these methods which I thought wouldn't work and didn't :
labelName & value
labelName(value)
I looked through this post but I didn't quite understood everything in it and I'm not sure if that's what I need since it's VB.NET and no exactly VBA. Post I checked
EDIT:
Following user R3uK comment, I tried this but it doesn't work either. I gives me the Invalid qualifier for my leLabelDom.Caption...:
Sub Week1()
'Week1
Dim labelDom As String
labelDom = "lblDom"
Dim week1 As String
week1 = "W1"
Dim leLabelDom As String
For k = 1 To 11
leLabelDom = labelDom & k & week1
If ActiveDocument.FormFields("ListeDomaine" & k).DropDown.ListEntries.Count <> 0 And ActiveDocument.FormFields("ListeDomaine" & k).DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeDomaine" & k).DropDown.Value).Name <> "Choisissez un domaine." Then
leLabelDom.Caption = ActiveDocument.FormFields("ListeDomaine" & k).DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeDomaine" & k).DropDown.Value).Name
End If
EDIT 2 - Working:
Started from the user R3uK answer and end up with this code which works. Huge thanks to you R3uK !
Sub Remplir()
Dim leLabelDom As String, _
wDocD As Word.Document, _
IsHd As InlineShape, _
leLabelSit As String, _
leLabelInt As String, _
leLabelGram As String, _
semaine As String
Set wDoc = ActiveDocument
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 1" Then
semaine = "S1"
lblMaterielS1.Caption = TextBoxMateriel.Text
lblEvaluationS1.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 2" Then
semaine = "S2"
lblMaterielS2.Caption = TextBoxMateriel.Text
lblEvaluationS2.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 3" Then
semaine = "S3"
lblMaterielS3.Caption = TextBoxMateriel.Text
lblEvaluationS3.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 4" Then
semaine = "S4"
lblMaterielS4.Caption = TextBoxMateriel.Text
lblEvaluationS4.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 5" Then
semaine = "S5"
lblMaterielS5.Caption = TextBoxMateriel.Text
lblEvaluationS5.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 6" Then
semaine = "S6"
lblMaterielS6.Caption = TextBoxMateriel.Text
lblEvaluationS6.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 7" Then
semaine = "S7"
lblMaterielS7.Caption = TextBoxMateriel.Text
lblEvaluationS7.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 8" Then
semaine = "S8"
lblMaterielS8.Caption = TextBoxMateriel.Text
lblEvaluationS8.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 9" Then
semaine = "S9"
lblMaterielS9.Caption = TextBoxMateriel.Text
lblEvaluationS9.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 10" Then
semaine = "S10"
lblMaterielS10.Caption = TextBoxMateriel.Text
lblEvaluationS10.Caption = TextBoxEvaluation.Text
End If
If ActiveDocument.FormFields("ListeSemaine").DropDown.ListEntries.Item(ActiveDocument.FormFields("ListeSemaine").DropDown.Value).Name = "Semaine 11" Then
semaine = "S11"
lblMaterielS11.Caption = TextBoxMateriel.Text
lblEvaluationS11.Caption = TextBoxEvaluation.Text
End If
For k = 1 To 11
leLabelDom = "lblDomaine" & k & semaine
leLabelSit = "lblSituation" & k & semaine
leLabelInt = "lblIntention" & k & semaine
leLabelGram = "lblGrammaire" & k & semaine
If wDoc.FormFields("ListeDomaine" & k).DropDown.ListEntries.Count <> 0 And _
wDoc.FormFields("ListeDomaine" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeDomaine" & k).DropDown.Value).Name <> "Choisissez un domaine." _
Then
If wDoc.InlineShapes.Count <> 0 Then
For Each IsH In wDoc.InlineShapes
If IsH.Type = wdInlineShapeOLEControlObject Then
If TypeName(IsH.OLEFormat.Object) = "Label" Then
If IsH.OLEFormat.Object.Name = leLabelDom Then
IsH.OLEFormat.Object.Caption = wDoc.FormFields("ListeDomaine" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeDomaine" & k).DropDown.Value).Name
End If
End If
End If
Next
End If
End If
If wDoc.FormFields("ListeSituation" & k).DropDown.ListEntries.Count <> 0 _
Then
If wDoc.FormFields("ListeSituation" & k).DropDown.Value <> 0 _
Then
If wDoc.FormFields("ListeSituation" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeSituation" & k).DropDown.Value).Name <> "Choisissez une situation" _
Then
If wDoc.InlineShapes.Count <> 0 Then
For Each IsH In wDoc.InlineShapes
If IsH.Type = wdInlineShapeOLEControlObject Then
If TypeName(IsH.OLEFormat.Object) = "Label" Then
If IsH.OLEFormat.Object.Name = leLabelSit Then
IsH.OLEFormat.Object.Caption = wDoc.FormFields("ListeSituation" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeSituation" & k).DropDown.Value).Name
End If
End If
End If
Next
End If
End If
End If
End If
If wDoc.FormFields("ListeIntention" & k).DropDown.ListEntries.Count <> 0 And _
wDoc.FormFields("ListeIntention" & k).DropDown.Value <> 0 _
Then
If wDoc.InlineShapes.Count <> 0 Then
For Each IsH In wDoc.InlineShapes
If IsH.Type = wdInlineShapeOLEControlObject Then
If TypeName(IsH.OLEFormat.Object) = "Label" Then
If IsH.OLEFormat.Object.Name = leLabelInt Then
IsH.OLEFormat.Object.Caption = wDoc.FormFields("ListeIntention" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeIntention" & k).DropDown.Value).Name
End If
End If
End If
Next
End If
End If
If wDoc.FormFields("ListeGrammaire" & k).DropDown.ListEntries.Count <> 0 And _
wDoc.FormFields("ListeGrammaire" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeGrammaire" & k).DropDown.Value).Name <> "Choisissez un niveau." _
Then
If wDoc.InlineShapes.Count <> 0 Then
For Each IsH In wDoc.InlineShapes
If IsH.Type = wdInlineShapeOLEControlObject Then
If TypeName(IsH.OLEFormat.Object) = "Label" Then
If IsH.OLEFormat.Object.Name = leLabelGram Then
IsH.OLEFormat.Object.Caption = wDoc.FormFields("ListeGrammaire" & k).DropDown.ListEntries.Item(wDoc.FormFields("ListeGrammaire" & k).DropDown.Value).Name
End If
End If
End If
Next
End If
End If
Next k
Set wDoc = Nothing
End Sub
In your edit, you try to use a property for a String, but Properties are only for Object variables.
So, you need to find where are stored the controls, in InlineShapes and then loop and filter it to narrow it down to your specific control and change its value.
Here is something that should work or at least is probably close (can't test it) :
Sub OuO()
Dim leLabelDom As String, _
wDoc As Word.Document, _
wListE As DropDown, _
IsH As InlineShape
Set wDoc = wDoc
For k = 1 To 11
leLabelDom = "lblDom" & k & "W1"
Set wListE = wDoc.FormFields("ListeDomaine" & k).DropDown
If wListE.ListEntries.Count <> 0 And _
wListE.ListEntries.Item(wListE.Value).Name <> "Choisissez un domaine." _
Then
If wDoc.InlineShapes.Count <> 0 Then
For Each IsH In wDoc.InlineShapes
If IsH.Type <> wdInlineShapeOLEControlObject Then
Else
'filter on name
With IsH.OLEFormat.Object
If .Name <> leLabelDom Then
Else
.Caption = wListE.ListEntries.Item(wListE.Value).Name
End If
End With
End If
Next IsH
Else
End If
Else
End If
Next k
Set wDoc = Nothing
Set wListE = Nothing
End Sub
sometimes it happens that the trendline label in excel isn't updated when I changed the graph-data. Therefore I want to update via VBA. I want to do it for all existing trendlines in all sheets and charts.
My code until now doesn't work. You will find the error in the comment.
Sub Auto_Open()
Debug.Print "Start"
Dim oChart As ChartObject, nSheet As Integer, nChart As Integer
nSheet = 1
Do While nSheet <= Sheets.Count
nChart = 1
Do While nChart <= Sheets(nSheet).ChartObjects.Count
nSeriesCollection = 1
'Debug.Print Sheets(nSheet).ChartObjects(nChart).SeriesCollection.Count
'Error in next line
Do While nSeriesCollection <= Sheets(nSheet).ChartObjects(nChart).SeriesCollection.Count
Debug.Print "nSheet: " & nSheet & " nChart: " & nChart
Set oChart = Sheets(nSheet).ChartObjects(nChart)
oChart.Activate
'Next line has to changed too
ActiveChart.SeriesCollection(1).Trendlines(1).Select
With Selection
.DisplayRSquared = False
.DisplayEquation = False
.DisplayRSquared = True
.DisplayEquation = True
End With
nSeriesColletion = nSeriesColletion + 1
Loop
nChart = nChart + 1
Loop
nSheet = nSheet + 1
Loop
End Sub
########################################################################
There is a run-time error '438'. Object doesn't support this property or method in the line with the error comment.
There is error in
ActiveChart.SeriesCollection(1).Trendlines(1).Select
Change it to:
ActiveChart.SeriesCollection(1).Trendlines(1).DataLabel.Select
Here is the solution:
:)
Sub Auto_Open()
Debug.Print "###########################################"
Debug.Print "Start"
Dim oChart As ChartObject, nSheet As Integer, nChart As Integer
nSheet = 1
Do While nSheet <= Sheets.Count
Debug.Print "Sheet: " & nSheet
nChart = 1
Do While nChart <= Sheets(nSheet).ChartObjects.Count
Debug.Print " ChartObjects: " & nChart
nSeriesCollection = 1
Do While nSeriesCollection <= Sheets(nSheet).ChartObjects(nChart).Chart.SeriesCollection.Count
Debug.Print " SeriesCollection: " & nSeriesCollection
Sheets(nSheet).ChartObjects(nChart).Chart.SeriesCollection(nSeriesCollection).Trendlines(1).DisplayEquation = True
nSeriesCollection = nSeriesCollection + 1
Loop
nChart = nChart + 1
Loop
nSheet = nSheet + 1
Loop
End Sub
I'm waaay late to the game, but for posterity's sake...
I think the error can be avoided by using for each ... next construct rather than the do while ... loop option and eliminating the unnecessary Select(ellipses is other misc code):
...
For Each oSheet In Sheets
iSheet = iSheet + 1 'if an indexing is needed'
...
For Each oChart In oSheet.Charts
iChart = iChart + 1 'if an indexing is needed'
...
For Each oSeries In oChart.SeriesCollection
iSeries = iSeries + 1 'if an indexing is needed'
For Each oTrend In oSeries.Trendlines
With oTrend
.DisplayEquation = False
.DisplayRSquared = False
'the next statement often assures eq is updated
'unsure if there is a more reliable solution
DoEvents
.DisplayEquation = True
.DisplayRSquared = True
End With
Next oTrend
...
Next oSeries
...
Next oChart
...
Next oSheet