PDA

View Full Version : closing down the open document..



RPL
06-24-2009, 12:44 AM
I cannot get the open file to close, and it needs a second pair of eyes...


Dim fs As Object, fol As Object, f As Object
Set fs = CreateObject("Scripting.FileSystemObject")
pth = ThisDocument.Path
tmpname = ThisDocument.Name
myname = InputBox("Enter template doc name", , "TestTemplate.doc") 'ThisDocument.Name
shortname = Left(myname, Len(myname) - 4)
Set fol = fs.getfolder(pth)
If fs.fileexists(fol & "\" & myname) = False Then
'
MsgBox ("No such file!")
Exit Sub
End If
'
Documents.Open (fol & "\" & myname)
'
For Each f In fol.Files
If f.Type = "Microsoft Word Document" And Left(f.Name, 1) <> "~" And f.Name <> tmpname _
And f.Name <> myname And Left(f.Name, Len(shortname)) <> shortname Then
'
Documents.Open (fol & "\" & f.Name)
Documents(myname).Activate
Selection.HomeKey Unit:=wdStory
Selection.TypeParagraph
Selection.PasteAndFormat (wdPasteDefault)
ActiveDocument.SaveAs fol & "\" & shortname & "-" & f.Name
Documents.Open (fol & "\" & myname)
Documents(shortname & "-" & f.Name).Close
Documents(f.Name).Close savechanges:=False
'
End If
'
Next
Documents(myname).Close
'
End Sub

Ty in advance..
Richard

joms
06-24-2009, 07:15 AM
try this:


Documents(f.Name).Close savechanges:=wdDoNotSaveChanges

RPL
06-24-2009, 07:36 AM
Sad to say mate, she no work :))

the template saves to a new name,
the template re-opens to loop,
but the open file stays open,,,
hmmm, .....
R

joms
06-24-2009, 08:39 AM
hi just curious why after you save you need to open..

Documents.Open (fol & "\" & myname) ' why need to open?
Documents(shortname & "-" & f.Name).Close ' closes what?
Documents(f.Name).Close savechanges:=False ' and this one also close what file

try not to loop first open one file and try to close it.. to isolate issues then if everything is working do the loop... :o)
'

fumei
06-24-2009, 11:33 AM
Much better to use Document objects. It makes it far easier to explicitly perform actions on specific documents.

I am a little confused, as it seems you are working with THREE documents, as I will show in a slight amendment to your code.Sub Yadda()
Dim ThisDoc As Document
Dim ThatDoc As Document
Dim ThatOtherDoc As Document

Dim fs As Object, fol As Object, f As Object
Set fs = CreateObject("Scripting.FileSystemObject")

' this sets a document object for the CURRENT doc
Set ThisDoc = ActiveDocument

' notice you can use the document object here
' rather than ThisDocument
pth = ThisDoc.Path
tmpname = ThisDoc.Name

myname = InputBox("Enter template doc name", _
, "TestTemplate.doc") 'ThisDocument.Name
shortname = Left(myname, Len(myname) - 4)
Set fol = fs.getfolder(pth)
If fs.fileexists(fol & "\" & myname) = False Then
'
MsgBox ("No such file!")
Exit Sub
End If
' this is different from CURRENT doc
Set ThatDoc = Documents.Open(fol & "\" & myname)
'
For Each f In fol.Files
If f.Type = "Microsoft Word Document" And _
Left(f.Name, 1) <> "~" And _
f.Name <> tmpname And _
f.Name <> myname And _
Left(f.Name, Len(shortname)) <> shortname Then
' this is diffferent AGAIN
Set ThatOtherDoc = Documents.Open(fol & "\" & f.Name)
However, by declaring, and setting, document objects, you can action them without doing any Activate. Remember:
' this is different from CURRENT doc
Set ThatDoc = Documents.Open(fol & "\" & myname)
This means the document object ThatDoc is the myname one. You can of course name the document objects themselves whatever you want. The point is that by having named objects you can action them by name. So instead of Activating, then using:
ActiveDocument.SaveAs
[\vba]you would - if I understand what you are doing -[vba]ThatDoc.SaveAs Filename:= fol & "\" & _
shortname & "-" & f.Name
I am concerned about your use of the word "template" though...

RPL
06-24-2009, 06:17 PM
Thanks for the reply, Joms

hi just curious why after you save you need to open..
I'm using the active directory so others can place their 'template' documents in one of their folders, and run the code.
Hmm, so it may not be a code issue, but a path issue, as they have the issue on their system...aha..hmm..


Thanks for the reply, fumei.
Always happy to take another approach! :)

Yes, I am working with 3 docs; the original source doc f.Name, the 'template' doc MyName; and the saved output doc shortname-f.Name. The 'template' is a normal word doc.

The code creates a versioned copy of the original files in the active folder after running a series of macros.
hth with explaining the process.

regards,
Richard