Creating document macros can be easy or hard. There is however one thing that is common to most of them: we need to get hold of the path and file name of the file that was downloaded due to the file operation the user executed, be it View Document, Edit Document, Print Document or other.
Below I'm sharing a small utility macro block that you can use as a base when writing macros of your own, plus a simple and generic macro block that will open whatever file that was downloaded. The code is written with Aurena/IFS Cloud in mind but should work in IEE as well.
Firstly, here is the utility macro. Save the macro code connected to macro block called UTILS (or whatever, the name does not matter):
' Some utility functions
Public Function GetAttr (name)
On Error Resume Next
Err.Clear
Dim value
Dim obj
value = ScriptValues.Item(UCase(name)).Value
If Err.Number <> 0 Then
MsgBox "Could not get attribute """ & UCase(Name) & _
""". It might be misspelled. Error: " & Err.Description
Err.Clear
End If
GetAttr = value
End Function
Public Function GetClientAttr (name)
On Error Resume Next
Err.Clear
Dim value
Dim obj
value = ClientScriptValues.Item("LOCAL_FILE_1").Value
If Err.Number <> 0 Then
MsgBox "Could not get client attribute """ & UCase(Name) & _
""". It might be misspelled. Error: " & Err.Description
Err.Clear
End If
GetClientAttr= value
End Function
Public Function SlashToBackslash (str)
SlashToBackSlash = Replace(str, "/", "\")
End Function
Public Sub OpenFile (filename)
Set shell = CreateObject("WScript.Shell")
shell.Run filename
End Sub
Here is a generic macro that can be used for View Document as well as Edit Document (both the first edit, if no file is checked in, or the next edit, when there is a file checked in). It does not do much, it just gets hold of the file name and "opens" the file in the same way it would open if you double clicked it in Windows:
Public Sub OpenDownloadedFile
Dim filename
Dim twoNewLines
Dim answer
Dim oShell
twoNewLines = Chr(10) & Chr(10)
filename = SlashToBackslash(GetClientAttr("LOCAL_FILE_1"))
answer = MsgBox ("Do you want to open the following file? File Name:" & _
twoNewLines & _
filename & _
twoNewLines & _
"Click OK to open the file or click Cancel to skip.", 1, title)
If answer = 1 Then
Set oShell = CreateObject("Shell.Application")
' Below, "Open" can be replace by "Print" for printing the file
' instead of opening it.
oShell.ShellExecute fileName, "", "", "Open"
Else
MsgBox "Open cancelled", 0, title
End If
End Sub
Save this macro block as OPENFILE, or whatever name you like better.
The OpenDownloadedFile routine should work for at least the following macro processes:
- VIEW (View Document command)
- CHECKOUT (Edit Document command, when a file reference exists)
- CREATENEW (Edit Document command, when no file reference exists and when a file template exists to fetch the file from)
- PRINT (Print Document command)
Make sure to connect both macro blocks UTILS and OPENFILE (if you chose those names) to your macro/macro header. In Main Function, type OpenDownloadedFile, which is the main function, or subroutine in VBScript terms, that will be executed.
Trouble shooting tips
It's common to not get the macro working on the first try and it's sometimes hard to understand why a macro is not working. A common technique to understand what part of the macro that fails is to add message boxes throughout the code.
Example:
MsgBox "Hello!"
If your macro is not working, start by adding a line like above, perhaps with a unique number in the message text (so that you know what message box it is in the code), as early as possible in the macro code (as the first line below the Public Sub XYZ). Then add more like it in other parts of the macro code. Then test the macro. You will see how many of the message boxes that appears and you will eventually be able to find the place where the macro stops executing.
The next tip is also using message boxes. Instead of just displaying a static message, display a text plus the content of a variable.
Example:
MsgBox "The variable foo contains this value: " & foo
The & character above is used to concatenate the static text inside double quotes with the content of the variable foo.
In order for macros to be executed in Aurena/IFS Cloud, the Aurena Agent needs to be installed and enabled for the environment.
Happy hacking!