Home
TeamSite
Checkin a New Version of an Active Word Document
System
Hi
I'm a bit stuck in creating a new version of the document via VBA. Basically I'm trying to create a new version of a document opened from FileSite into Word and is the current active document.
I did consider using the checkin method, but from what I can see, this would mean that I would have to save and close the document down first before checking out then back in again. The drawback is that it means I would have to save changes to the current version first, which kind of negates creating a new version in the first place.
I've tried a couple of the suggestions in the forum, but I get an abnormal program termination if I use the NewVersionCmd example, or a warning that only one document can be imported when using the ImportNewVersionCmd example (despite the fact that I'm only referencing one document).
Here is an example using the NewVersionCmd method (please assume all variables have been declared)
ActiveDocument.SaveAs "c:\documents and settings\" & vUserName & "\my worksite\" & Format(Now, "ddmmyyyy-hhmmss") & ".doc"
ActiveDocument.Saved = True
vTmpDocName = ActiveDocument.FullName
ActiveDocument.Close
Dim WSCmd As IMANEXTLib.NewVersionCmd
Dim iContextItems As IMANEXTLib.ContextItems
Set objDocument = iManDatabase.GetDocument(vDocNum, vDocVersion)
If objDocument.CheckedOut = False Then
objDocument.CheckOut "c:\documents and settings\" & vUserName & "\my worksite\temp.doc", nrReplaceExistingFile, Now, ""
objDocument.UpdateProfile vRet
End If
Set iContextItems = New IMANEXTLib.ContextItems
iContextItems.Add "IManExt.NewVersion.Document", objDocument
iContextItems.Add "SelectedNRTSessions", iSessions(1)
iContextItems.Add "IManExt.Checkin.FileName", vTmpDocName
iContextItems.Add "iManExt.NewVersionCmd.NoCmdUI", True
Set WSCmd = New IMANEXTLib.NewVersionCmd
WSCmd.Initialize iContextItems
WSCmd.Update
WSCmd.Execute
Dim objNewDocument As iManage.NRTDocument
Set objNewDocument = iContextItems.Item("iManExt.NewVersionCmd.Document")
objNewDocument.UnlockDocument
objNewDocument.UpdateProfile vRet
objNewDocument.Refresh
Dim objNewPath As String
objNewPath = iContextItems.Item("iManExt.NewVer.FileLocation")
Set iContextItems = Nothing
If objDocument.CheckedOut = True Then
If objDocument.IsOperationAllowed(nrUnlockDocumentOp) = True Then
objDocument.UnlockDocument
Else
objDocument.CheckIn "c:\documents and settings\" & vUserName & "\my worksite\temp.doc", nrReplaceOriginal, nrDontKeepCheckedOut, vRet
End If
objDocument.UpdateProfile vRet
End If
The abnormal termination error is occurring at the WSCmd.Execute line. A little bit more info - this is WorkSite 8.2.
Any assistance would be greatly appreciated.
Many thanks
Jamie
Find more posts tagged with
Comments
danbrown
Yeah, I'm a bit stuck with this as well. All I want to do is simulate someone doing File, Save As, New Version, Ok, Save with maybe a couple of fields fiddled with, all without any UI. It should be so easy...
Any help much appreciated.
jny
Try this:
[VBA]
Option Explicit
Public Sub SilentSaveAsNewVer()
Dim oExtensibility As iManO2K.iManageExtensibility
' An object used for extending the functionality of iManage Office 2000
' integration.
' Get the iManage O2K extensibility object.
Set oExtensibility = _
Application.COMAddIns("iManO2K.AddinForWord2000").Object
Dim oDMS As NRTDMS
Dim oSess As NRTSession
Dim oSrcDoc As NRTDocument
Dim oNewVer As NRTDocument
Dim oCmd As NewVersionCmd
Dim oContext As ContextItems
Dim arrSessions(1 To 1) As NRTSession
Dim sSrcPath, sNewVerPath As String
Dim bRefresh, iErrs
' Check the current connection mode
If oExtensibility.CurrentMode = nrConnectedMode Then
' Get the ContextItems collection
Set oContext = oExtensibility.Context
If Not oContext Is Nothing Then
On Error GoTo GetContextItemErr
Set oSrcDoc = oExtensibility.GetDocumentFromPath(ActiveDocument.FullName)
If oSrcDoc Is Nothing Then
' Not an iManage doc
GoTo Cleanup
End If
' Get the session object
Set oSess = oSrcDoc.Database.Session
If oSrcDoc.CheckedOut = False Then
' Existing document must be checked out already."
GoTo Cleanup
End If
Set arrSessions(1) = oSess
' Put the documnet in a Safe Array of type NRTDocument
Set oContext = New ContextItems
' Required values
oContext.Add "IManExt.NewVersion.Document", oSrcDoc
' The document object you pass in should be _
of type NRTDocument
oContext.Add "SelectedNRTSessions", arrSessions
' The safe array should contain only one NRTSession object.
'Optional
oContext.Add "IManExt.NewVersionCmd.NoCmdUI", True
' Skips the NewVersion Profile dialog
' Create an instance of the command object
Set oCmd = New NewVersionCmd
' Initialize command
oCmd.Initialize oContext
oCmd.Update
If oCmd.Status = (oCmd.Status And nrActiveCommand) Then
oCmd.Execute
On Error Resume Next
Set oNewVer = oContext.Item("IManExt.NewVersionCmd.Document")
' New version document object
sNewVerPath = oContext.Item("IManExt.NewVer.FileLocation")
' Get new version's checked out path
If oNewVer Is Nothing Then
MsgBox "Error occured during command execute...new version is null."
Exit Sub
End If
'Remove context items
oContext.Remove "IManExt.NewVersionCmd.NoCmdUI"
End If
End If
' Save the current doc as new version in Word; otherwise, it will be a zero-byte file.
' You have only established a document profile for the new version up until now.
ActiveDocument.SaveAs sNewVerPath
' You may release original document before exiting routine, _
' as it's still checkedout.
If True = oSrcDoc.CheckedOut Then
' Hold checkout path
sSrcPath = oSrcDoc.CheckoutPath
If oSrcDoc.Comment <> "" Then
' Build mhc file (if comments exist)
Dim mhc As String
mhc = Replace(sSrcPath, ".DOC", ".MHC")
End If
If oSrcDoc.IsOperationAllowed(nrCheckInDocumentOp) = True Then
' checkin source doc object
oSrcDoc.CheckIn sSrcPath, nrReplaceOriginal, nrDontKeepCheckedOut, iErrs
' Build prf file
Dim prf As String
prf = Replace(sSrcPath, ".DOC", ".PRF")
' Destroy stale paths
Kill sSrcPath
Kill prf
If mhc <> "" Then Kill mhc
End If
End If
Else
' Notify: not in connected mode.
MsgBox "You're not connected to WorkSite."
End If
GoTo Cleanup
GetContextItemErr:
MsgBox Err.Description
Err.Clear
Cleanup:
' Free pointers before exiting
Set oExtensibility = Nothing
Set oDMS = Nothing
Set oSess = Nothing
Set oContext = Nothing
Set oContext = Nothing
Set oCmd = Nothing
Set oNewVer = Nothing
Set oSrcDoc = Nothing
End Sub
Private Sub ReOpenDoc(ByVal objNewVer As IManage.NRTDocument)
Dim oCmd As OpenCmd
Dim oContext As ContextItems
Dim oDox(0) As NRTDocument
Set oDox(0) = objNewVer
Set oCmd = New OpenCmd
Set oContext = New ContextItems
' Required
oContext.Add "SelectedNRTDocuments", oDox
' Optional
oContext.Add "IManExt.OpenCmd.NoCmdUI", True
' This will skip the dialog
oContext.Add "IManExt.OpenCmd.Integration", True
' This indicates you're in an instance of Office Integrated app
oCmd.Initialize oContext
oCmd.Update
If oCmd.Status = (oCmd.Status & nrActiveCommand) Then
oCmd.Execute
' Get the file location
Dim filLoc As String
filLoc = oContext("IManExt.OpenCmd.ING.FileLocation")
Documents.Open (filLoc)
'Remove context items
oContext.Remove "IManExt.OpenCmd.NoCmdUI"
oContext.Remove "IManExt.OpenCmd.Integration"
End If
End Sub
danbrown
Awesome, thanks very much! There needs to be a WorkSite Cookbook or something. Maybe when I've spent far too much time doing this I'll write it and make my millions.
Migrateduser
Thanks also for the code example. Works a charm and has cured this particular development headache. Absolutely superb!!!
danbrown
Ok, this code works great, apart from when it leaves an entry in the 'Checked-out Documents' folder. I can open a Word document from FileSite, run the macro which saves it as a new version, close it, and the file will appear unlocked as it should in the view under the workspace, but it stays in 'Checked-out Documents'.
Any ideas much appreciated as always...
jny
It looks like FileSite is still hanging onto the cache copy of the document. If you do a refresh (by selecting the popup menu off of the "_Checked-out Document" node) it should go away; this is when FileSite gets the latest copy from the server.
danbrown
No luck I'm afraid! It shows up as being the version before the new one saved and shown in the workspace too.
williamp
The source document continues to be checked out.
jny
Which version of WorkSite is this? Does the same behavior occur when you do a save as new verison through UI?
jny
I just tested that same code snippet I provided and it worked correctly; no source document remained checked out.
I tested in WorkSite 8.2 SP1 Patch 4, which is the latest release.
There was a defect reported on a problem with the previous version isn't checked in after saving it as a new version. This was reported sometime prior to patch 6.
williamp
Worksite 8.0 SP1 - It does not happen when i use the UI.
jny
Worksite 8.0 SP1 - It does not happen when i use the UI.
I ran the same VBA code against 8.0 SP1. I tested by opening a WorkSite doc from within FileSite as well as Word, and the code save the opened doc as a new version without leaving the previous version checked out.
Show me the exact set of steps you run against the VBA code that can recreate the problem happening on your end.