Word 2007 : Command Failure on ActiveDocument.Close SaveChanges:=.

A

ABHale

In VBA, I am trying to save a document, then remove a section containing
company specific information, then save the document again in PDF for
distribution.
The code executes ActiveDocument.SaveAs (no problem);
I have tried two variations to preserve the original document:
ActiveDocument.Close SaveChanges:=wdSaveChanges, with the intent of
re-opening the document, results in Command Failure;
If remove the content, save the PDF (ActiveDocument.ExportAsFixedFormat,
ExportFormatPDF:=wdExportFormatPDF) (no problem) then close the document with
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges, results in Command
Failure; I need to preserve the content and inhibit the display of the Save
Changes message to the author.
Thank you in advance for your time and consideration.
 
D

Doug Robbins - Word MVP

Show us all of the code that you are using.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
A

ABHale

Thank you for your help

Private Function funGenerateClient(sOriginatingFilename As String)
Dim loFV As Object
Dim loDocument As Object

On Error GoTo funGenerateClient_Error

Set loFV = New clsUVersionControl

loFV.pVersionPathFilename = sOriginatingFilename ' Parses the filename

funChangeLogEntryAdd loFV.pVersionAvlMinor, txtSummaryText.Text,
lblSummaryText

subRefresh

Dim sFilename As String
Dim sFilenameX As String

DoEvents

ActiveDocument.TrackRevisions = True
ActiveDocument.SaveAs Filename:=moFV.pVersionIncMajorFilename,
FileFormat:=wdFormatDocument

' When I set SaveChanges:=wdSaveChanges
' it goes to funGenerateClient_Error with the Command Failed Error

ActiveDocument.Close

Documents.Open Filename:=moFV.pVersionIncMajorFilename, _
ConfirmConversions:=False,
ReadOnly:=False, AddToRecentFiles:=False, _
PasswordDocument:="",
PasswordTemplate:="", Revert:=False, _
WritePasswordDocument:="",
WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, XMLTransform:=""

' Remove Change Log / Document Infomation

sFilename = Left(moFV.pVersionIncMajorFilename,
(Len(moFV.pVersionIncMajorFilename) - (Len(moFV.pVersionFilenameExt))))

ActiveDocument.TrackRevisions = False

subVerifyDocumentStructure ' This subroutine establishes
' the points where the content
is deleted


If tDS(cCoverPageSectionBreak).bFound And
tDS(cChangeLogSectionBreak).bFound _
Then
Set rRange =
ActiveDocument.Range(Start:=tDS(cCoverPageSectionBreak).lLocation + 1, _

End:=tDS(cChangeLogSectionBreak).lLocation + 1)
rRange.Delete
Else
End If

DoEvents
funFieldsUpdate ' This routine updates the Table of Contents and
' Document Property Fields

lblProcess.Caption = "Generating PDF Version"
DoEvents

ActiveDocument.ExportAsFixedFormat OutputFileName:=sFilename, _
ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, _
OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, _
From:=1, To:=1, _
Item:=wdExportDocumentContent, _
IncludeDocProps:=False, _
KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, _
DocStructureTags:=True, _
BitmapMissingFonts:=True, _
UseISO19005_1:=False

' When I set SaveChanges:=wdDoNotSaveChanges
' it goes to funGenerateClient_Error with the Command Failed Error

ActiveDocument.Close

Set loFV = Nothing
Exit Function

funGenerateClient_Error:
Dim lErr As Long
Dim sErr As String

lErr = Err.Number
sErr = Err.Description
Resume Next
End Function
 
D

Doug Robbins - Word MVP

I haven't worked out what all of your code is doing, but I cannot generate
an error using either

With ActiveDocument
.SaveAs filename:="c:\users\doug\documents\test.docx"
.Close SaveChanges:=wdSaveChanges
End With

With ActiveDocument
.SaveAs filename:="c:\users\doug\documents\test.docx"
.Close SaveChanges:=wdDoNotSaveChanges
End With

which is what you say is causing the error.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
A

ABHale

In both cases I'm executing ActiveDocument.Close as a standalone statement.
It could be that I need to include in a With ActiveDocument. I will try it
and post the result here. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top