Excel VBA / SAP GUI: how to get Value from Cell in SAP to Excel. "Node?" - excel

I have created an Excel VBA Macro to connect to SAP GUI, pull data, etc. Nearly everything works, but I have one problem.
I am using the Transaction CV04N to download some documents from it. My code pastes all required Document names (or numbers) into the multiple selection here:
After executing it, we get this list:
Now my code just double clicks the list one-by-one and this opens up:
So, in most cases, there is only one PDF file in here, but sometimes there is also a TIFF file in there, which then produces an error, because the program tries to download it as a .pdf.
However I only want the PDF. But my program always just selects the first entry.
So I need a function/routine that reads what is in the first line, if it is not PDF, then take the next one. (there are never 2 PDFs in there, so taking the first PDF that shows up is sufficient)
If I just choose document, that only contain PDFs, then everything runs normal.
My current code looks like this (starts from the window shown in last picture)
For j = 0 To k - 1
On Error Resume Next
FileName = XYZ
SaveName = DlFolder & FileName
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").currentCellRow = j
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").doubleClickCurrentCell
Set Tree = Session.findById("wnd[0]/usr/tabsTAB_MAIN/tabpTSMAIN/ssubSCR_MAIN:SAPLCV110:0102/cntlCTL_FILES1/shellcont/shell/shellcont[1]/shell")
Tree.selectNode " 1"
Tree.nodeContextMenu " 1"
Tree.selectContextMenuItem "CF_EXP_COPY"
'It has selected the "copy to" in context menu, now just saves it to Folder saved in "Savename"
Session.findById("wnd[1]/usr/ctxtDRAW-FILEP").Text = SaveName
Session.findById("wnd[1]/tbar[0]/btn[0]").press
'goes to next doc:
Session.findById("wnd[0]/tbar[0]/btn[3]").press
Next
So, if I could get the data from the Table, I could select the one with TIFF, for that I need to read the table.
I have tried
Tree.Text
Tree.Value
Tree.Copy (and then paste in Excel)
But nothing gives me the correct value...
When I select the entry, and press CTRL-C and paste it somewhere it gives me the whole line, so a String with all columns in this entry.
If you have a solution just to get that mentioned String into a Excel Cell, that's fine with me! From there I can set up some routine to make it work.
I hope I made it understandable what I want, if not please feel free to reach out to me!

Related

Excel VBA code that Copy/Pastes into a word document is causing my code to fail

I have an excel sheet
Which is generating a word document (which then gets converted into PDF)
The code loops through various rows of data and creates a page in word based on the data
Part of the loop takes a Graph from Excel and pastes it into the word page.
The code all works fine, and it generates the word document the way I want it. But the copy/paste of the chart is causing me intermittent issues.
The strange behaviour is that :
1 - My code will suddenly exit before it's finished looping through the data.
2 - I can't step through the code using F8 in break mode, as soon as I get to the code that does the pasting, the code just runs everything.
3 - if I put a break point just after the paste, and let the code stop and press f5 continuously, then I don't get the code suddenly exiting.
(Sorry it's such a long explanation)
This is a snippet of my code where the problem is occuring
IndividualChartSheet.ChartObjects("IndividualCompletionChart").Copy
DoEvents
objRange.PasteSpecial Link:=False, DataType:=wdPasteMetafilePicture, Placement:=wdWrapSquare, DisplayAsIcon:=False
Application.CutCopyMode = False
DoEvents
Set myShape = objDoc.InlineShapes.Item(objDoc.InlineShapes.Count).ConvertToShape
myShape.WrapFormat.Type = wdWrapBehind
myShape.RelativeVerticalPosition = wdRelativeVerticalPositionPage
myShape.Top = InchesToPoints(2)
myShape.Left = InchesToPoints(-0.4)
objRange is a reference to a range in my word object.
I can get the code to run, by putting a breakpoint on the Application.CutCopyMode = False and then pressing F5. But I'm hoping to pass this tool on to other users, so I can't need to fix this.
I added in the DoEvents and the CutCopyMode = False after some searching.
I also tried moving the paste command into a seperate function.
But that didn't work.
In case anyone is interested I finally go to the bottom of it.
Such a weird one that it took me a really long time to figure out.
Basically part of my code that was constructing the word document was also adding in hyperlinks so the the person looking at my final output (word document converted to a pdf file) could jump around the document.
My hyperlinks and bookmarks were based on employee names and word didn't like me having non alphanumeric characters in the hyperlink/bookmark names. Once I removed those the code runs through without any problems now.

Collecting more than one data from Clipboard

I manually copy 4 or 5 pieces of text from an email body to clipboard.
Then I want a procedure that assigns each one of these piece of text to a string variable separated by semicolons.
The “codes” below do not work. I write them just to helping explain what I want.
I will appreciate any help.
Sub ColectClipboardItems()
Dim strClip As String
strClip = vbNullString
For Each Item In Clipboard.Items
strClip = strClip & ";" & Item
Next Item
End Sub
I'm not sure if it's true, but the following link indicates that currently Excel can only handle information from the Windows clipboard, which only stores one value at a time:
Access The Nth Item of Clipboard
However, you could try to create the following:
Two step manual process:
A macro in Outlook that stores your data in the Windows clipboard (concatenated). Instead of Ctrl + C you would click a button in your Outlook ribbon.
A macro in Excel that retrieves such data from it.
I think this is prone to human errors.
Or a one step process:
A macro in Outlook that stores all your data & by itself calls another macro in Excel to save it there.
This depends on your e-mail having a consistent structure, though.

VBA .SetText and .PutInClipboard putting two symbols in clipboard instead of desired data

Using Excel 2016 and a reference to the Microsoft Forms 2.0 Object Library, I'm trying to copy the ActiveCell's contents to my clipboard. Instead, the resulting contents of my clipboard are the following 2 symbols (if they'll actually show up in this text field.
��
��
(In case those symbols aren't rendering, in the StackOverflow website's text editor they look like white rectanges. Depending on the text editor I'm pasting it in, they've also resembled a question mark, a black diamond containing a white question mark, and just a blank space as if the space bar was pressed.)
I'm not trying to copy symbols of any kind, it's plain English. I've used code similar to this in other macros and it's always worked until today. The code itself is below. I hope you can help!
Dim clipboard As New MSForms.DataObject
clipboard.SetText ActiveCell.Value
clipboard.PutInClipboard
Debug.Print clipboard.GetText(1)
Set clipboard = Nothing
The Debug.Print command prints out the desired text, but after the macro finishes, the desired text is not there and instead there are the 2 symbols again.
In Windows 10, if file explorer is open the putinclipboard does not work. Go figure.
https://www.mrexcel.com/board/threads/copy-cell-address-to-clipboard-issue-putinclipboard-not-working.983442/
One way that worked for me is;
-Close Excel and File Explorer.
-Reopen Excel test the functionality of PutInClipboard /paste
-Open Window 10 File Explorer
-Test again.
*I work for me why? I don't know but it seems that excel has to be open prior to File Explorer.
The effect seems version dependent, whether the explorer window is opened before or after Excel. It also depends on what is in the explorer window - if it is a 'system location' such as "This PC", then putinclipboard still works normally.
I had tried various options for ages, but eventually found one workaround - acting as if copying manually...
In this case, the text to be copied to the clipboard is in the active cell, but the approach can be adapted for other circumstances:
Sub AACopyText() 'has to be called from Excel workbook (ie not from the VB window!)
SendKeys "{F2}^a^c{ESC}"
End Sub
F2 activates the contents of the cell, ^a selects the entire contents, ^c copies the contents to the clipboard; {ESC} is then required or the cell contents remain active.
if the required text is not the activecell content, for example is in a variable eg MyTextToCopy then the activecell can be temporarily over-written:
AppActivate ActiveWorkbook.Name
SendKeys "{F2}^a" & MyTextToCopy & "^a^c{ESC}"
These lines activate the activecell in the active workbook (so can be used in a procedure called from a form, etc), selects the contents, overwrites the contents, copies the new contents, then returns the original contents.
Alternatively, use can be made of Notepad; the following routine will work in programs other than Excel:
Function clip(ClipText As String)
On Error Resume Next
Dim WasntOpen As Boolean
AppActivate ("Notepad")
If Err Then
WasntOpen = True
Err.Clear
x = Shell("Notepad.exe", vbNormalFocus)
AppActivate ("Notepad")
End If
SendKeys "^a" & ClipText & "^a^c^z"
If WasntOpen Then SendKeys "%Fx"
End Function
where 'ClipText' is the text you want to save
eg In your routine, the line
Clip("1234")
will put "1234" on the clipboard

Embed excel into word using vba does not always display the top of the embedded sheet

I have a Word document with VBA code to embed multiple excel sheets using the following process i.e. addOLEObject
Set IllSheet = _
ActiveDocument.Bookmarks("example1").Range.InlineShapes.AddOLEObject(ClassType:="Excel.Sheet.12", filename:=filename1, LinkToFile:=False)
however on occasion the embedded spreadsheet does not display at the top of the sheet but seemingly randomly scrolled part way down the sheet. I have checked and the excel sheet is saved with it scrolled to the top.
How can I ensure the sheet embedded is always scrolled to the top?
I can access the embedded sheet and input values as per below but can not seem to find a suitable line of code to ensure that the sheet displays at the top of active sheet.
Set objOLE = ActiveDocument.InlineShapes(3).OLEFormat
objOLE.Activate
objOLE.Object.ActiveSheet.Cells(1, 3).Value = Format(Funds, "#,###0")
Selection.MoveRight
objOLE.Object.Application.Quit
DoEvents
I have tried scroll functions etc and also want to avoid using SendKeys because it is so unreliable.
Any help greatly appreciated.
M

Excel VBA forcing selection of objects after run

I've researched this to death and feel like I'm the only person it's ever happened to.
I have some VBA that:
Creates a copy of 3 sheets to a new wb
In the new wb converts to values, deletes objects (shapes and controls) and all but 3 ranges
Opens an existing third file and sets the contents of three ranges in that to match the new wb
Closes the existing file (saved)
Closes the new wb (saved)
Gives a message box saying complete
At the end of all this, something weird happens with the state of the windows. The selected cell does not appear selected. If I try and click a control afterwards, it selects the object (hence users could drag them). It shouldn't and this is the big problem.
I've tried selecting a cell through code, it throws an error. I had limited success by forcing drawing mode off using Call CommandBars("Drawing").Controls("Select Objects").Execute and activating a specific sheet & selecting a cell. However, even then if I even click on a few cells afterwards, the next time I select a control it will select it as an object rather than click the thing.
I have no idea why and can't find anyone who's seen this before.
Any ideas on what I can do?
Thanks,
Basil
I didn't figure it out entirely, but I did find a fix. Hopefully it works for anyone else who finds this problem.
At the end of the code I added this:
ActiveSheet.Shapes.Range("ctrlExportPrices").Select
ActiveSheet.Range("B8").Select
So it forced a control on the sheet to be selected, and then a cell.
The next time I select the control manually, it clicks it rather than selecting the drawing object.

Resources