Charts template folder - vba

I've developed a superuseful addon which recreates source data for charts "disconnected" from Excel. So I .SaveChartTemplate then ApplyChartTemplate to a new chart, and need to delete temp. template.
Don't know how to get chart template folder. I assume that it's Environ("AppData") & "\Microsoft\Шаблоны\Charts\" but it's obvious that it works only with russian locale.

After analyzing registry keys, i've come to realize that templates folder name can be found in HKCU\Software\Microsoft\Office\<OFFICE VERSION>\Common\General\Templates
Assuming the above a possible solution is
Function chartTemplatesFolder() As String
On Error GoTo er:
Dim templatesFolder As String
templatesFolder = CreateObject("WScript.Shell").RegRead( _
"HKCU\Software\Microsoft\Office\" & Application.Version & "\Common\General\Templates")
chartTemplatesFolder = Environ("AppData") & "\Microsoft\" & templatesFolder & "\Charts\"
Exit Function
er:
End Function

Related

"Kill" / "FileCopy" leads to permission denied

I'm having a problem with my Access VBA macro which for some reason is breaking on those lines with error "Permission Denied [Run-time error 70]:
sOutputFile = CurrentProject.Path & "\Output Files\Recon\" & sDate & " " & sClient & " Cash Recon.xlsx"
sTemplateFile = CurrentProject.Path & "\Temp Files\Template_Listed.xlsx"
If Dir(sOutputFile) <> "" Then Kill sOutputFile
FileCopy sTemplateFile, sOutputFile
What's getting "pointed out" directly is the "Kill sOutputFile" phrase.
It is worth to mention, that I do not have any of the files open, I have full access to the directories, and not long ago (before declaring sOutputFile and sTemplateFile) they've been both cleared.
Any help is much appreciated and I'm willing to share more of my code if needed.
Edit: Also, from time to time, the macro goes to the next line and breaks at the FileCopy instead.
The logic is not quite right in my opinion, please try the following (FileCopy should be inside an If...End If):
sOutputFile = CurrentProject.Path & "\Output Files\Recon\" & sDate & " " &
sClient & " Cash Recon.xlsx"
sTemplateFile = CurrentProject.Path & "\Temp Files\Template_Listed.xlsx"
If FileExists(sOutputFile) Then
Kill sOutputFile
FileCopy sTemplateFile, sOutputFile
End If
And this is the FileExists function:
Public Function FileExists_1(sFileName As String) As Boolean
Dim obj_fso As Object
Set obj_fso = CreateObject("Scripting.FileSystemObject")
FileExists_1 = obj_fso.fileExists(sFileName)
Set obj_fso = Nothing
End Function
What was the issue in my case, was that I previously used the macro, but the full ran failed. After trying to run it again after few adjustments (not related to the code I posted here), the above issue was happening. All just because I had the file still open somewhere in my excel's memory, hence the file couldn't be deleted.
Thanks for all the contributions guys. You're amazing as always!

Changing the Folder security permissions using Excel VBA

I am trying to write some code to put a text file into a secure folder. The folder has attributes already set to read-only so that the files within are secure and cannot be altered but still read.
The FileSystemObject will allow me to use the attribute property which I can set to 1 (read-only) but this is easily overridden.
My next port of call was to GetAclInformation etc.
I downloaded some code and I got through a large portion of it but, at GetAclInformation it crashes Excel.
I then continued to look and so used the ADsSecurity dll. This returns an error stating
the ActiveX cannot create the object.
I have downloaded a copy of the dll and put it into the windows\syswow64 directory and then registered it with RegSvr32 which returned a success.
I can add the references required and see the object in the object viewer. But trying both late and early binding has no affect and it still errors saying the ActiveX cannot create the object.
Does anyone have any other ideas or a suggestion on what to try?
Sub TestApproval()
Dim oSec As New ADsSecurity
Dim oSd As Object, oDac1 As Object, oAce As Object
Set oSec = New ADsSecurity
Set oSd = oSec.GetSecurityDescriptor(CStr("FILE://C:\Test"))
Set oDac1 = oSd.DiscretionaryAcl
For Each oAce In oDac1
Debug.Print oAce.trustee & "|" & oAce.AceType & "|" & oAce.AccessMask & "|" & oAce.AceFlags & "|" & oAce.Flags & "|" & oAce.ObjectType & "|" & oAce.InheritedObjectType
Next oAce
Set oSec = Nothing
Set oSd = Nothing
Set oDac1 = Nothing
End Sub
Thanks in advance :)

Store file inside VBA Project and open it

I have created a VBA Application in MS Excel 2010. It has one user form. There I'd like to add a feature to open a (MS Word) file for support and FAQ purposes. I do not want to save the file at a central location and open the file then via VBA. Is there a possibility to store the file maybe inside the vba project?
You can embed an object in an Excel Worskeet (Insert -> Object). If you click on the embedded object, in the upper left corner you will see the name of the object (e.g. "Object 7"). With that you can open it in vba via
Sub openEmbed()
Dim ole As OLEObject, wdoc As Word.Document
Set ole = Worksheets("Sheet1").OLEObjects("Object 7")
ole.Activate
Set wdoc = ole.Object
End Sub
You could store the content as XML in the VBA and then insert it with InsertXML in a new document:
Dim app As Object
Set app = CreateObject("Word.Application")
app.Visible = True
app.Documents.Add.Content.InsertXML "<?xml version=""1.0""?><abc:books xmlns:abc=""urn:books"" " & _
"xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " & _
"xsi:schemaLocation=""urn:books books.xsd""><book>" & _
"<author>Matt Hink</author><title>Migration Paths of the Red " & _
"Breasted Robin</title><genre>non-fiction</genre>" & _
"<price>29.95</price><pub_date>2006-05-01</pub_date>" & _
"<abstract>You see them in the spring outside your windows. " & _
"You hear their lovely songs wafting in the warm spring air. " & _
"Now follow their path as they migrate to warmer climes in the fall, " & _
"and then back to your back yard in the spring.</abstract></book></abc:books>"

Is it possible to add an XML "log" using CustomXMLParts in Excel VBA?

I have project that require I maintain log of users (using Application.UserName) that perform a certain actions with a macro-enabled Excel spreadsheet. I've tried using the VB examples found at the links list below and modifying for VBA, but I'm running into error at last line of Sub. Can anyone point me towards a better VBA example or guide me in the right direction?
http://msdn.microsoft.com/en-us/library/bb608627.aspx
http://msdn.microsoft.com/en-us/library/bb608612.aspx
Here's the code I'm currently working with.
Private Sub AddCustomXmlPartToWorkbook(ByVal Workbook As Excel.Workbook)
Dim xmlString As String
xmlString = _
"<?xml version=""1.0"" encoding=""utf-8"" ?>" & _
"<employees xmlns=""http://schemas.microsoft.com/vsto/samples"">" & _
"<employee>" & _
"<name>Karina Leal</name>" & _
"<hireDate>1999-04-01</hireDate>" & _
"<title>Manager</title>" & _
"</employee>" & _
"</employees>"
Dim employeeXMLPart As Office.CustomXMLPart
employeeXMLPart = ActiveWorkbook.CustomXMLParts.Add(xmlString)
End Sub
You just run the statement that adds the CustomXMLPart:
Call ActiveWorkbook.CustomXMLParts.Add(xmlString)
It is added to the end of the collection, so you know it's position by doing ActiveWorkbook.CustomXMLParts.Count

FileDialog in Word 2011 VBA

I'm hoping for a bit of a sanity check. I'm adapting a Word add-in (written in VBA for Word 2010) for Mac, specifically, at this point, Word 2011. I'm aware of many of the differences, but one that I haven't been able to find much documentation on is the apparent lack of FileDialog. The closest I've come to an answer is here: http://www.rondebruin.nl/mac.htm where the author uses Application.GetOpenFilename. That method doesn't seem to exist for Word, though (the focus of that site is Excel).
Does anyone know how to use the file and folder picker dialogs that FileDialog makes available? I'm not familiar with Applescript, really, but I've had to learn a little in order to get around Word 2011's funky file management issues (Dir, FileCopy, etc.). So, if that's the answer, any sense of what the code might look like in Applescript would be greatly appreciated. (I more or less know how to translate that into VBA).
I believe you have to use Apple Script in order to do this a bit better on the Mac. The following code allows the user to select text files which is returned as an array from the function. You would simply be able to modify the Apple Script to return other file types and select directories, I'll leave that to you.
The code that calls the function and displays a message box with all the files:
Sub GetTextFilesOnMac()
Dim vFileName As Variant
'Call the function to return the files
vFileName = Select_File_Or_Files_Mac
'If it's empty then the user cancelled
If IsEmpty(vFileName) Then Exit Sub
'Loop through all the files specified
For ii = LBound(vFileName) To UBound(vFileName)
MsgBox vFileName(ii)
Next ii
End Sub
And the function that does the Apple Script work:
Function Select_File_Or_Files_Mac() As Variant
'Uses AppleScript to select files on a Mac
Dim MyPath As String, MyScript As String, MyFiles As String, MySplit As Variant
'Get the documents folder as a default
On Error Resume Next
MyPath = MacScript("return (path to documents folder) as String")
'Set up the Apple Script to look for text files
MyScript = "set applescript's text item delimiters to "","" " & vbNewLine & _
"set theFiles to (choose file of type " & " {""public.TEXT""} " & _
"with prompt ""Please select a file or files"" default location alias """ & _
MyPath & """ multiple selections allowed true) as string" & vbNewLine & _
"set applescript's text item delimiters to """" " & vbNewLine & _
"return theFiles"
'Run the Apple Script
MyFiles = MacScript(MyScript)
On Error GoTo 0
'If there are multiple files, split it into an array and return the results
If MyFiles <> "" Then
MySplit = Split(MyFiles, ",")
Select_File_Or_Files_Mac = MySplit
End If
End Function
Finally, it can be a bit of a pain specifying different file types, if you want to specify only Word documents, then replace public.TEXT with com.microsoft.word.doc, however this won't allow .docx or .docm files. You need to use org.openxmlformats.wordprocessingml.document and org.openxmlformats.wordprocessingml.document.macroenabled respectively for these. For more info on these see: https://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html