VBA: Using Filesystemobject to locate workbook path - excel

I am using the GetFolder method within FileSystemObject to locate the path of a workbook. I am then using the path to search for filenames in the same directory and extract information from files with certain names using some parameters. It works fine when working from the windows directory, however the company I work at uses Sharepoint. When accessing the Excel-workbook from Sharepoint this method no longer works. The workbook opens fine, but when I run the Macro I get an error message.
Code in question:
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Application.Thisworkbook.Path)
Error: Unable to find path (Swedish literal error message: Det går inte att hitta filsökvägen)
Anyone with a clue as to what to do about this? I've tried some alternate ways to get the path but nothing has worked thus far.

Perhaps it would be cleaner use WinHttp object to get the SP library's contents via SharePoint REST API. It's how Microsoft wants you to approach this problem anyway. Refer to this article and SP REST API Documentation.
There is also a dirty way to this we found a few years ago. If you sync a library's contents to your File Explorer, there's a good chance FSO could work. But keep in mind that the synced library is better kept up-to-date. Also, SharePoint can handle much longer paths than FSO, so when hierarchies get deep, you'll only get the first 250-something characters.
Therefore, I'd use the API.

Related

How can you access hidden published ms-project .mpp files

I was writing a simple script in VBA for batch updating project info posted on Project Web App. I've stumbled on a problem which seems unsolvable to me though, opening projects programatically through FileOpenEx method is working only on a fraction of them.
I found out when opening files manually through MS Project's file explorer that most of the projects are hidden until you pick "Show list of all projects" option, which appears on the top of the list of .mpp files to choose from. After picking this option, all projects unavailable to open through macro show up after a few seconds of loading.
I have no clue on how to tackle this problem, maybe connecting to the SharePoint database in a different, more direct way would help? I'm posting below a few lines of code responsible for opening the projects.
Dim myMPP As MSProject.Application
Set myMPP = CreateObject("Msproject.Application")
myMPP.Visible = True
Dim projectName, fpath As String
projectName = "ExampleProject"
fpath = "<>\" & projectName
myMPP.FileOpenEx Name:=fpath, ReadOnly:=True, openPool:=pjDoNotOpenPool
Set mpp = myMPP.ActiveProject
Solution to the problem was opening MS Project manually before running any macros. Instances of Project opened programatically seem to be able to operate only on cached projects.

VBA - where are "available references" stored?

In Excel (or other MS Office apps), when you go to the VBA IDE, the Tools, References list shows you currently selected assemblies as well as a list of others you can add by checkmarking them. You can also hit Browse to navigate to a folder containing a .TLB file of your own making.
But there's a problem. Let's say you you browse to C:\Fubar\PumpHandle.tlb and add it. Everything works fine. A week later, you uncheckmark the reference, because you don't need it in the VBA project your are working on. No problem: PumpHandle is still on the Available References list -- it's just not checkmarked.
A week after that, you delete C:\Fubar\PumpHandle.tlb. Now go into the VBA IDE, Tools, References, and PumpHandle is still on the list, with no way to remove it. Waaaaaah! If you checkmark it, you will of course get an error message, because the PumpHandle.tlb file does not exist. How can you remove it from the Available References list? I have tried searching the Registry and deleting all references to PumpHandle.tlb, but it still shows up in the Available References list. I have tried searching everywhere in %APPDATA%, but I can not find PumpHandle anywhere in there either. Finally, I have looked inside the .xls? --> .zip file of Personal.xlsb, and can't find it in there.
So -- where does the machine store that (obsolete and unusable but highly confusing) reference? How can I get rid of it?
This list is loaded from the registry. The information is put into the registry when you run a command line regsvr32.exe (plain ol' COM) or regasm.exe (.NET COM). This usually happens at installation time and is cleaned up at uninstallation time. Deleting the file won't remove it from the list.
There are a few locations where it could exist in the registry, depending on whether it's a 32-bit or 64-bit type library and upon whether it's registered for all users or only the current user. The list below is (roughly) in order of most common to least common.
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\TypeLib
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Classes\TypeLib
HKEY_CURRENT_USER\Software\Classes\TypeLib
I don't know where they are stored. Maybe this will get you where you want to go, however.
' You need to add a reference to Microsoft Visual Basic for Applications Extensibilty
Sub ZapReference()
Dim VBAEditor As VBE, VBProj As VBProject, VBRef As Reference
Set VBAEditor = Application.VBE
Set VBProj = ActiveWorkbook.VBProject
For Each VBRef In VBProj.References
If VBRef.IsBroken Then
Debug.Print "Removing: ", VBRef.Name, VBRef.FullPath
VBProj.References.Remove VBRef
End If
Next VBRef
End Sub

Error message "ActiveX component can't create object" appears for all objects

I'm unable to create any objects in an Excel macro I am trying to write. Initially I was trying to work with MSXML2.DOMDocument60, however, I realized that i'm not able to get ActiveX to create any objects. For example, I get the same error (Run-time 429 'ActiveX component can't create object') for this line of code:
Dim ExcelSheet As Object
Set ExcelSheet = CreateObject("Excel.Sheet")
Obviously since i'm in excel, the program is installed correctly and it should have access to it. I've checked several other Stackoverflow pages for this. It doesn't appear to be an issue with a reference and I can't imagine the simple code above not working because of missing dlls since I am again already in Excel.
Is there possibly a security feature on my computer blocking this action?
Again, my issue is not with the above code. This was just a simple way to show that Create object is not working.
My main goal is to get the following to work:
Dim objXML As MSXML2.DOMDocument60
Set objXML = CreateObject("MSXML2.DOMDocument60")
I've already verified that I have the correct reference and I re-registered the Dlls.
I found the answer I was looking for. My company has the C drive and another network drive locked down, which prevents CreateObject from working when an excel sheet is saved to either of these locations. By saving the excel sheet to my desktop instead I am able to get it to work.
There's no class with an Excel.Sheet progID in the Excel object model. You probably meant Excel.Worksheet, but that won't work either, and it's not because of CreateObject or any kind of obscure security feature.
Simply put, not all classes/types are creatable: In the Excel type library Application is, and that's about it: everything else needs to be created within the object model, using the factory methods provided by that API.
In other words the only way you can create an Excel.Worksheet, is if Excel creates it for you.
Set excelSheet = Excel.Application.ActiveWorkbook.Worksheets.Add
Same with an Excel.Workbook:
Set excelBook = Excel.Application.Workbooks.Add
There are other ways (e.g. copying an existing Worksheet without specifying a Target argument will create a new Workbook that contains a copy of that source Worksheet), but rule of thumb if you can't New up a class in a referenced type library, there's little chance CreateObject can do any better (unless the type registration explicitly prevents early-binding usage).
The question was edited, but this answer stands: don't use CreateObject to create instances of classes that are already resolved and readily available.
Set objXml = New MSXML2.DOMDocument60 'works fine
If you really want to use CreateObject, then you need to use progID strings that exist in your registry.
Set objXml = CreateObject("MSXML2.DOMDocument60") ' blows up on my machine as well
Set objXml = CreateObject("MSXML2.DOMDocument") ' works fine
But using CreateObject to create an instance of a class the compiler already knows about, is a very, very roundabout way to do this.

Error 70 in Excel VBA

I am trying to upload directly a picture/chart from excel to a Sharepoint group URL. Here is the script:
Sub ExportChartJPG()
ActiveChart.Export Filename:="http://sharepoint.ap.xxxxxxxxxxxxxx.com/xxxxxx/xxxxxxxxxxxxxx/Pictures/MyChart.jpg", _FilterName:="jpeg"
End Sub
Is that possible? If it's not then can you suggest another way of doing it? Thanks
You can only export to a file, not to a URL. So, you could export to a temporary file on disk, and then submit the file to your web server. You would of course need the web server to have the ability to receive files.
Hang on, from the URL, it's a SharePoint server, yes? Presumably a SharePoint document library? In that case, you need to write some code to use one of the following techniques to upload the file:
SharePoint Web Service
WebDAV
FrontPage Extensions
If you want to do this in VBA, then the MSXML3 library may be useful, since it will let you do HTTP requests.
EDIT: OK, based on your comments, here's a simple VBScript script to get you started. This opens an Excel workbook at a known location, and exports the first chart sheet.
Save this as "test.vbs" and then double-click on it to run it (having created a suitable Excel file, etc.).
Dim oExcel : Set oExcel = CreateObject("Excel.Application")
Dim oWorkbook : Set oWorkbook = oExcel.Workbooks.Open("C:\test.xls")
Dim oChart : Set oChart = oWorkbook.Charts(1)
oChart.Export "C:\chart.jpg", "JPEG"
oWorkbook.Close False
oExcel.Quit
As I said in my comment, VBScript is very much like VBA, but the downside is that there's no strong typing, so you don't get Intellisense, etc. It might be easier to create the script in VBA where you do have Intellisense (and a debugger, etc.) and then "port" it to VBScript.

Excel 2007 VBA FileSearch missing

Someting happened with FileSearch object in Excel 2007.
In documentation it is said that FileSearch is hiden.
Does it mean that there is no way to search for files using Excel VBA anymore?
You can still search using the Dir function - lots of examples on the web if you search for 'excel 2007 vba filesearch', for example this post.
I would add a reference to the Microsoft Scripting Runtime library. That gives you access to the FileSystemObject, which lets you pretty easily test for the existence of a file and/or iterate through directories, doing your own search.
If you want to search on custom document properties, there's currently no way to do it. You have to narrow the search with Dir and open each one individually.
As Steve said you can use the FSO object to search for files using a given root path and filter expression. There is good documentation available on fitlering by extension (remember to include the extensions you want or filter by .xls) and using the objects in the FSO object model.
For an explanation (by msft) on the depreciation of FileSearch method. see - http://support.microsoft.com/kb/920229
For a basic pattern to using the FSO for searching a set of directories recursively with the FSO see -
http://support.microsoft.com/kb/185601
You can still use the
Application.GetOpenFilename
if you want to get back the path to a workbook.

Resources