Embedding files in exported Microsoft Word documents
 
Product:
 
Export Utility for Lotus Notes / Domino
>
Export to MS-Word
>
Version
All
 
Platform(s):
 
Windows
 
Edition(s):
 
All
 
Doc Number:
 
1000110
Published
04-Mar-2018
 General Information
This support note provides LotusScript code you can use in the Post-Export custom script section of the export profile to convert all exported RTF files to DOCX files and then embed related file attachments in the Word file.
 Solution
Please note: the following LotusScript code is provided as-is and has been tested with Microsoft Word 2016. It may not work under all circumstances. It is provided as a convenience only and is not supported. If you make improvements to the code to make it work better or resolve an issue you experienced please let us know so we can update the information posted here for the benefit of everyone.
Open the export profile document for the Microsoft Word export, put it into Edit mode then perform the steps contained below.
Setting up the Export Profile document
For the provided code to work the export profile document must be setup to perform the following:
Create all target Microsoft Word documents in a specific folder
Export all related file attachments to a subfolder where the subfolder name is based on the parent document it relates to
Ensure file attachments icons are not output in the target Microsoft Word file.
Create all Microsoft Word documents in a specific folder
Under the 'Export Options -> Output File' tab set the following fields:
Existing export files: This should be set to 'Do Not Overwrite'
Output folder: Select or specify the name of the target folder to create the Microsoft Word files in for the exported documents
File Attachments Folder
Under the 'Export Options -> Attachments' tab set the following fields:
Export Attachments: Yes
Attachment Icons: Remove attachment icons
OLE Objects: Convert to file attachment and export
Save Attachments: To sub-folder of exported file on filesystem
Overwrite existing files: No
File Attachment Icons
Under the 'Export Options -> Rendering Options' tab set the following fields:
Include Attachment Icons: This should be set to 'No'
Adding the Post-Export Custom Script
Here are the steps to implement the code:
Go to the Custom Scripts -> Post Export section of the export profile.
Click the checkbox next to 'Run this script on next export' near the top of the section.
Paste the code below into the field that appears underneath the 'Enter the custom script in the field below' label.
Make any required customizations to the code.
Run the export as usual
Important notes:
The SourceFolder in the code below should be edited so that it matches the Output folder specified in the export profile document
The TargetFolder is where the converted DOCX files will be saved to and should be different from the source folder.
******************** Copy everything below this line ********************
Option Declare
Use "Common"
Const SOURCE_FILE_EXT = "rtf"
Const TARGET_FILE_EXT = "docx"
' MS-Word Constants
Const wdFormatDocument = 0
Const wdFormatDocumentDefault = 16
Const wdCurrent = 65535
Const wdMove = 0
Const wdStory = 6
Dim TargetFolder As String
Dim TotalFilesConverted As Long
Sub Initialize
Dim WordApplication As Variant
Dim FileName As String
Dim FileNameList List As Integer
Dim FilePattern As String
Dim FoldersList List As Integer
Dim FolderName As String
Dim NumFiles As Long
Dim NumFolders As Long
Dim SourceFolder As String
Dim SubFolderName As String
Dim TargetSubFolder As String
' Error Handler
On Error Goto Error_Handler
' Set the folder containing all the RTF files to be converted (make sure it ends with backslash)
'SourceFolder = "c:\Temp\Export\Word\"
' Specify the source folder
SourceFolder = ExportProfileDoc.OutputFolder(0) ' Get the source folder from the export profile document
' Set the folder to save the converted Docx files to (make sure it ends with backslash)
TargetFolder = "c:\Temp\Export\WordDocx\"
Set WordApplication = Nothing
Err = 0
On Error Resume Next
Set WordApplication = CreateObject("Word.Application")
If Not IsObject(WordApplication) Or Err <> 0 Then
' Application not successfully initialized - Try GetObject method
Err = 0
Set WordApplication = GetObject("", "Word.Application")
End If
If Not IsObject(WordApplication) Or Err <> 0 Then
Messagebox "Post export custom script error: The Word Application could not be instantiated. Please ensure it is installed on this machine.", 0+16, "Cannot Initialize Word"
Exit Sub
End If
' Make sure the target folder exists
TargetFolder = FileSystemObject.CreateFolder(TargetFolder)
On Error Goto Error_Handler
WordApplication.Visible = False
WordApplication.DisplayAlerts = False
' Get all the rtf files in the base folder
FilePattern = "*." & SOURCE_FILE_EXT
Call FileSystemObject.GetFilesInFolder(SourceFolder, FileNameList, NumFiles, FilePattern)
If NumFiles > 0 Then
' Files were found
Print "Converting " & Trim(Cstr(NumFiles)) & " files in " & SourceFolder
Call ProcessDocFiles(FileNameList, WordApplication)
End If
Exit_Sub:
On Error Resume Next
Print Trim(Cstr(TotalFilesConverted)) & " files were converted"
' Close Word
If Not WordApplication Is Nothing Then
Call WordApplication.Quit
Set WordApplication = Nothing
End If
Exit Sub
Error_Handler:
Call Output_Error("Post Export Custom Script - Initialize", Erl, True, True)
Resume Exit_Sub
End Sub
Function ProcessDocFiles(FileNameList As Variant, WordApplication As Variant) As Boolean
' Process the rtf files in the specified folder
Dim WordDoc As Variant
Dim WordSelection As Variant
Dim AttachmentsList List As Integer
Dim BaseFileName As String
Dim LookupAttachmentsFolder As String
Dim NumAttachments As Long
Dim SourceFileName As String
Dim TargetFileName As String
Dim ThisFilename As String
' Error Handler
On Error Goto Error_Handler
ForAll v_FilePathName In FilenameList
SourceFileName = ListTag(v_FilePathName)
BaseFileName = SourceFileName
If InStr(1, BaseFileName, "/") > 0 Then
BaseFileName = StrRightBack(BaseFileName, "/")
ElseIf InStr(1, BaseFileName, "\") > 0 Then
BaseFileName = StrRightBack(BaseFileName, "\")
End If
TargetFileName = BaseFileName
If Instr(1, TargetFileName, "." & SOURCE_FILE_EXT, CASE_INSENSITIVE) > 0 Then
TargetFileName = Strleftback(TargetFileName, "." & SOURCE_FILE_EXT, CASE_INSENSITIVE)
End If
' Set the target filename
TargetFileName = TargetFolder & TargetFileName & "." & TARGET_FILE_EXT
Print "Converting to docx: " & BaseFileName & " to " & TargetFileName
' Open the file
Set WordDoc = WordApplication.Documents.Open(SourceFileName, False)
' Get all the files to be attached
LookupAttachmentsFolder = SourceFileName
If Instr(1, LookupAttachmentsFolder, "." & SOURCE_FILE_EXT, CASE_INSENSITIVE) > 0 Then
LookupAttachmentsFolder = Strleftback(LookupAttachmentsFolder, "." & SOURCE_FILE_EXT, CASE_INSENSITIVE)
End If
LookupAttachmentsFolder = LookupAttachmentsFolder & "\"
Erase AttachmentsList
NumAttachments = 0
Call FileSystemObject.GetFilesInFolder(LookupAttachmentsFolder, AttachmentsList, NumAttachments, "*.*")
If NumAttachments > 0 Then
' Set focus to end of file
Set WordSelection = WordApplication.Selection
Call WordSelection.EndKey(wdStory, wdMove)
Print "Attaching " & Trim(Cstr(NumAttachments)) & " files to " & TargetFileName
Call WordSelection.TypeParagraph()
Call WordSelection.TypeParagraph()
ForAll v_AttachmentFile In AttachmentsList
ThisFileName = ListTag(v_AttachmentFile)
If Trim(ThisFileName) <> "" Then
BaseFileName = ThisFileName
If InStr(1, BaseFileName, "/") > 0 Then
BaseFileName = StrRightBack(BaseFileName, "/")
ElseIf InStr(1, BaseFileName, "\") > 0 Then
BaseFileName = StrRightBack(BaseFileName, "\")
End If
Print "Attaching file: " & BaseFileName
Call WordSelection.InlineShapes.AddOLEObject(, ThisFileName, False, True, , , BaseFileName)
End If
End Forall
End If
' Save the RTF file as a docx
'Call WordDoc.SaveAs2(TargetFileName, wdFormatDocumentDefault, wdCurrent )
Call WordDoc.SaveAs2(TargetFileName, wdFormatDocumentDefault, , , , , , , , , , , , , , , wdCurrent)
' Close the file
Call WordDoc.Close
Set WordDoc = Nothing
TotalFilesConverted = TotalFilesConverted + 1
End Forall
ProcessDocFiles = True
Error_Handler:
Call Output_Error("Post Export Custom Script - ProcessDocFiles", Erl, True, True)
Exit Function
End Function
******************** Copy everything above this line ********************
You may get an error similar to the following when the code attempts to embed a PDF file in the Microsoft Word document:
This is normally due to the protected mode enabled for the installed Adobe Reader application. To disable the protected mode follow these steps:
Open the Adobe PDF Reader application
Select 'Edit' from the menu bar and then select 'Preferences'
Select the 'Security (Enhanced)' option
Uncheck the 'Enable Protected Mode at Startup' option (see the following image)
Click the Ok button to save the changes
Exit out of the Adobe PDF Reader application then try running the export again.
©
2018
AGE Computer Consultancy. All rights reserved.
Material may not be reproduced or distributed in any form without permission.