Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
THRUST SERVICES & TOOLS
CLOUD EDITIONS
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
CopyAndOpenCmd in Word VBA with no UI
SirHC
Is there a way to prevent the New Profile dialog appearing when using the CopyAndOpenCmd within Word?
I am trying to take a copy of a document, and only change the description and folder/location, without the user being prompted (it's all part of a batch process within Word that opens a bunch of documents that are used as templates, does a simple find/replace and saves the new documents into a specific folder)
Other than that, is there a better way of acheiving the same result without using CopyAndOpenCmd - like opening the file (OpenROCmd?), saving the file locally and then importing?
Regards
Chris
Find more posts tagged with
Comments
jny
I'm not sure I understand how you are doing this. From which end-user operation are you intending to run a silent CopyAndOpen-like command?
SirHC
Hi,
Sorry for the long time between posts (got distracted on another project as always!)
The end users will simply be clicking on a macro button within Word which will then go and open the appropriate documents and process them.
I have written some code to try explain what I am trying to do. The Test Sub is essentially what is behind the macro button (obviously not hard coded document numbers and versions etc!)
===================
Sub Test()
Dim objDatabase As IManage.NRTDatabase
Dim RC As Long
RC = GetDatabase("Development", objDatabase)
If RC = 1 Then ' Successfully retrieved database object
Dim objDocument As NRTDocument
Set objDocument = objDatabase.GetDocument("1234567", "1")
Call CopyAndOpenDocument(objDocument)
Set objDocument = Nothing
End If
End Sub
Public Function CopyAndOpenDocument(ByRef Document As IManage.IManDocument, Optional ByRef NewDocument As IManage.IManDocument) As Boolean
On Error Resume Next
Dim objCommand As IMANEXTLib.CopyAndOpenCmd
Dim objContextItems As IMANEXTLib.ContextItems
Dim objSelectedDocs(0) As IManage.NRTDocument
CopyAndOpenDocument = False
Dim hwnd As Long
hwnd = FindWindow("OpusApp", Null)
Set objSelectedDocs(0) = Document
'Create command and contextitems objects
Set objCommand = New IMANEXTLib.CopyAndOpenCmd
Set objContextItems = New IMANEXTLib.ContextItems
'Pass NRTDocument object to command
objContextItems.Add "SelectedNRTDocuments", objSelectedDocs
'Pass parent window handle
objContextItems.Add "ParentWindow", hwnd
' Initialize the command with populated ContextItems collection
objCommand.Initialize objContextItems
' Use the Update method to find out if the command is available,
' based on the information posted to the ContextItems collection
objCommand.Update
' If the command is active, it can be executed.
If objCommand.Status = (objCommand.Status And nrActiveCommand) Then
objCommand.Execute
Dim bRefresh As Boolean
bRefresh = objContextItems.item("IManExt.Refresh")
If bRefresh = True Then
Dim pImportedDoc As IManage.NRTDocument
Set pImportedDoc = objContextItems.item("ImportedDocument")
If Not pImportedDoc Is Nothing Then
If Not IsMissing(NewDocument) Then Set NewDocument = pImportedDoc
CopyAndOpenDocument = True
End If
' When finished using the ContextItems, remove them from the collection.
objContextItems.Remove "IManExt.Refresh"
objContextItems.Remove "IManExt.ImportedDocument"
End If
End If
Set objCommand = Nothing
Set objContextItems = Nothing
End Function
===================
The problem with this code is that it will display the New Profile dialog (which we do not want - as we want to copy the document, open it and then make some changes to it (running more code) - it'll then be saved, printed and close automatically as well) [There's another problem with this code in that WorkSite reports a DDE problem and the document couldn't be opened, even though the document is already open]
We know all of the parameters we want to set, like the location to save the file to, document name etc, so having the prompt for the user is unnecessary (most of these things are not the same as the document being copied from) Therefore, we need to know if there is a way to specify all the profile information and save the document.
If it's not possible using the CopyAndOpenCmd, can you suggest another way to acheive the same result (like doing a GetCopy to copy the document locally and then doing an import of it, and then open via Worksite)
I hope all that makes sense - my code probably doesn't though!
Regards
Chris
Sort.png
jny
In your case, the CopyAndOpenCmd would not be useful as it cannot (1) import a copy silently and (2) open an imported document in the same instance of Word probably because the imported document is a zero-byte file as the command does not have a means (contextitem) to upload the physical file to Worksite -- which could be the cause of the DDE problem as it causes a deadlock; the source of the command is probably trying to open a file that does not exist or that it attempts to open the file from the source document file path which is already opened.
What you could do instead is to use the ImportCmd which has contextitems that can be used to duplicate the source document's profile, to upload a copy of the source document file, and to import silently. Then, use the OpenCmd to checkout the imported document to its portable path, so that you may use Documents.Open (WOM) to open the imported document file from its checkout path. Like so:
[VB6]
Dim objCmd As IMANEXTLib.ImportCmd
Dim oContext As IMANEXTLib.ContextItems
Dim srcDoc As IManage.NRTDocument
Dim importedDoc As IManage.NRTDocument
Dim sPath As String
' Assuming srcDoc is the source document to copy.
' Download a copy of the source document locally
sPath = "C:/Temp/" & srcDoc.Number & "_" & srcDoc.Version & "_Copy" & "." & srcDoc.Extension 'Arbitrary example of a filepath
srcDoc.GetCopy sPath, nrNativeFormat
Set oContext = New IMANEXTLib.ContextItems
' The WorkSite object you pass in should be the target server.
oContext.Add "DestinationObject", objDatabase
'To be able to use "IManExt.Import.DuplicateProfileFromDoc, _
IManExt.OpenCmd.NoCmdUI needs to be set to true.
oContext.Add "IManExt.OpenCmd.NoCmdUI", True
'srcDoc points to a valid document object.
oContext.Add "IManExt.Import.DuplicateProfileFromDoc", srcDoc
'filename set here is the file path of the copied doc.
oContext.Add "IManExt.Import.FileName", sPath
' this will override the source doc's description
' oContext.Add "IManExt.Import.DocDescription", "Copy: " & srcDoc.Number & "_" & srcDoc.Version 'the new document Description
Set objCmd = New IMANEXTLib.ImportCmd
objCmd.Initialize oContext
objCmd.Update
' Use bitwise operation to check condition.
If objCmd.Status = (objCmd.Status And nrActiveCommand) Then
objCmd.Execute
' Get the imported document
Set importedDoc = oContext.Item("ImportedDocument")
If Not importedDoc Is Nothing Then
oContext.Remove "ImportedDocument"
oContext.Remove "IManExt.Import.Filename"
oContext.Remove "DestinationObject"
Set oContext = Nothing ' recycle the object variable
'open the copy document
Dim oOpenCmd As IMANEXTLib.OpenCmd
Dim arrDox(0) As IManage.NRTDocument
' Create an instance of the OpenCmd Object
Set oOpenCmd = New IMANEXTLib.OpenCmd
Set oContext = New IMANEXTLib.ContextItems
Set arrDox(0) = importedDoc
oContext.Add "SelectedNRTDocuments", arrDox
' This bypasses the file browse dialog
oContext.Add "IManExt.OpenCmd.NoCmdUI", True
oContext.Add "IManExt.OpenCmd.Integration", True
' This indicates to WorkSite that you are in an instance of Word
oOpenCmd.Initialize oContext
oOpenCmd.Update
' Use bitwise operation: the example from SDK is incorrect.
If oOpenCmd.Status = (oOpenCmd.Status & nrActiveCommand) Then
oOpenCmd.Execute
Dim filLoc As String
filLoc = oContext("IManExt.OpenCmd.ING.FileLocation")
' Get the checkedout path
Documents.Open (filLoc)
' Use WOM to open the WorkSite document from the checkedout path.
'Remove context items
oContext.Remove "SelectedNRTDocuments"
oContext.Remove "iManExt.OpenCmd.Integration"
End If
End If
End If
' If sPath exist, destroy it.
If sPath <> "" Then Kill (sPath)
' Free pointers
End Sub
SirHC
Thanks jny - it works a treat!