Libre office Basic macro is ignoring the last folder that I have added to the folder path - libreoffice-basic

Sorry if my wording is a little weird, but I don't know how to summarise what's wrong.
I'm using a macro in LibreOffice Calc which takes the selected cell and opens a folder in explorer. I want the spreadsheet to also find what folder it is in so that it can be moved anywhere and still work. So far, I have got it to open a folder based on the cell that is selected, and I can get it to figure out what folder it is in.
I am trying to tack on and additional part of the folder path that will be dependent on the selected cell, and then have the program open that folder.
Here is my code:
If oConv.UserInterfaceRepresentation = "Sheet4.A1" then
OpenFolder = ThisFolder & "/NewFolder"
shell ("C:/Windows/explorer.exe",1,OpenFolder)
End If
'ThisFolder' is the folder that the spreadsheet is currently in, and 'OpenFolder' is the new folder path that is supposed to be opened depending on the selected cell. The problem is that LibreOffice just ignores the '& "NewFolder"' and opens the path defined for 'ThisFolder'. What's even weirder is that If I watch the variable 'OpenFolder' and go step by step, it shows the full path for that variable including the 'NewFolder' before it does the shell command.
How can I get it to open the 'NewFolder'?

Related

Preserve settings in the file

I am creating my excel add-in that saves the current file as csv into user-specified folder. I would like my program to ask for the folder path the first time the program is launched and to remember that folder in the future. I am wondering is there any way to preserve the data within the program? I figured that I could write the path into .txt-file but that feels a little hack-like solution and would clutter the addin folder.
I use the GetSetting and SaveSetting functions in my VB 6 apps. Rather than cover them in detail, take a look at this excellent web page that illustrates how to use in with Excel
Excel Tips From John Walkenbach
Create a Worksheet, and store the values in cells. Then in the VBA Editor find the Worksheet in the Project Explorer (Ctrl + R) and set "Visible" to "2 - xlSheetVeryHidden" in the Properties Pane (F4) so that it is not readily visible to users.
You can then set/retrieve the data in with code in the format SheetName.Cells(row,column).Value, e.g.
MyPath = Sheet1.Cells(1,2).Value 'Get data from cell B1
Sheet1.Cells(2,2).Value = NewPath 'Set data in cell B2
There are multiple ways to approach this. Besides the hidden sheet approach, already described, you can
Write the information to a CustomXMLPart. This is a xml file stored in the workbook's ZIP file where information can be stored.
For something as short and simple as a file path, you could also use a CustomDocumentProperty

Change Path to Picture Links in Excel

I have manually pasted a large number of linked pictures into a 2010 Excel spreadsheet using insert picture -> select picture location --> link to file. These pictures are part of a report. I update the pictures using R each quarter, and my report automatically updates. Perfect.
I now need to change the directory where the plots are kept, and need to update the links. As there are around 200 of them (its a big report), I want to do this in VBA. Whilst I can loop through the pictures ok (ActiveSheet.Pictures), I can't seem to find the links/address. Any idea how I can see the underlying file location so I might change it - the reference has to be stored somewhere (note - these don't seem to be stored as hyperlinks).
Any idea how I can see the underlying file location so I might change it - the reference has to be stored somewhere
Create a new folder
Paste a copy of the .xlsx or .xlsm excel file
Uncompress the file with a zip tool (i'm using 7-Zip)
Delete the .xlsx or .xlsm file (optional)
Now we have all the component parts of the original file as plain text xml files and folders
Inside the folder xl\drawings\ _rels there are files named as drawing2.xml.rels, drawing3.xml.rels, ...
It seems that each file corresponds to a sheet and stores the paths to images in this format:
Target="file:///C:\Users\myusername\Documents\MyImageFolder\My%20Image%20Name.png"
Change the paths with a text editor
Compress all the contents of the folder to a .zip
Change the extension to the original .xlsx or .xlsm
These steps could be automated with VBA, AutoIt, etc., here some references:
An example with AutoIt and 7-zip
http://www.jkp-ads.com/Articles/Excel2007FileFormat.asp
http://www.jkp-ads.com/Articles/Excel2007FileFormat02.asp
Ron de Bruin zip examples with VBA
Read and change multiple XML files in Excel (2007) VBA
Excel uses the Formula Bar as the link in this case, just the same as it would link between ranges in two different worksheets. When I select a linked picture, the formula below populates in the formula bar:
=[TrialWB.xlsm]Sheet1!$C$3:$E$6
You can access the Shape's formula using the code below and inserting your picture's specific name:
ActiveSheet.Pictures("Picture Name").Formula = "=[TrialWB.xlsm]Sheet1!$C$4:$E$6"
In updating the links, you'll have to change the file path in the formula. This might look like:
ActiveSheet.Pictures("Picture Name").Formula = "='C:\Reports2015\[TrialWB.xlsm]Sheet1'!$C$4:$E$6"
changing to
ActiveSheet.Pictures("Picture Name").Formula = "='C:\Reports2016\[TrialWB.xlsm]Sheet1'!$C$4:$E$6"
This question may be of some further assistance for accessing formulas:
Excel: create image from cell range
And here's a useful Microsoft page for formula file path editing: https://support.office.com/en-us/article/Create-an-external-reference-link-to-a-cell-range-in-another-workbook-c98d1803-dd75-4668-ac6a-d7cca2a9b95f

Cell linking to an external workbook shows #VALUE! error after saving and reopening when external workbook is not open

I'm using formula
=COUNTIFS('F:\kray_srrg\[ahw2.xlsx]kray_f2'!$A$2:$A$159451,B9,'F:\kray_srrg\[ahw2.xlsx]kray_f2'!$AC$2:$AC$159451,">64.5")
in an excel workbook list.xlsx and in this formula the file ahw2.xlsx is an external file. My problem is that when I close all files and again open list.xlsx then #VALUE! appears in place of result value. How can I keep the formula?
I think you do not lose the formula and it should show the same result as when list.xlsx was last saved if you choose to open list.xlsx without updating links. Since the formula is preserved, provided ahw2.xlsx has not been deleted or moved after list.xlsx was last saved you should be able to accept updating of links then go to Edit Links and Open Source to open ahw2.xlsx and automatically update #VALUE! to whatever is more appropriate at the time.

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.

Update hyperlinks in shapes when file location changes

I have a process flow diagram that uses various excel shapes to visually represent a data production process from start to finish, I.e. from data input to analytic environment to data output to submission file. I have used vba to hyperlink many of the shapes in the diagram to another sheet in the workbook (using thisworkbook.fullname) that contains definitions for abbreviations contained in the text of each shape, eg C1 is listed in a shape, the hyperlink takes you to the definition tab cell where C1 is defined as control point one. The hyperlinks work when the xlsm workbook is in my home location where i saved the file but they do not work if I save the file to another location (they try to open my original workbook). Is it as easy as changing the hyperlink addresses to thisworkbook.filename and dropping the path to make this work? do i need to create a macro that will automatically look up the old hyperlink address and replace it with the new address of current file location for every shape in the workbook containing a hyperlink. The path could change in the future, so want it to be relative and not fixed - for example if I save the file to share point and another user saves a copy to their home directory, I still want the links to work for them in either location. Some hyperlinks go to "sheet1" some to "sheet2" for example, but sheets 1 & 2 are both located in the same workbook. Please help!
ActiveWorkbook.Name solves the problem, replace ThisWorkbook.FullName with it and hyperlink works in other directories.