Is it possible to copy the contents of the Immediate window in Excel VBA? - vba

Question
I would like to know whether it is possible to copy or extract the contents of the Immediate window in Excel VBA, so I can store it somewhere. I already know you can copy and paste from the window manually, I am interested in whether it is possible to do it with code (preferably in VBA itself).
Explanation
Questions similar to this have been asked a few times on SO (e.g. this and this), no-one has given a definitive answer and I was unable to find one from a quick search. Most of the time, answers respond asking why anyone would want to do that, and give ways to get around it in the circumstances provided in the question (e.g. how to output messages to a file or cell range). Given this, I have thought of a couple of example scenarios where someone might want to do this and cannot (easily) get around it.
Example scenarios
A) I am developing a large workbook including a large set of macros, debugging as I go. This includes using debug.print statements and directly querying in the Immediate window, e.g. ? myVar. At the end of the session, I would like to be able to automatically copy the contents of the immediate window into a log file, so I know what happened during my debug session afterwards. This could, for example, make use of the Workbook_BeforeClose event procedure.
B) I am working with two workbooks that contain VBA projects - one I can't edit, another that I can and am working on. The one I can't edit outputs debug information to the immediate window via debug.print that I would like to store somewhere (but I don't really care where).
Disclaimer
I ask this question purely out of curiosity. I suspect I already know the answer (it's not possible to do this), but I am not sure.

Yes-- but not with control-c .... select what you need and then drag and drop

1-Create a sheet named "debug.print"
2-Hide it:
Sheets("debug.print").Visible = 2 'xlSheetVeryHidden, only can be visible with vba
3-Create this function:
Function debug_print(c As String)
ThisWorkbook.Sheets("debug.print").[a1048576].End(xlUp).Offset(1).Value = c
Debug.Print c
End Function
4-Replace your codes "debug.print" to "debug_print"
5-Example:
Sub blablabla()
debug_print "i am doing whatever"
End Sub
But: Using "?" in the immediate window will not save
There is many ways to export this as file now like CSV, TXT, save in SQL etc...

Related

How to select current row in SAP GUI Grid View with VBA Macro?

I am trying to automate a repetitive task in the SAP GUI. I need to search for an order number, select the row that the order number is in and then click a button to complete the task. I have recorded a macro which gives me:
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarButton "&FIND"
session.findById("wnd[1]/usr/txtGS_SEARCH-VALUE").text = "4521305207"
session.findById("wnd[1]/usr/txtGS_SEARCH-VALUE").caretPosition = 10
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[12]").press
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").currentCellColumn = ""
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectedRows = "2894"
session.findById("wnd[0]/tbar[1]/btn[14]").press
session.findById("wnd[1]/usr/chk[1,6]").selected = true
The line:
session.findById("wnd[1]/usr/txtGS_SEARCH-VALUE").text = "4521305207"
Corresponds to the order I want to search, but if I change this value it still tries to process the same order that the macro was recorded on, I'm assuming because of the line:
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectedRows = "2894"
Does anyone know how I would go about finding the number of the row which corresponds to the outcome of the SEARCH-VALUE and then using that as the .selectedRows = ""?
First of all I'd really recommend you add a reference to the native SAP library. Go to your VBA Editor, click Tools, then References, then Browse, and find this file: "C:\Program Files\SAP\FrontEnd\SAPgui\sapfewse.ocx". Add it, and now you'll have types and libraries and coding for SAP will be a lot easier, safer, and slightly faster (Variant types in VBA impose a tiny overhead that in this case is totally unnecessary). Get familiar with this new library if you are going to do any SAP scripting more than once.
Second, about this problem, what you have is a shell, of type GuiShell, which inherits from GuiGridView. GuiGridView looks like a table, a classic Excel-like set of rows and columns. In your transaction, is showing you a big list of orders, in which you go click the "Find" button, put the order you're looking for, and then close the Search Window. Back to your (Grid)Shell, this cell has been selected (Grid has properties SelectedCells, SelectedRows, SelectedColumns that get all set when you go find something), but then you go and modify the value of SelectedRows to a specific one.
So yeah, upon find, a cell has been selected, so all you need is to query its row and then assign it where you need:
Dim numrRow As Long
numrRow = session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell").CurrentCellRow
session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectedRows = numrRow
where "thisShell" is however you do to find a reference to the Shell (session.findByID("blabla") for example, but I'd advise to reduce all the findByID's, they're very slow and type-unsafe).
If you need help about this SAP libraries, feel free to maybe make some new post and ping me on the comments about it.

Checking if a macro is called in another macro

I have a lot of macros in my excel. I don't know whether they are used in that workbook. I don't know whether it is called as a procedure inside other macros.
Is there any way to find out whether the macro is used inside another macros or used in different sheets?
Probably the easiest way - Ctrl + F.
Write the name of the "Macro" and select search in Current Project. Then start counting how many times it will show up.
Another way is to write debug.print "I am used" after the Sub line of the "Macro". Then count how many times has it popped up on the immediate window.
put “Option Explicit” at the beginning of every module
comment out your “Macro
start debug
And it’ll point you to any occurrence of “Macro” call

How to find and edit the named range that a VBA variable is referring to?

My work has a Macro that we use to split combined mailing addresses out into multiple columns, but it is a little sloppy. I am looking to tighten up some of the search parameters, but I am not the one who initially wrote it so I am trying to figure some things out.
The thing I am looking at now is updating the city list in the Macro so that it will identify more cities. The trick is when I look at the Sheet that the process refers to, I cannot find an array or list with cities that the macro is checking against. It just has a bunch of sub processes that look empty to me. I am new to a lot of this so maybe I am missing something obvious.
The part of the module that references the worksheet looks like this:
CityList = shtCity.Range("CityList").Column
And the is no code in the module of the worksheet (shtCity)
I don't really know what I am looking at, so please let me know if there is any other information that I can collect to help resolve this.
Punch the following into the immediate window to see exactly where that range lives.
Debug.Print Range("CityList").Address
Alternative #1 - instead of using a debug.print you could put the same into a MSG box immediately before the module references that range
Alternative #2 - open shtCity and choose CTRL+F3 to see the named ranges.

VBA Word original text on revised range on readonly files

I am creating a VBA tool to record (for QA purposes) revisions of translated files.
I can read both original and revised text, whitch I do paragraph by paragraph.
But I run into problems every time the file cannot be written to (open by someone else or wasn't closed correctly).
Since the macro will run for a long time, this is an issue. I could work around it, but my client probably will not be able to follow the procedure.
Is there no way to just read the original text without having to modify the file?
It really feels that there should be a way. I serache for a way to copy the range into a dummy and then rever revisions on that new one, but no such luck.
I'm really just doing
set wrdApp=CreateObject("Word.Application")
set wrdDoc=wrdApp.Documents.Open(filePath)
for each par in wrdDoc.Paragraphs
if par.Range.Revisions.Count <> 0 then
dim original, revised as String
revised=par.Range.Text
par.Range.Revisions.RejectAll
original=par.Range.Text
ProcessRevision original, revised
End if
next
Thanks!
Answering it myself, bottom line is it can't be done. I run into some other issues and had to undo changes after rejecting revisions, but I still can't do it unless I have write permissions.
My solution to make sure the files were not open somewhere was to copy the files prior to reading.
It's frustrating that a clearly read operation (getting the original text) need write permission.
If I ever find another solution, I'll be sure to post here.

Excel VBA: Resetting spreadsheet count

I have a excel VBA macro that dynamically generates and deletes spreadsheets based on user input. However, when I open the VBA IDE, it seems that although I am naming my spreadsheets in the subs that create/delete them, the overall count is still increasing.
For example, depending on how far into execution my program is, under the "Microsoft Excel Objects" folder in my current project, the spreadsheets in the current workbook could look something like
Sheet101(Sheet3)
Sheet103(Sheet2)
Sheet104(Sheet1)
Or
Sheet81(Inputs)
Sheet83(Date Adjustment Interpolation)
Sheet84(Pricing)
Sheet85(Comparison)
No matter if I delete the rest of them and add one, it still picks up where the last highest one left off.
I don't know how many times this macro will be run and I'd feel a lot better about putting it out there if I could reset this annoying tally on the number of spreadsheets that have ever been generated, since I don't know for sure where excel will cut me off. Plus it's just annoying.
My Question:
I would like to know how to alter that spreadsheet number, or at least what the relevant object is for doing so.
Thanks!
Thanks to #dijkay s suggestion on code names, I've found some code to accomplish this.
ThisWorkbook.VBProject.VBComponents("Sheet1").name = "test"
Will change the code name of Sheet1 to test, so in the Excel Objects folder, it will appear as test(Sheet1) for example.
This option, however, requires messing around with some trust/security settings in each individual excel client running the macro, which is unsuitable for my purposes, unfortunately. You can also change the value manually by changing the (Name) property directly in the IDE through the properties window.
here are some ideas you can try...
Sheets(x).Name = "Sheet" & x
or (assuming in this example, 'Sheet3' doesn't already exist:
Set Sheet3 = sheets.Add
Sheet3.name = "Sheet3"
This is more cleanup than re-setting
cheers,
Micéal