A basic document macro to build from


Userlevel 7
Badge +30

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!
 


13 replies

Userlevel 2
Badge +6

Hi Matthias, 

thanks a lot .

Have a nice day

Userlevel 5
Badge +11

Hi @Mathias Dahl,

I'm in 22R2 and I wanted to test your macro but it didn't work.
No macro is available in the Word file.
I added everything I needed in the "Document Macro Block", "Document Macros", "Document Class Process Action" and "Document Class Management" screens, but nothing helped.
The aurena agent is active.

Any ideas?
Thanks in advance

Userlevel 7
Badge +30

Hi,

What did you mean by "No macro is available in the Word file"? Did you expect to find a macro in the Word file?

I think you need to show screenshots of the setup here, otherwise it's hard to see if you did anything wrong.

 

Userlevel 5
Badge +11

Hi Mathias,

Thank you for your reply.
I took the macro from this post and I thought, when displaying the word file, that I would see the dialog box ("MsgBox" function).

To tell you the truth, we're trying to put into practice the documentation available in V10 "Document Templates" (the aim of which is to add certain values from our IFS screens to a Word document).
Only, the procedure works in V10 but not in the cloud.
Is the same documentation available in the cloud? Or what more needs to be done for the procedure to work in the cloud?
Thanks in advance.

Userlevel 7
Badge +30

Editing documents and running macros to fill in information in the file that opens is working in IFS Cloud, but there might be some page in your version where it doesn't work correctly.

If you can show screenshots of all the configuration that you have been trying to setup, and when you try to use it, we can try to spot the problem.

Userlevel 5
Badge +11

Hi Mathias,

All right, I'll tell you how I did it: 
1. I have my macro blocks that I used in V10 and that worked very well: 

2. I created a document class:

3. I've linked my macros to my class: 

4. In the "Document Class Process Action" screen, I created a line for each process used:

5. I want to retrieve attributes from the customer order header, so I've specified the LU "CustomerOrder" and some attributes:

6. Now, if I want to create a Word document in the screen and header of the customer order :

7. I can't find my attributes from Word when I "Checkin" the document:

What can I do?
Thanks in advance.

Userlevel 7
Badge +30

Hi, thanks for that! That macro is not the simple MsgBox macro, is it? 🙂 I cannot help you debug that, sorry. This is not the product of R&D I'm afraid and it's too complex for me to spend time on debugging it.

Can you configure the system such that it will only try to run a simple "MsgBox" macro?
 

Userlevel 5
Badge +11

Hi @Mathias Dahl,

Thank you for your reply.
I tried to trigger a simple "MsgBox" macro, but nothing happens in my Word file...
However, everything is going very well in APPS10.
I understand that there were problems between Chrome & Aurena Agent, but I tried on Edge and I have exactly the same problem..
Do you have an idea?
For information, I am in 22R2.

Userlevel 7
Badge +30

Can you show us the code of the macro blocks in the MsgBox macro? If it only contains a message box call, Word will not open automatically.

Userlevel 5
Badge +11

To test the macros, I set up the 2 macro blocks specified in the post: 


In terms of class management, I've of course included my macro lines for each type of operation:


If I click on "View document" in the document revision, nothing happens.

Is something missing?
Thanks in advance.

Userlevel 7
Badge +30

Before you click View Document, press F12 and select the Network tab. There, if the Aurena Agent is installed, you should see a call like this:

That's where we check if any macro is available for that document (using the document class process action, file type and class to figure it out).

Userlevel 5
Badge +11

Hi @Mathias Dahl,

I've just checked, and I do have an existing macro: 
 

However, the file doesn't open automatically (I have to go to the "IFS" folder in my documents and open it).
When I open it, I get absolutely no msgBox.
Is this normal? Is the problem due to my Word version?
For your information, it's a Word 365 64 Bits.

Thanks a lot!

Userlevel 7
Badge +30

Before we conclude if that macro works or not, you need to test with just a simple macro that shows a message box. The documentation on macros has one. Only when we see that works you can try a more advanced macro. So try that first, tell us what happens and show the macro code and the rest of the macro setup.

Btw, the macro does not open when Word opens. The macro runs when Aurena execute it. And, when a macro runs it's the job of the macro to open the document file. So in the message box example the document would not open.

 

Reply