Unable to find file path using workbooks.open in VBA - vba

I am new to VBA howeverI have come across a bit of a stumbling block.
I need to import a CSV file to automate some data.
using a pc I have been able to import the data fine using the below code:
Workbooks.OpenText Filename:="File path is inserted here", local:=True
The above code works fine.
However, when trying this on my Mac, I get a file path error.
I managed to get round this but then returned the file or folder may be read only (which its not). Checked the file to read+write but when entering the file path again I got the same old error of not being able t find the file.
I am racking my brains here as I have tried using ":" and "\" as separators however the file path is still not found
Any help would be much appreciated.
Oh and I have also recorded a macro and imported the file manually, which bought up a whole list of code.
Thanks in advance.

Related

IOError although file name is correct and in the same folder

I imported an Excel file (.xlsx) with pandas, and it worked great.
Then, I tested my code with a copy of the same file (I changed something in its content to test something). I named it differently, and changed the name of the file in the code as well, and it worked great.
HOWEVER NOW - I tried to import the original file again (I didn't forget to change the name in the code as well) - and it doesn't recognizes it anymore!
I checked many times that the name of the file is identical to the one I wrote in the code, and that the .py file is in the same folder as the .xlsx file - I don't get it - why doesn't it work anymore?
Please look at the attached screenshot, where you can see the code, and also the actual file name and that the files are in the same folder (left side of the screenshot)
Please help me understand and fix the situation.
Thank you!
p.s - the "Invalid file name" message is what I wrote in the 'except' block after the 'try' block.
except IOError:
print("Invalid file name")
[Screenshot showing file name matches in code + excel file and python file in the same folder][1]
[1]: https://i.stack.imgur.com/FPWhM.pngstrong text
RESOLVED
Has something to do with Right to Left languages aka Hebrew and Arabic... If your language is one of these and you face the same problem - try what I did
How I found out and what I did to fix it:
I tried to rewrite the file name, and when I started deleting it, I noticed the typing pointer is facing the left side (like in languages that write Right to Left)..
After deleting the entire file name I made sure my keyboard is set to English (my native language is Hebrew, which is Right to Left) - although as you saw, I did wrote the file name in English - weird... And then I wrote the file name again - exactly the same - and it worked!
Apparently some bug regarding RtL languages...
Hope it'll help someone in the future, or that Pycharm will fix the issue...

Trouble specifying path to open Excel woorbook in vb.net app

I have a WFP app that works well. I open an Access DB and want to do the same with an Excel workbook. Opening the DB is not an issue as I am able to remove the drive letter from the path. I am having a bit of an issue with the search path for the workbook. My path operates as expected when the drive letter is specified [here is the line of code that works properly -- xlWorkBook_AR = xlApp_AR.Workbooks.Open("S:\11_2017_Spring\MPRecords-2\Accounting\FinancialSystem.xlsm")]). When I remove the drive specification from the path it does not operate as expected. I receive an error stating the file is not found. What do I need to do to make this dynamic?
Thanks in advance.
Ed
Try this (if it works for your scenario):
Place the excel file in the same folder as your app (.exe) (I assume this works for your needs).
Use this code to dynamically get the path of your app, and then add on the name of the file, something like the below:
Application.StartupPath & "\FinancialSystem.xlsm"
Supply this to excel's .Open method and I believe it will work.
You will need to import System.Windows.Forms. Read about Application.StartupPath property here: https://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx

Excel 2016 vba editor, cannot open ThisWorkbook module

I have an Excel 2016 file (.xlsm). After few weeks working on this file, now I'm facing with one small but weird problem - cannot open ThisWorkbook module. As you can seen in the picture, trying to open this module, window just simply freezes on the screen, any mouse's single or double-clicks on it have no effect.
Maybe somebody also had the same issue? What might cause it and how to resolve it?
this might have only worked for me but is worth a try
clear the office cache (C:\Users\username\AppData\Local\Microsoft\Office\16.0\OfficeFileCache\0)
open the file on a different computer and name it something random.
open the file on the first computer and resave file.
As pointed out in Han's comment, it's possible that the module is corrupted. You may be able to salvage it by right clicking the module and using 'Export File...' to save it as a class file.
If this succeeds you can attempt to recover your code by:
Importing the saved file, this will add 'ThisWorkbook' as a class file that can be opened like other modules.
If this doesn't work, try opening the saved file in Notepad. This should reveal your code in plain text.
I literally have this probablem every day or so. There is no answer, I have to save multiple files everytime I make any changes to the workbook. Its incredibly frustrating when all the macros disapear from the list and you can't open a single module

Word file corrupts after SaveAs

I've build a program to generate other docm files. The program works just fine without saving the generated files through VBA. When I do save through VBA with a SaveAs statement, however, the generated file becomes corrupt. Trying to open the file gives the following message:
"Could not open the file, there is a problem with the contents."
The SaveAs statement is as follows:
qDoc.SaveAs (getQuizURL(quizname))
The quizUrl function has been thoroughly tested and operates properly.
Does anyone know what's causing this and how to solve it? I could save it all manually, but given that other people will be using this program I'd like it to make it as friendly as possible.
You are missing file format specification.
I am not sure about SaveAs either from your code.
I have got Office -2016 and it has a function called SaveAs2, but may be SaveAs is available in older versions.
Anyhow, change your code to specify the file format.
ThisDocument.SaveAs2 "C:\temp\Test.docm", WdSaveFormat.wdFormatXMLDocumentMacroEnabled
This will do the trick.

VBA Zip File error

I am using code similar to the below to add some files to a zip folder:
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(CVar(strDest)).CopyHere CVar(strSource)
This works fine most of the time, but sometimes I get an error:
"(Compressed (zipped) Folders Error) Cannot create output file". This error is raised asynchronously outside of my VBA code and as such I cannot trap it to take remedial action.
If I enter break mode and step back to:
oApp.Namespace(CVar(strDest)).CopyHere CVar(strSource)
then the file is added correctly.
I am guessing that there is some kind of lock either on the compressed folder or the source file that is causing this problem, but I am unsure as to how to check this. I should note that the files are being added are pdf files created from Microsoft Access 2007 and we are using the fully qualified paths and the code runs as follows creating up to 10 pdfs per zip file:
Create Zip
Run this loop:
For Each ReportToRun
CreatePdf ' using DoCmd.OutputTo acOutputReport, "rptHame", acFormatPDF, strReportName
AddToZip
Next
Any idea how to either fix this or trap the Cannot create output file error?
Thanks
I've very successfully used the open souurce Info Zip DLLs. See Compression DLLs, OCXs, etc for links and sample VB code.
Two thoughts, neither likely to fix the issue:
Have you tried explicitly providing FALSE as the next argument, i.e., the AutoStart argument? The help file say it defaults to FALSE, but maybe it's opening the PDFs after generating them, so that they are still open?
add a SLEEP of a couple of seconds (see Make code go to Sleep for the code).
alternatively, loop through all your reports and generate all of them, then start a new loop to copy each one into the zip file, rather than generate/copy for each report. Having a couple of seconds sleep between the two loops might not hurt (assuming that the problem is that the asynchronous PDF generation process is not fully complete at the point the copy is initiated).