How to discover Oleobject ClassType of files? - excel

I'm stuck how to determine files' Classtypes needed to use in code to embed these files into a Word document:
Selection.InlineShapes.AddOLEObject ClassType:="AcroExch.Document.11", _
FileName:="C:\Work\Dashbaord & ".pdf", LinkToFile:=False, _
DisplayAsIcon:=False
I need to embed csv, pdf, xlsx and txt files. How I can automatically loop all files in folders and automatically determine the ClassType of each?

In order to insert a file as an OLE Object the file type needs to have an available OLE Server installed on the machine, or it needs to be in a format that the Windows Packager mechanism can "wrap up" into an OLE type. Before you go this route you need to ensure that anyone who tries to work with such a document has corresponding OLE Server software installed on the machine on which the document is opened. Just because the machine that creates an embedded OLE object can do so doesn't mean another machine can work with the result, later on.
OLE Server software will be noted in the Registry. The Microsoft Office applications (Word, Excel, PowerPoint, etc.) are able to function as OLE Servers. In the Registry you'll find corresponding entries such as Word.Document and Excel.Workbook... or AcroExch.Document for PDF files if Microsoft Office and the Adobe Acrobat Reader are installed.
One way to figure out which ClassTypes to use would be to manually insert each file type and inspect the resulting Embed field code.
To look the ClassTypes up in the Registry, something like the following code sample can be used in Word. Word has the function System.PrivateProfileString that wraps up a Windows API call to the Registry. It can be used to retrieve and to write information. (This code does not loop the files in a directory as the question was about how to determine the ClassType. For the sake of simplicity a file extension is hard-coded.)
A file type that does not have an OLE Server won't have a . in the default value of the Registry key. A .txt file, for example, is listed as txtfile. You may have to watch out for some file types; for example on my installation a csv file is listed as Excel.CSV, which may not be what you want...
Sub RetrieveOLEInfo()
Dim fileExt As String
Dim regKey As String
Dim oleServer As String
fileExt = "docx"
regKey = "HKEY_Classes_Root\."
oleServer = System.PrivateProfileString("", regKey & fileExt, "")
'Debug.Print oleServer
If InStr(oleServer, ".") = 0 Then
Debug.Print "Insert as a Package"
Else
Debug.Print "Insert as: " & oleServer
End If
End Sub

Related

Do the DSOFile functions only apply to the non-binary Excel Document types?

Using Windows 10 (Build 1903 if that's relevant?) and 64bit Office 365 (probably relevant?) I've implemented a system that allows me to version control Excel VBA code.
I'm using the Workbook_BeforeSave method to check whether the current file is saved or not, and if it is saved, where it is saved to.
This works fine and will prompt the user as to whether they want to update the code contained within. I then thought that maybe I should in fact check if the code "needs" to be updated prior to prompting the user.
First off, I found the following question/solution: Using VBA to read the metadata or file properties of files in a SharePoint doc library
which I couldn't use without DSOFile.dll that I was able to install from here:
https://www.microsoft.com/en-us/download/details.aspx?id=8422
Here follows the code I have which doesn't work:
Private Function CheckTemplateIsNewerThanCurrentFile(ByVal templatePath As String) As Boolean
Dim templateName As String
Dim fso As New FileSystemObject
templateName = ActiveWorkbook.CustomDocumentProperties("TemplateName").Value
If fso.FileExists(templatePath & "\" & LocalTemplateName) Then
Dim objDSO As New DSOFile.OleDocumentProperties
objDSO.Open templatePath & "\" & LocalTemplateName, True, dsoOptionDefault
If Not objDSO.CustomProperties("LastCommitDate") = ActiveDocument.CustomDocumentProperties("LastCommitDate").Value Then
CheckTemplateIsNewerThanCurrentFile = False
Else
CheckTemplateIsNewerThanCurrentFile = True
TemplateLastCommitDate = objDSO.CustomProperties.Item("LastCommitDate")
End If
End If
End Function
And here (highlighted) is the error I receive trying to run the method above on an .xlsb file:
(FWIW: the reason for use of the .xlsb format is because we're working with 500K+ rows of data in the process we're carrying out. Yes, I know Excel is ABSOLUTELY NOT the tool for this but we're lumbered with it now)
I know I could have already tried changing the file format to .xlsm but because this file is version controlled that is a pain to do if the method is still likely to fail.
Thanks in advance,
Alex.

custom function in excel using vba that works in any matchine

I have created a custom function via vba in excel. If I use it in my computer, it works ok, but if I change the file to another computer (where this computer also has the created function), it does not work. I must change the path of the created function. Is there any way to not change the path everytime I copy the file into another computer?
='C:\Users\Usuario1\Documents\Complementos\BondsTIRMDuration.xlam'!TIrbonds($A2;F2;'C:\Users\Usuario1\Documents\Complementos\AsBusinessDay.xlam'!asbusinessday('C:\Users\Usuario1\Documents\Complementos\AsBusinessDay.xlam'!PrevBusinessDay(HOY())))*100
Solution 1: You could use a common paths in both computers
(for example: C:\work , C:\Work2)
Solution 2: You could put all files in the same path (C:\work), then you only need the to put the file name
='BondsTIRMDuration.xlam'!TIrbonds($A2;F2;'AsBusinessDay.xlam'!asbusinessday('AsBusinessDay.xlam'!PrevBusinessDay(HOY())))*100
Just save your add-in in the correct path on every computer.
It should be something like:
C:\Users\YOURNAME\AppData\Roaming\Microsoft\AddIns\
See Install and Use Excel Add-ins to determine the correct path.
If your add-in is installed correctly you should be able to run your user defined function without a path.
You can call special folder with application.
MsgBox Application.DefaultFilePath
This example will be: C:\Users\Usuario1\Documents
'Here are a few VBA path functions
MsgBox Application.Path
MsgBox Application.DefaultFilePath
MsgBox Application.TemplatesPath
MsgBox Application.StartupPath
MsgBox Application.UserLibraryPath
MsgBox Application.LibraryPath
You can too create wscrit object to call another paths, for example:
MsgBox CreateObject("Wscript.Shell").SpecialFolders("Desktop")
Example folders for Wscript.shell object:
AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
Startup
Templates
And execute a macro like this,(allways have to use same directory):
Sub Macro()
AddIns.Add Filename:=Application.DefaultFilePath & "\Complement.xlam"
AddIns("Complement").Installed = True
End Sub

Overwrite file when copying into zip file using Microsoft Shell COM Library

I'm using Microsoft Shell Controls And Automation COM library from VBA. I have a zip file and I am copying a file into the zip file. The problem is that the file already exists and it throws a dialog box which says "There is already a file with the same name in this location do you want to a) Copy And Replace or b) Don't copy". I would like to programmatically say Copy And Replace (overwrite!).
I think the equivalent on the .NET framework has a documentation page at http://msdn.microsoft.com/en-us/library/windows/desktop/ms723207%28v=vs.85%29.aspx
but I have tried a variety of these.
Also, I will accept removing the original item so that the addition succeeds with no problem; sadly I could not find a delete item method.
I am using Windows 8, 64 bit edition. I'm in Excel (14) VBA (7) and the library I'm using is at C:\Windows\SysWOW64\shell32.dll
Here is head of IDL of the type library I'm talking about
// Generated .IDL file (by the OLE/COM Object Viewer)
//
// typelib filename: shell32.dll
[
uuid(50A7E9B0-70EF-11D1-B75A-00A0C90564FE),
version(1.0),
helpstring("Microsoft Shell Controls And Automation")
]
library Shell32
{
// TLib : // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
My preferred answer is the use the MoveHere method to move the old one out the way before adding. That way no dialog box is thrown. Thanks all.
Here's how you can delete a file from a zip using Shell:
Sub DeleteFromZip()
Dim fldr, zpath, itm, vrbs, vrb
zpath = "C:\_stuff\test\test.zip"
Set fldr = CreateObject("Shell.Application").Namespace(zpath)
For Each itm In fldr.items
If itm.Name = "IMG_0000322_2014.03.11.jpg" Then
itm.invokeverb "Delete"
Exit For
End If
Next itm
End Sub
This does raise a confirmation dialog though - see this link for how to avoid it:
FolderItem.InvokeVerb("Delete") without confirmation

loading csv "Could not find installable ISAM"

Im building a program in Excel VBA to automate a process and I require data stored in a csv file to be imported. So, I'm trying to use a query table to import specific columns in a csv file.
Im using ADO to interface with the csv file using the Jet Provider OLE DB. In the connection string I have specified the provider, data source, and extended properties. Im using windows xp and office 2003, so my Excel version is 8.0. I specified the provider as Microsoft.Jet.OLEDB.4.0. My code is pasted below. When the code executes I get the error "Could not find installable ISAM" at the last line that I included in the below code. I cant find anything wrong with the syntax so I was wondering if this error could be because I dont have the correct version of Jet installed? Please see code below. Thanks
Sub Excel_QueryTable()
Dim oCn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim ConnString As String
Dim SQL As String
Dim qt As QueryTable
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\testfile.csv;" & _
"Extended Properties=Excel 8.0;HDR=Yes; FMT=Delimited; IMEX=1"","
Set oCn = New ADODB.Connection
oCn.ConnectionString = ConnString
oCn.Open
Try this:
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\directory\where\csv\file\is\;" & _
"Extended Properties=""text;HDR=Yes;FMT=Delimited"""
I don't think you want to use the "Excel 8.0" portion, but you aren't actually connecting to an Excel worksheet; you're connecting to a text file. Also, you had extra spaces, and connection strings are REALLY picky about those, so I've deleted them.
Note that you can set the delimiter here:
HKLM\SOFTWARE\Microsoft\Jet\4.0\Engines\Text\Format
Mine is currently CSVDelimited; other options are TabDelimited and Delimited(;)
I found this info at: http://www.connectionstrings.com/textfile
-- EDIT --
Instead of pointing to a specific .csv file, you actually point the data source to a target directory. Note my changes above, in the connection string: the filename has been removed, and the path now ends in a \.
In order to use this type of data source, you actually need to teach the driver how the file is laid out. You do this through the use of a schema.ini file.
You can create the .ini file one of two ways:
Manually edit the file by hand, in notepad, using the above link to provide usage.
Use the tool built into the ODBC Data Source Admin.
In order to use the tool, you need to actually go through the steps of creating a data source. You don't actually need the data source, but as far as I can tell, it's the only way to get to the create schema.ini creation tool.
To get there:
Go to the ODBC control panel.
Add a new DSN, type: Microsoft Text Driver (*.txt, *.csv), click Finish
On "ODBC Text Setup", uncheck Use Current Directory and select the directory where your file lives.
Click on the options button, and click on the Define Format button.
Click on the file that you want to work with, and click "Guess" to get you started, then refine the data types as necessary.
When done, click OK, and you should see a schema.ini file in the directory where your .csv file is. Open it in notepad, and make sure it looks right.
The above connection string should now work correctly, using ONLY the directory where your csv lives, not the full path to the .csv.

Change .xla File with MSBuild

I'm trying to create a build script for my current project, which includes an Excel Add-in. The Add-in contains a VBProject with a file modGlobal with a variable version_Number. This number needs to be changed for every build. The exact steps:
Open XLA document with Excel.
Switch to VBEditor mode. (Alt+F11)
Open VBProject, entering a password.
Open modGlobal file.
Change variable's default value to the current date.
Close & save the project.
I'm at a loss for how to automate the process. The best I can come up with is an excel macro or Auto-IT script. I could also write a custom MSBuild task, but that might get... tricky. Does anyone else have any other suggestions?
An alternative way of handling versioning of an XLA file is to use a custom property in Document Properties. You can access and manipulate using COM as described here: http://support.microsoft.com/?kbid=224351.
Advantages of this are:
You can examine the version number without opening the XLA file
You don't need Excel on your build machine - only the DsoFile.dll component
Another alternative would be to store the version number (possibly other configuration data too) on a worksheet in the XLA file. The worksheet would not be visible to users of the XLA. One technique I have used in the past is to store the add-in as an XLS file in source control, then as part of the build process (e.g. in a Post-Build event) run the script below to convert it to an XLA in the output directory. This script could be easily extended to update a version number in a worksheet before saving. In my case I did this because my Excel Add-in used VSTO, and Visual Studio doesn't support XLA files directly.
'
' ConvertToXla.vbs
'
' VBScript to convert an Excel spreadsheet (.xls) into an Excel Add-In (.xla)
'
' The script takes two arguments:
'
' - the name of the input XLS file.
'
' - the name of the output XLA file.
'
Option Explicit
Dim nResult
On Error Resume Next
nResult = DoAction
If Err.Number <> 0 Then
Wscript.Echo Err.Description
Wscript.Quit 1
End If
Wscript.Quit nResult
Private Function DoAction()
Dim sInputFile, sOutputFile
Dim argNum, argCount: argCount = Wscript.Arguments.Count
If argCount < 2 Then
Err.Raise 1, "ConvertToXla.vbs", "Missing argument"
End If
sInputFile = WScript.Arguments(0)
sOutputFile = WScript.Arguments(1)
Dim xlApplication
Set xlApplication = WScript.CreateObject("Excel.Application")
On Error Resume Next
ConvertFileToXla xlApplication, sInputFile, sOutputFile
If Err.Number <> 0 Then
Dim nErrNumber
Dim sErrSource
Dim sErrDescription
nErrNumber = Err.Number
sErrSource = Err.Source
sErrDescription = Err.Description
xlApplication.Quit
Err.Raise nErrNumber, sErrSource, sErrDescription
Else
xlApplication.Quit
End If
End Function
Public Sub ConvertFileToXla(xlApplication, sInputFile, sOutputFile)
Dim xlAddIn
xlAddIn = 18 ' XlFileFormat.xlAddIn
Dim w
Set w = xlApplication.Workbooks.Open(sInputFile,,,,,,,,,True)
w.IsAddIn = True
w.SaveAs sOutputFile, xlAddIn
w.Close False
End Sub
I'm not 100% sure how to do exactly what you have requested. But guessing the goal you have in mind there are a few possibilities.
1) Make part (or all) of your Globals a separate text file that is distributed with the .XLA I would use this for external references such as the version of the rest of your app. Write this at build time and distribute, and read on the load of the XLA.
2) I'm guessing your writing the version of the main component (ie: the non XLA part) of your application. If this is tru why store this in your XLA? Why not have the main part of the app allow certain version of the XLA to work. Version 1.1 of the main app could accept calls from Version 7.1 - 8.9 of the XLA.
3) If you are just looking to update the XLA so it gets included in your version control system or similar (i'm guessing here) maybe just touch the file so it looks like it changed.
If it's the version of the rest of the app that you are controlling i'd just stick it in a text file and distribute that along with the XLA.
You can modify the code in the xla programmatically from within Excel. You will need a reference to the 'Microsoft Visual Basic for Applications Extensibility..' component.
The examples on Chip Pearson's excellent site should get you started.

Resources