In batch file, how to get focus on a specific window (opened file) - vba

A very simple question I suppose, but I reached a deadlock with this:
I have to use a .bat file to imput plain text data into the right cells an excel sheet with lots of graphics content, vba parts, macro that deactivate "normal" EXCEL buttons and functions, password to protect the pre-typed functions, sudden and unexpected changes in the version of the "taget file", and many other complications...
My need is to be absolutely sure that the .bat is sending the sequence into the right version of the .xlsm file.
To do that I want to store the last known filename (that include the file version) in the .bat file, and I want to take focus on the excel window in wich the data have to be written ONLY IF the title of the excel window is exactly the same.
So Here is the question: How to get the focus on a specific open file from a .bat file?

You can't. If you wanted to use vbscript or jscript you could do what you want in a command prompt in an unreliable way (but it will work most circumstances).
Excel has it's own forms.
Put column headers in a row. Put selection in same row. Alt + D, O.
Plus you can make Excel only allow entries on some cells, like a invoice form.
Right click cell, Properties, Protection, Unlock. Then Alt + T, P, P.
Word has it's own forms similar to Excel (Word is also a spreadsheet).
Excel VBA language (and VBScript too) has input box command.
Sub main()
Sheet1.Range("A1").Value2 = InputBox("enter Value")
End Sub

Related

Make small changes to multiple functions without affecting cell reference

What is the best way to change ">" to ">=" in 100+ formulas such as:
=IF(INDEX($B$2:$B$46,MATCH(A3,$D$2:$D$46,0))**>**$A$2),"1","0")
=IF(INDEX($B$2:$B$46,MATCH(A3,$D$2:$D$46,0))**>**$B$2),"1","0")
=IF(INDEX($B$2:$B$46,MATCH(A3,$D$2:$D$46,0))**>**$C$2),"1","0")
Would I need some VBA code?
I would suggest one more idea if you are not comfortable doing the find and replace in excel.
Press the key combination Ctrl+` (BackQuote symbol)
Now you will have all the formulas visible in the cells.
Copy the range or columns in which you need to modify the formula
Paste it in a .txt file and then do the find and replace
Once it is done copy all the formula and paste it back to the same range.
Press Ctrl+` again and now you have got all the formulas changed as per your need
As long suggested, take a backup of your worksheet before you do these modifications
As Scott_Craner stated, a simple Find/Replace should work.
Save a version of your data in case you misjudged where the search subject is in your data.
Highlight the cells with the formulas you wish to change.
Press Ctrl and the F keys (or click on "Find&Select" in the Home tab in the "Editing" group and select Replace from the drop-down)
In the Find and Replace dialog box, make sure you are on the Replace tab.
In the "Find what:" box, type ">" without quotes (note, if there are other > symbols in some formulas, you can distinguish these with some repetitive text before and after)
In the "Replace with:" box, type "<" without quotes (note, if you included other text to specify certain instances of ">", make sure you include all the text that should also be replaced)
Hit the "Replace All" button.
Your spreadsheet formulas should now be as you'd like them. If they're not, thank goodness you saved another version first. Find/Replace is very powerful, so make sure to limit the scope by selecting the cells you want searched and being sure that Excel will find only those data you wish to change.
If you are interested in VBA, you could use it here. But Excel already has a powerful, user-friendly tool at your disposal.

Excel sheet Deletes the formulas present in the sheet when I open it. How to avoid this?

I'm uploading an excel file that contains sheets, to my server which encodes to base 64 so I decode it as required and process it by adding data in sheet 5 as column1 and column2 with certain number of rows. At the time of uploading, this sheet has some specific formulas on sheet 5 that makes changes in other sheets. So on opening the file which I send as response after editing from server, There comes this prompt that reads
"Excel Found unreadable content in 'MyDownloadedExcelData.xlsx'. Do you want to recover the contents of this workbook?If you trust the source of this workbook, click Yes', with Yes and no buttons
and when I click on yes and open the sheet, all the formulas are deleted.
I see something like
Excel was able to open the file by repairing ot removing the unreadable content.
Removed Records :Formula from /xl/calcChain.xml Part
Repaired Records : Cell Information from /xl/worksheets/sheet1.xml part etc
So, How do I make sure my formulas in the sheet are retained?
Using VBA you could have an on close event that pastes values and an on open event that recreates the formulas. Your file would essentially save with static data, but then be used with functions intact.
If this solution is of interest I can help provide some coding framework.

VBA Save a String across submodule calls

I am making an "intelligent save" button for word and excel files.
The first time it is run from a file, it will prompt user to navigate to the correct folder. The important part is the selected path will be saved for that file and automatically referenced the next time someone uses the macro. Then the user can specify pdf vs. docx/xlsx file type, then save the file.
Is the bolded part possible, and what kind of technique/functions can I use to do this?
Posted as an answer, with a bit of example code and more detail:
For such a small amount of data, why not use VBA's SaveSetting/GetSetting commands to put the needed info in the registry?
Sub SaveGetSettingExample()
' Saves string values to:
' HKEY_CURRENT_USER\Software\VB and VBA Program Settings\AppName
SaveSetting "AppName", "Section", "Key", "Value"
' Displays the just-stored value: Value
MsgBox GetSetting("AppName", "Section", "Key", "Default Value")
End Sub
I'm assuming you're embedding the code in the workbook (and not in the Personal code folder). If so, I've had success writing the file path to a cell in a hidden column (usually out to far right of view) in Excel. Your code can reference this default location as needed when loading the next time.
I'm not sure about Word, our Word documents stay pretty simple.

How to copy conditionally formatted cells from Calc as a table into Writer

I have a LibreOffice Calc spreadsheet that uses some conditional formatting of cells. I would like to copy it into Writer as a table. The colours/formats of the cells should remain as they were due to the conditional formatting in Calc. Unfortunately when I do that, the formatting vanishes.
How can I copy it keeping the formatting?
Of course the Writer version no longer has to be conditional, but I need to keep current colours.
My work is done so eventually I can do the trick in Calc first (abandon the "conditional" part, and just preserve the formatting as - is). However due to amount of data I would prefer not to do it manually.
Is macro the only way to do that?
Use Insert -> Object -> OLE Object
Choose Create from file
Pick the right .ods file.
If you want to modify further (in my case - I need to create many tables from one spreadsheet as the original file is humongous - up to CL column) - do not tick "Link to the file" option.
After pressing OK, the spreadsheet is inserted as is (cloned and embedded), with the conditional formatting. Can be further modified (e.g. rows/cols can be deleted, hidden or whatever is needed). The conditional formatting remains active.
I personally prefer to copy as an image. This ensures the format is always exactly as it was in the spreadsheet and that no weird OLE/DDE links go wrong.
However, you specifically ask for a table. For that there are three (or 2.5) options:
Insert the entire as spreadsheet as an object. In Windows that can be done as Ister describes in his answer. This will be editable as an inline mini-sheet (Writer will invoke Calc for any editing actions).
Insert a part of the sheet as an object: Select what you want in the document, copy to the clipboard, go to Writer and select Edit->Paste Special. Then select the OLE option, or if on Linux, select "calc8". This will be editable as an inline mini-sheet.
Insert as HTML. This creates a standalone table. Formatting will not be 100% as in the sheet, as fonts, etc, will be reset by Writer, but it is a native Writer table that you can manipulate in Writer without invoking Calc. Colors, etc, are preserved.
If you use any of the object embedding options, you'll notice that formulae are kept intact (when not referring the data outside the pasted sheet or region). If you want all the data to be verbatim, then you need an intermediate step:
Select the data in your original sheet that you wish
Copy to the clipboard
Create a new sheet and place the cursor in the same spot as the first cell of the copied data (e.g. if your copied region is B4:X99, then place the cursor in B4 of the new sheet)
Select Edit->Paste Special
In the Paste Special window, check only the following options and click OK:
Text
Numbers
Date & Time
Formats

Excel VBA Password via Hex Editor

I have used the "Hex Editor to modify DPB to DPx" many times in the past to bypass VBA project security on my old Excel VBA projects (.xls), so I definitely know how to do it and know that I can do it.
However I have just tried to do it yesterday and found that it no longer seems to work. I tried using both Excel 2011 (Mac) and Excel 2003 (Windows) and in both cases, I got the same behaviour;
Opening the VBA editor gave a message saying that the project is corrupted and that the project will be removed. The VBA editor then opens and, sure enough, all VBA is stripped out from modules and worksheets.
I have tried this method:
Is there a way to crack the password on an Excel VBA Project? (ie. creating a spreadsheet with a known password and then copying across the relevant fields)
But find that the length of the "GC" key created on my 'dummy' spreadsheet is shorter than the "GC" key on the spreadsheet that I am wishing to access (the "target"). I had read elsewhere that in cases where the "target" keys were longer, you could pad the "dummy" keys to the same length but there is nothing i can find to say what to do in the reverse case.
So - my questions (s);
Is anyone aware if a patch has been applied that makes the "hex editor" approach invalid?
Can anyone help with what to do when the dummy keys are longer than the target keys?
Can anyone else provide any updated onsite into this issue?
EDIT
Having now solved this (to some degree) i thought i'd add a summary here.
I HAVE NOT been able to get this to work on Mac Excel 2011. Something about changing the file from filname.xlsm to fielname.zip and back again results in a corrupted excel file which Excel 2011 refuses to recognise.
I DID manage to get this to work on an old windows machine (XP/Excel 2007) by modifying the .xlsm file name to .zip, editing the DPB= AND GC= values in the vbaproject.bin file with a hex editor then saving this in the .zip file before renaming the .zip back to xlsm. I used the "test" example given by Ricko at the bottom and it worked with ONE CAVEAT - i had to 'pad' out my GC value to make it that same length as the original one in my file.
ORIGINAL: GC="0F0DA36FAF938494849484"
NEW: (TEST) GC="BAB816BBF4BCF4BCF4" (from Ricko below)
NEW: (TEST) GC="BAB816BBF4BCF4BCF40000" (what i used and what worked)
I have your answer, as I just had the same problem today:
Someone made a working vba code that changes the vba protection password to "macro", for all excel files, including .xlsm (2007+ versions). You can see how it works by browsing his code.
This is the guy's blog: http://lbeliarl.blogspot.com/2014/03/excel-removing-password-from-vba.html
Here's the file that does the work: https://docs.google.com/file/d/0B6sFi5sSqEKbLUIwUTVhY3lWZE0/edit
Pasted from a previous post from his blog:
For Excel 2007/2010 (.xlsm) files do following steps:
Create a new .xlsm file.
In the VBA part, set a simple password (for instance 'macro').
Save the file and exit.
Change file extention to '.zip', open it by any archiver program.
Find the file: 'vbaProject.bin' (in 'xl' folder).
Extract it from archive.
Open the file you just extracted with a hex editor.
Find and copy the value from parameter DPB (value in quotation mark), example:
DPB="282A84CBA1CBA1345FCCB154E20721DE77F7D2378D0EAC90427A22021A46E9CE6F17188A". (This value generated for 'macro' password. You can use this DPB value to skip steps 1-8)
Do steps 4-7 for file with unknown password (file you want to unlock).
Change DBP value in this file on value that you have copied in step 8.
If copied value is shorter than in encrypted file you should populate missing characters with 0 (zero). If value is longer - that is not a problem (paste it as is).
Save the 'vbaProject.bin' file and exit from hex editor.
Replace existing 'vbaProject.bin' file with modified one.
Change extention from '.zip' back to '.xlsm'
Now, open the excel file you need to see the VBA code in. The password for the VBA code
will simply be macro (as in the example I'm showing here).
New version, now you also have the GC=
try to replace both DPB and GC with those
DPB="DBD9775A4B774B77B4894C77DFE8FE6D2CCEB951E8045C2AB7CA507D8F3AC7E3A7F59012A2"
GC="BAB816BBF4BCF4BCF4"
password will be "test"
Open xls file with a hex editor.
Search for DPB
Replace DPB to DPx
Save file.
Open file in Excel.
Click "Yes" if you get any message box.
Set new password from VBA Project Properties.
Close and open again file, then type your new password to unprotect.
Check http://blog.getspool.com/396/best-vba-password-recovery-cracker-tool-remove/
If you deal with .xlsm file instead of .xls you can use the old method. I was trying to modify vbaProject.bin in .xlsm several times using DBP->DBx method by it didn't work, also changing value of DBP didn't. So I was very suprised that following worked :
1. Save .xlsm as .xls.
2. Use DBP->DBx method on .xls.
3. Unfortunately some erros may occur when using modified .xls file, I had to save .xls as .xlsx and add modules, then save as .xlsm.