String works in InsertFile. Variable containing same string does not. Why? - excel

I am using Excel VBA 2016. I have applied all available updates.
This works fine for me when I attempt to insert into a WORD document.
Range.InsertFile FileName:="c:\tmp\filename.rtf"
If I do this...
variable = "c:\tmp\filename.rtf"
Range.InsertFile FileName:=variable
It does not work. The error message is, "Sorry, we couldn't find your file. Was it moved, renamed, or deleted?" Then it gives me the path in parenthesis.
I have unsuccessfully tried all sorts of permutations that might make this work and Googled for too long. Any ideas?

This appears to be a bug in VBA. I was trying to read a list of RTF file names in a Word document I had generated from a different program. Then I was going to insert the RTF back into the same Word document in a new table. I tried both an array and an ArrayList. Both failed. Ultimately, I had to dump these file names into a text file and read it from there. That worked perfectly. Thanks Ahmed for making me do the hard word I was avoiding. Hopefully this helps someone else.

Related

Excel display wrong values from XML file

Can somebody help me with this problem?
We are receiving XML files with some data, but then I open that XML file using Excel values are not the same like when I use notepad to open same XML file.
For example
This is what is diplayed when I open using Excel
And when i open it using Notepad in the same place value is different
as you can see, values are different in last digit.
Excel reads it as "0" for last digit, but in Notepad last digit is "1".
Does somebody knows a reason why this is happening?
Since there is no response over here, i am putting link from microsoft community where i also started a dispute and there are some replays (not exactly solutions) but explanation why this scenario is happening
https://answers.microsoft.com/en-us/msoffice/forum/all/opening-xml-using-excel-values-wrongly-imported/87b69b13-bbd4-4c02-9776-b2644084935b?page=2

Automatically name a file based on cell data and function when saving a spreadsheet

I have an .xltm template spreadsheet that I'm wondering if I can get a macro to populate the "save as" file name based on cell data, is this possible?
There are over 7 people who will have this spreadsheet, it's more of a form, and we are trying to figure out a way to keep the filenames uniform. I know there is the ThisWorkbook.BeforeSave, but I'm not really having any luck there. I just need it to make a file named something like $A$1 R $B$1 T $B$3.xlsx
how ever the problem im having is that one of the celss will have a function "Today()" and sometimes it will be a regular text the picture I included is off code that doeas work but only if the cell have a regular text in them but not a function, I quess it is due to it being a string or variable or something.
enter image description here
Any ideas on how to do this? anything helps thank very much.
I see from the image that you are working with Mac OS. The thing that comes to mind immediately is that the forward-slash character (/) is not allowed in a file name in Mac OS and by default, TODAY() is going to include the forward-slash character. Try using the replace function to switch it out for an underscore (_). So if your cell B7 uses the TODAY() function replace it with:
replace(range("B7").value,"/","_")
It also seems a bit strange to save an .xltm template as .xls file. Be sure you understand the workbook.saveas method including the fileformat argument
I think that part of your code should be:
ActiveWorkbook.saveAs strPath, xlExcel8

Blueprism Write A Collection To An Excel File Issue

I am attempting to create a blank excel file, write a collection to the file and save it at a location. I am able to create a file, however when I run the write collection step I get a generic code error message. I have also noticed that the Save As Step doesn't seem to work correctly. It outputs blank as the Workbook Name. I believe the issue may be linked to a column in my collection which is a set of inserts. The job seems to work when the collection is empty, only producing the error when it is populated.
You've stumbled across issues that can be really tricky to debug.
The issue with the 'Save Workbook As' is a Blue Prism bug that you can fix on your own. Open the action in Object studio and you will notice that the Code stage outputs the result to 'workbookname' instead of 'newworkbookname'. Change that and it will work as intended.
As for the issue with the 'Write Collection', if you say that it works if there are no rows then the values may be the issue. I am not quite fond of the equal sign at the beginning of the value, what happens if it's not there? Have you tried including apostrophe in front of the string to force the value into text and not a function?
Like this:
' = insert into dbo.table ...

Excel gets strange characters when importing data from Oracle DB

I'm using an excel workbook to fill a few worksheets with data from an oracle DB.
The connections are properly set up, however when the worksheets are refreshed, the text that has accents like '~' is replaced with '?'.
Ex: The result of the query is ' Não '. In Excel it shows ' N?o '.
Maybe the encoding isn't right however I've tried saving the workbook with UTF-8 encoding and the problem remains.
Any suggestions?
Thanks
The answer lied within NLS_LANG.
The environment variable was missing and the solution was adding it with the right value. As soon as I added it, when I tried refreshing the connection in excel everything worked fine.
In my case the value for NLS_LANG was: PORTUGUESE_PORTUGAL.WE8ISO8859P15.
However that may vary according to where you live and the regional settings of your machine.
I hope this helps other people, seeing as I've barely seen any other solutions for this online.

Excel Error: Removed Records: Sorting from /xl/worksheets/sheet10.xml part

I'm almost sure I will have to create a new excel file, but maybe at least here I get some ideas what was the source of the problem.
My excel file is constantly giving the following error:
Excel found unreadable content in 'filename.xlsm'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.
So I do. And the file opens as repaired showing:
Removed Records: Sorting from /xl/worksheets/sheet10.xml part.
The detail is that I don't even have a sheet10
Its a big excel file full of custom-objects. But I have no DB connections or autofilter so could not relate to AutoFilter Criteria Using Array (Error) - Too Large String? or xlsx error: "Removed Records: Named range from /xl/workbook.xml part" when tried to resolve errors.
Anyone has a clue what's going on?
tks in advance
try clearing the sort before saving down the workbook. When this happened to me I had my sheet references mixed up so the sort wasn't cleared
Sheets(yoursheetname).Sort.SortFields.Clear
I am afraid I cannot explain what is going on, but I had similar experience several times - always with XLSM file. In the latest case I learnt that Excel did not like the same range name used twice, when defined as Worksheet range name (we assumed it is safe to use it that way).
Our troubleshooting path is:
try to save file as XLSB (binary macro file) and either use XLSB file or save it back to XLSM
check file history (we always keep daily snapshots) and find when the problem firstly occurred; use previous version and port your changes
extract the code (via SVN code), create a new workbook and import the code back
Usually Step 1 solves the problem, but Step 3 never failed us.
Awesome,
This issue has been driving me crazy!
It seems that saving as the xlsb (binary) file PLUS adding the clearing of sort fields appears to have sorted out my issue.
I added the following code to the BeforeClose sub, it appears to clear the fields really quick, changes to my Dashboard and prompts for saving.
The xlsb file saved over a mb in file size and appears to load a little quicker!
Thanks for the pointers & support!
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Sht As Worksheet
' Clear all Sort Fields prior to Save & Exit
For Each Sht In Application.Worksheets
Sht.Sort.SortFields.Clear
Next Sht
aa1_Dashboard1.Select
aa1_Dashboard1.Activate
End Sub
I would like to add my own confirmation regarding clearing sort fields in your spreadsheet, as first suggested by Chris. I specifically had the error occur after I used column sorting in a VBA macro, so I deduced that the problem must be column/field-Sort related. It was.
Adding the line:
Sheets(yoursheetname).Sort.SortFields.Clear
after the sort command allowed the sorted columns to remain sorted (in my case), but cleared whatever was causing the
"Sorting from /xl/worksheets/sheet10.xml part." error when the file was next opened.
Many thanks for your support.
I don't know if this will help anyone but it's solved my problem. I had an excel sheet, nothing complex, just contacts, a much used file in Excel 2010 on Win7. Today I was using the 'filter' to select specific groups to email targeted content to. In the middle of selecting and copying to paste into Outlook, the document came up with a pop up as if I was installing something, then the 'installer' both of which I cancelled out of. The Document closed and then on trying to reopen, I was getting the error message. If I chose 'yes' to repair I was given just the subset of my filtered list, so about 40 of my 350 records, the rest lost. I googled and tried different things, including opening in Googledocs google sheets and nothing worked. I never 'saved' the repaired version as my old data wasn't in that. Just now, I right clicked on my document in explorer and chose "restore previous versions" and it brought me to the version of yesterday - thankfully, nothing added to it since then. Opened that one and bingo, it opened fine with ALL the data in it. I know the filter had corrupted it, so will be cautious next time I use it. I don't know why it doesn't like the filter but 4 hours on, i can now calm down and relax! Hope this helps someone.
was having this issue as well. I have a spreadsheet with multiple tabs (maybe 30 tabs?) and had filters on each sheet, and shared on SharedPoint. The error was not enabling editing of the file on SharePoint. I removed the filters on every sheet and uploaded back to SharePoint and the issue is gone!
I did what Chris mentioned above. Clear the sort fields in your spreadsheet.
Sheets(yoursheetname).Sort.SortFields.Clear
Also, save the file as *.xlsb.
That should get it to work.
When you get a message telling that Excel found unreadable.... just click "Open" and then close all the following messages you get.
-In the VBE open the project showing the name of Addin or the .xlsm.. the click anywhere in any module of the workbook as you are going to edit the text of the macro, then click save, select workbook type like Addin select the folder where you want to save it, give it a new name then click "Save", that is all you have to do, the new file you created will ok.

Resources