opening powerpoint using excel vba on a macintosh - vba

I have done a lot of research on this topic, and can't seem to find anything that works for me.
What my project does is the user fills out the excel file with data. The user presses finish which takes the excel data and auto populates it into a powerpoint. The powerpoint "called "ExcelUseThisOne") is already made and is saved in a folder (called "PowerPoint") on the desktop. I want the excel file to open the powerpoint, auto populate it, save it, then close powerpoint.
I have this working on a PC, but not a Mac.
Here is my code on opening powerpoint which I thought would work, but doesn't:
UserName = InputBox(Prompt:="You name please.", Title:="ENTER YOUR NAME", Default:="all LOWERCASE and ONE WORD")
Dim strPresPath As String, strExcelFilePath As String, strNewPresPath As String
strPresPath = ":Users:" & UserName & ":Desktop:PowerPoint:ExcelUsesThisOne.ppt"
FilePath = ":Users:" & UserName & ":Desktop:PowerPoint:NewPresentation.ppt"
strNewPresPath = FilePath
Set oPPTApp = CreateObject("PowerPoint.Application")
oPPTApp.Visible = msoTrue
Set oPPTFile = oPPTApp.Presentations.Open(strPresPath)
Any help would be great! Thank you.

I'm not certain but I seem to recall that Mac does not use file extensions, so you could try this on Mac:
strPresPath = ":Users:" & UserName & ":Desktop:PowerPoint:ExcelUsesThisOne"
Other possible reason is that the username has been entered incorrectly? I think this will work on a mac, instead of the input box you can try one of these options:
strPresPath = ":Users:" & Environ("username") & ":Desktop:PowerPoint:ExcelUsesThisOne"
Or try that approach with the file extension (again, I'm not 100% certain about the file extensions not used on Mac OS).
You may need to volume name, e.g.,:
strPresPath = "volume_name:Users:..."`
Otherwise, you can debug by checking the path of some other workbook, which should show you the correct way to formulate the path string.
Good luck!

Related

Trying to Open a File with a Button from Access using ShellExecute

In my Access database, I have a button on a form to open an external file.
Here is the code that I am using for that.
Private Sub btn_OpenFile_Click()
Dim a As New Shell32.Shell
Dim strPath As String
strPath = Me.Attachment
strPath = Chr(34) & strPath & Chr(34)
Call a.ShellExecute(Me.Attachment)
'Call CreateObject("Shell.Application").ShellExecute(strPath)
'MsgBox strPath
End Sub
The problem that I have is if I actually put in the value of the variable (Me.Attachment) it works fine and opens the program and the file.
For Example, If I put in Call a.ShellExecute("C:\Docs\Some File.pdf") it will open. But if I use the variable in it's place it won't open and tells me it cannot find the file. I have verified with the msgbox that it is receiving the correct information. I have tried to wrap it in quotes and have used the Chr(34) as shown above but nothing works.
How can I get that variable to work in the ShellExcute command?
I have looked through all the forums and it seems like everyone is using a string but not a variable. I don't want to use just the shell command as I don't want to track down all of the different apps people use to open different types of files. There will be different file types that will need to be opened and I thought this would be easier than it actually is.
Thank you for the help.
I'm pretty sure that ShellExecute expects a Variant parameter, not a String.
So try this:
Call a.ShellExecute(CVar(strPath))
or use a Variant variable from the start.
I had the same problem here.
Both of the following work for me:
Dim a As New Shell32.Shell
Dim strPath As String
strPath = Me.Attachment
Call a.ShellExecute(strPath)
Dim a As Shell
Dim strPath As String
strPath = Me.Attachment
Set a = CreateObject("Shell.Application")
a.ShellExecute strPath
Even referencing Attachment directly.
a.ShellExecute(Me.Attachment)

Copy file with progress bar bypass file replacement confirmation

This is a follow up to this question and great answer:
Copy files with progress bar
So I added the code from Siddharth Rout's answer and it does exactly what I want to happen with a minor exception. When I copy the files, I am looping through each file in the directory and copying it up as long as it is not *List.xml. Because I am replacing an existing library the 97% of the documents are pre-existing and I get prompted to replace existing documents each time.
Is there a way to get it to prompt me to choose to replace for all files? Do I need to reformat/structure the sequence of my code?
Function UploadToSharepoint(Folderpath As String, Foldername As String, Filenames() As String, SharepointLinks() As String) As Boolean
'upload file to sharepoint library based on the folder name
Dim SharePointLib As String
Dim LocalAddress As String
Dim DestinationAddress As String
Dim xCounter As Long
On Error GoTo loadFailed
Pickafolder:
Folderpath = FolderPick
Foldername = Left(Folderpath, Len(Folderpath) - 1)
Foldername = RIght(Foldername, Len(Foldername) - InStrRev(Foldername, "\"))
Select Case Foldername
Case "OPSS", "SSP", "OPSD", "MTOD", "SSD"
SharePointLib = "\\my.company.com\Subsite\" & Foldername & "\"
Case "West", "Eastern", "Northeastern", "Northwestern", "Head Office"
SharePointLib = "\\my.company.com\Subsite\NSP\" & Foldername & "\"
Case "NSP", "NSSP"
MsgBox "Pick the NSP regional sub folder: West, Eastern, Northeastern, Northwestern, Head Office"
GoTo Pickafolder
Case Else
MsgBox "Inappropriate directory to upload from. Please select one of the CPS download directories"
GoTo Pickafolder
End Select
Filenames = GetFilesDir(Folderpath)
ReDim SharepointLinks(LBound(Filenames) To UBound(Filenames))
For xCounter = LBound(Filenames) To UBound(Filenames)
LocalAddress = Folderpath & Filenames(xCounter)
DestinationAddress = SharePointLib & Filenames(xCounter)
'**********************************************************
Call VBCopyFolder(LocalAddress, DestinationAddress)
'**********************************************************
SharepointLinks(xCounter) = "#http:" & Replace(DestinationAddress, "\", "/") & "#"
Next xCounter
UploadToSharepoint = True
Exit Function
loadFailed:
UploadToSharepoint = False
End Function
And by the looks of things I am not excluding the file I was referring to earlier...must be doing that else where.
Update
Based on comment received at the linked question, the solution is to declare a public constant at the start:
Public Const FOF_NOCONFIRMATION As Long = &H10
and then in the copy procedure change the line of code to:
.fFlags = FOF_SIMPLEPROGRESS Or FOF_NOCONFIRMATION
Now, this does solve the problem of being constantly asked to confirm the replacement. I am very happy about this. The problem now is the progress window displays for the first file to be copied then disappears but fails to reappear for subsequent files. The remaining files still get copied and the prg carries on like it's supposed to. The whole point of the progress bar though was to let people know that "THINGS" were still happening in the background and now that is not happening. Is there something I need to adjust?
Update 2
After running my code and choosing a source directory on the network drive instead of the local computer, the copy window is popping up for every single file like I was expecting. I notice that sometimes the progress bar closes before reaching 100%. This leads me to believe that since the file sizes are so small that when it is copying from my local drive to sharepoint, the operation completes so fast that it does not have time to draw and update the progress window before its time to close it.

MS Word VBA - open file with parameters, string longer than 255 chracters

I'm developing a MS Word macro which needs to open a file on a network drive and pass it the calling file's path as a parameter (i can then retrieve the parameters in the opened file using this method http://www.vbaexpress.com/forum/archive/index.php/t-21174.html).
What i am trying to achieve is the following:
1. Document X (any MS word document) calls document Y (macro document)
2. Document Y processes document X (using the Document object)
3. Document Y closes
The reason i am doing step 1 above is do that users don't have to deploy complex vba code (i am dealing with non IT literate users) and the ease of making updates and enhancements to the code if required.
The following code snippet opens the file with parameters:
Dim currentFilePath As String
currentFilePath = ThisDocument.Path & ThisDocument.Name
Dim MacroFilePath As String
MacroFilePath = ThisDocument.Path & "\Test.docm"
MacroFilePath = """" & MacroFilePath & """" & currentFilePath
Documents.Open (MacroFilePath)
The value of 'MacroFilePath' is gets setup like this (263 chars):
“\\XXXXXXXXXXXX\XX_XX\XXX_XXX XXXX procedural documentation\XX Design Support\Macros - DO NOT MOVE\Work in progress\Calling Document.docm” \\XXXXXXXXXXXX\XX_XX\XXX_XXX XXXX procedural documentation\XX Design Support\Macros - DO NOT MOVE\Work in progress\Test.docm
When I run the above code the error Run-Time '9105': String is longer than 255 characters occurs. I have tested the code where i moved the files to a shorter directory and it works. Is there a way to get around this or another way of achieving what i am trying to do?
Shorting the file paths by saving the documents elsewhere, changing the language i am programming in, or creating any kind of executable is not an option as i am in an enterprise environment.
X can open Y, then call a procedure in Y and pass in it's own path as a parameter.
You can use Application.Run to do this.
See:
https://msdn.microsoft.com/en-us/library/office/ff838935.aspx
Here's the example from that link:
Dim strTemplate As String
Dim strModule As String
Dim strMacro As String
Dim strParameter As String
strTemplate = InputBox("Enter the template name")
strModule = InputBox("Enter the module name")
strMacro = InputBox("Enter the macro name")
strParameter = InputBox("Enter a parameter value")
Application.Run MacroName:=strTemplate & "." _
& strModule & "." & strMacro, _
varg1:=strParameter

VB2010: Create shortcut on desktop programmatically that runs as administrator

I am using this way below to create shortcuts on user's desktop. I also want them to run as administrator. I found this, here in StackOverflow but... I want to do the same thing programmatically, not by the hand.
Here is my code:
Private Sub CreateShortcuts()
Dim NewDir = AppFolder.Text
Dim WSH As Object = CreateObject("WScript.Shell")
WSH = CreateObject("WScript.Shell")
Dim MyShortcut, DesktopPath
DesktopPath = WSH.SpecialFolders("Desktop")
MyShortcut = WSH.CreateShortcut(DesktopPath & "\Application Controller.lnk")
MyShortcut.TargetPath = WSH.ExpandEnvironmentStrings(NewDir & "\Application.exe")
MyShortcut.WorkingDirectory = WSH.ExpandEnvironmentStrings(NewDir)
MyShortcut.WindowStyle = 1
MyShortcut.IconLocation = NewDir & "\Application.exe"
MyShortcut.Save()
End Sub
I think you are searching for the 'runas' command. You can say that your command line arguments for the target program is:
runas /savecred /user:administrator "yourprogrampathhere.exe"
Just make sure that the user named 'administrator' does exist. BTW, there is a flag on the Properties window of the shortcut, I just don't know how to do it with the WShell way, but this solution might work.
This line needs to change:
MyShortcut.TargetPath = WSH.ExpandEnvironmentStrings("runas /savecred /user:administrator """ & NewDir & "\Application.exe""")
If you're wondering why I put too many quotation marks, don't worry. They're used to escape the initial quotation mark. Have a wonderful day, and I hope your program will work!
EDIT: Using this will prompt you for an Administrator password. You must have one of that, because empty passwords are not allowed. The password will be saved and won't be prompted again until next system reboot.

Excel VBA error using WScript.Shell.Run

After recently upgrading from Excel 2010 to Excel 2013, I moved a custom add-in (.xlam) to the new Application.LibraryPath directory (C:\Program Files\Microsoft Office 15\root\office15\Library\BTRTools). There is a bit of code that launches an executable (exe) file (located in sub directory of the add-in). However, since the upgrade/move, I am not getting an error:
PrettyPrintXml.exe - Application Error
The application was unable to start correctly (0xc000007b). Click OK to close the application.
I'm obviously pretty convinced it is file permissions. I have explicitly added myself permissions with full rights to the \Library folder (and all subs). Note that I think I had to do this even with Excel 2010 (folder at C:\Program Files (x86)\Microsoft Office\Office14\Library) to make things work.
However, after all this, I'm still stuck and can not launch the exe file. Any ideas/suggestions on how to make this work?
Code is pretty standard:
Public Sub RunShellExecute(sFile As String, Optional params As String = "", Optional wait As Boolean = False)
Dim wsh As Object: Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = wait
Dim windowStyle As Integer: windowStyle = 1
Dim exe As String: exe = IIf(Left(sFile, 1) <> """", """" & sFile & """", sFile)
Dim exeParams As String: exeParams = IIf(params <> "", " " & params, "")
Dim errorCode As Integer: errorCode = wsh.Run(exe & exeParams, windowStyle, waitOnReturn)
If errorCode = 0 Then
'// MsgBox "Done! No error to report."
Else
MsgBox "Program exited with error code " & errorCode & "."
End If
End Sub
I know your question is "Why doesn't this work", but I thought you might be interested in an alternate solution: There is a native VBA PrettyPrintXML. You need to add a reference to the MSXML library in your VBA project by clicking "Tools" ---> "References..." and then check the box next to Microsoft XML, v6.0 (or whatever version is included with your version of Office/Windows).
Please change the title of your question, because Excel VBA is able to use WScript.Shell.Run, otherwise you wouldn't be getting your error.
As for the actual issue, this looks like a 32-bit / 64-bit problem. Investigate whether the program you're calling is appropriate for your system and whether it tries to load the right DLLs.
The problem is not file permissions, then you would get a different status code.
You should use a path without spaces in it, something simple like 'C:\BTRTools'. Then it should work.