View Full Version : Solved: how to open document saved as documentname
s?ren
04-10-2006, 01:13 AM
I've had a traumatic experience. My entire macro code was deleted by accident. I almost cried.
I thought I had a backup, but no. That was not so.
And I finally got the code to work.
I am able to redo most of it, but there are parts that I didn't understand - just copied it, and it worked.
So here's what I need help with:
I need to open the document that was saved using this code (I've tried RecentFile but that doesn't always work as I have another program running at the same time.):
DocumentName = "C:\Documents and Settings\Clientfolder\" & MinFolderName.Text & _
"\" & MinSubFolderName.Text & "\" & DocName.Text & LetterName.Text & _
Format(Date, "dd-M-yy") & " " & Format(Time, "hh-mm")
Set doctext = Source.Sections(i).Range
doctext.End = doctext.End - 1
Set target = Documents.Add
target.Range.FormattedText = doctext
target.SaveAs FileName:=DocumentName
target.Close
Søren
Edited 13-Apr-06 by Geekgirlau. Reason: insert vba tags
fumei
04-10-2006, 07:00 AM
I am confused.
1. "I need to open the document that was saved using this code " Was the file actually saved? if so...then why not just open it? I don't understand.
2. Could you explain "Source", as in Source.Sections(i).Range
s?ren
04-10-2006, 12:08 PM
1) the file was saved, and I can open the doc from outside the macro, but I want the macro to open the doc and at the same time close all other docs. And it wont accept documentname as a valid name.
Heres the dims:
Dim Source As Document, oblist As Document, DocName As Range
Dim DocumentName As String
Dim i As Long, doctext As Range, target As Document
Dim MinFolderName As Range
Dim MinSubFolderName As Range
Dim LetterName As Range
Set Source = ActiveDocument
Set oblist = ActiveDocument
søren
Edited 13-Apr-06 by GeekGirlau. Reason: insert vba tags
fumei
04-10-2006, 03:11 PM
I remain confused.
Why are you opening the document you just saved????
Set Source = ActiveDocument
Set oblist = ActiveDocument
This seems scary. Why are you making TWO objects of the same thing?
You need to be much more clear about what and WHEN you are doing things. Posting full code may help. For example I have no idea when you are getting the error - and you are not stating what the error actually is.
target.SaveAs FileName:=DocumentName
target.Close
Are you saying that target is saved, and closed, and then you can not open target again? I also have no idea where and what target is being made as an object.
Really...you have to walk through things step-by-step.
Opening a file, and closing all others, is a fairly simple thing to do, so if something is not working then there must be either a syntax error, or a sequential one.
s?ren
04-13-2006, 04:33 AM
I did as you suggested. Walked through things step by step and and as I tried to explain my problem further, I found out one of the problems with my code. Since I copied some of the code from elsewhere, I didn't know what the different parts of the code was doing. I was happy as long as it worked. Now I've found out that there were certain parts that actually messed things up. So you were right, there was something simple wrong with my code. And I didn't need the two objects either. One is enough.
So thank you for your input. It was most helpfull.
I still don't know how to open the document other that using the recentfile command. So I'm posting the full code here, and hopefully that will clear up things.
Søren
Sub Gem_Legatbrev()
'***************'
'IMPORTANT'
'***************'
'On new docs you need to create a tabel of 1 row with 4 cells at the very top of the doc.
'In this tabel you must place the mergefield you want to use as doc.name.
'(This tabel can be formatted as hidden text with no borders/white borders,
'so it doesn't print out)
'---------------'
' 1) Dimensioning variables
Dim Source As Document, DocName As Range, DocumentName As String
Dim i As Long, doctext As Range, target As Document
Dim MinFolderName As Range
Dim MinSubFolderName As Range
Dim LetterName As Range
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If
' 2) Tell Macro that table is found in active document
Set Source = ActiveDocument
' 3) Find table and cell where DocName, Folder, SubFolderName and LetterName is:
Counter = 1
For i = 1 To Source.Tables(1).Rows.Count
Set DocName = Source.Tables(1).Cell(i, 1).Range
DocName.End = DocName.End - 1
Set MinFolderName = Source.Tables(1).Cell(i, 2).Range
MinFolderName.End = MinFolderName.End - 1
Set MinSubFolderName = Source.Tables(1).Cell(i, 3).Range
MinSubFolderName.End = MinSubFolderName.End - 1
Set LetterName = Source.Tables(1).Cell(i, 4).Range
LetterName.End = LetterName.End - 1
' 4) Set names for MinFolderName and MinSubFolderName to the first 2 and 6 char. in of mergefield data.
MinFolderName = Left(MinFolderName, 2)
MinSubFolderName = Left(MinSubFolderName, 6)
' 5) Check if folder exist. If not, create one
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\Documents and Settings\Clientfolder\" & _
MinFolderName.Text & "\" & MinSubFolderName.Text) Then
' do nothing
Else
' Create new folder
Set objFolder = objFSO.CreateFolder("C:\Documents and Settings\Clientfolder\" & _
MinFolderName.Text & "\" & MinSubFolderName.Text)
End If
' 6) Define where to save doc (path) and add date and time to doc name
DocumentName = "C:\Documents and Settings\Clientfolder\" & MinFolderName.Text & _
"\" & MinSubFolderName.Text & "\" & DocName.Text & LetterName.Text & _
" " & Format(Date, "dd M yy") & " " & Format(Time, "hh mm")
Source.SaveAs Filename:=DocumentName
Next i
' 7) Open the saved document for manual datainput and close all other docs
RecentFiles(1).Open
Dim doc As Document
For Each doc In Application.Documents
If doc.Name <> RecentFiles(1) Then
doc.Close False 'closes without saving
End If
Next doc
' 8) Unprotect document in order to unlink sections 1,3 and 4 and open secion 2 for manual datainput
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
If ActiveDocument.Sections.Count >= 1 Then
With ActiveDocument.Sections(1).Range.Fields
.Unlink
End With
With ActiveDocument.Sections(3).Range.Fields
.Unlink
End With
With ActiveDocument.Sections(4).Range.Fields
.Unlink
End With
If ActiveDocument.Sections.Count >= 2 Then
ActiveDocument.Sections(2).ProtectedForForms = True
End If
End If
ActiveDocument.Protect Password:="", NoReset:=True, Type:= _
wdAllowOnlyFormFields
End Sub
Edited 13-Apr-06 by GeekGirlau. Reason: insert vba tags
fumei
04-13-2006, 02:04 PM
OK, this seems odd to me.
1. you open a document by using Documents.Open Filename:=path and filename
2. you did not answer WHY you are opening a file you just saved!
3.
' 2) Tell Macro that table is found in active document
Set Source = ActiveDocument This does nothing like that at all! You have NO test for a table, so how can it be telling the macro a table is found? This instruction explicitly sets a document object...with nothing at all regarding a table.
4. You want to close all "other" documents, but reopen the one you just saved. There are a couple of ways to do that.
First of...do NOT close the file you actually want open! Pick up its name, and close all other document using the Documents collection.Dim oDoc As Document
For Each oDoc in Documents()
If odoc.Name <> the document you want open Then
oDoc.Close
End If
Other questions. WHY are there other documents open? WHY is it needed to close them?
s?ren
04-14-2006, 05:22 AM
I'm sorry for not explaining properly.
I'll try to explain what happens:
1) I have a program that generates a mergedoc called printout.
2) I need to give this doc a specific name, depending on the values in the first table of document. And I need to place the doc in a specific folder. (Create one if it doesn't exist). Hence the procedure described in ' 2) - '3) -'4) -'5) -'6).
(Maybe I don't need '2, if I understand you correctly) ) I'll try that later -
3) I now need to be able to work with the document. I tried a few different approaches, but nothing worked so I thought I needed to close the printout.doc and open the one I just saved.
I will try to skip part '7) and see what happens.
The other program generates another doc, which I simply dont need open, (I haven't figured out why it does that yet, and then I'm left with two documents open: printout.doc and standard.doc), but all the data I need is in printout.doc. Therefore I need to close all other docs.
I remember seing a way to find out the name of the active document, so I'll try to put that in the code.
Thank you for your quick reply.
I won't be able to test my code for a couple of weeks due to vacation, but as soon as I get back to work I'll try it out. (I've got Word 2002 on my homecomputer and the mailmerge function no longer works after the latest update - (keeps renaming my mergefields) ) So I can not test the code at home.
Søren
fumei
04-15-2006, 03:15 AM
You save the file with SaveAs. OK. It is saved. But you have no CLOSE instruction, so the document is still open.
fumei
04-15-2006, 03:17 AM
Could it be that the document IS still there, but is no longer the ActiveDocument? In which case:For Each doc In Application.Documents
If doc.Name <> DocumentName(1) Then
doc.Close False 'closes without saving
End If
Next doc should close all but that document.
s?ren
04-15-2006, 04:44 AM
Thanks a bunch.
I'll try these things out as soon as possible.
Søren:friends:
fumei
04-15-2006, 09:50 PM
Be sure to post back.
s?ren
04-16-2006, 12:46 PM
Will do
Søren
s?ren
05-07-2006, 12:32 PM
It works!!!
I've tried out this code and it works perfectly.
Renaming my document as content of mergefield and add date and time to this name.
Locking specific sections and enabling one section for form input.
Unlinking doc from source, thus avoiding the mergefields to be updated on the next merge and saving it. (Allthough I have to use another macro for this action. But that's not a problem, as I use assigned macrobuttons anyway)
Here's the working code:
Sub Rename_Document()
'***************'
'IMPORTANT NOTICE'
'***************'
'On new docs you need to create a tabel of 1 row with 4 cells at the very top of the doc.
'In this tabel you must place the mergefield you want to use as doc.name.
'(This tabel can be formatted as hidden text with no borders/white borders,
'so it doesn't print out)
'---------------'
' 1) Dimension variables
Dim Source As Document, DocName As Range, DocumentName As String
Dim i As Long, doctext As Range, target As Document
Dim MinFolderName As Range
Dim MinSubFolderName As Range
Dim LetterName As Range
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If
' 2) Specify where table is located (Active document)
Set Source = ActiveDocument
' 3) Locate table and cell of DocName, Folder, SubFolderName and LetterName:
Counter = 1
For i = 1 To Source.Tables(1).Rows.Count
Set DocName = Source.Tables(1).Cell(i, 1).Range
DocName.End = DocName.End - 1
Set MinFolderName = Source.Tables(1).Cell(i, 2).Range
MinFolderName.End = MinFolderName.End - 1
Set MinSubFolderName = Source.Tables(1).Cell(i, 3).Range
MinSubFolderName.End = MinSubFolderName.End - 1
Set LetterName = Source.Tables(1).Cell(i, 4).Range
LetterName.End = LetterName.End - 1
' 4) set names of MinFolderName and MinSubFolderName to the first 2 and 6 characters of mergefield data
MinFolderName = Left(MinFolderName, 2)
MinSubFolderName = Left(MinSubFolderName, 8)
' 5) Check if folder exists. If not, create one.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\Documents and Settings\clientfolder\" & MinFolderName.Text & "\" & MinSubFolderName.Text) Then
' do nothing
Else
' create new folder
Set objFolder = objFSO.CreateFolder("C:\Documents and Settings\clientfolder\" & MinFolderName.Text & "\" & MinSubFolderName.Text)
End If
' 6) Set document path and add date and time to doc name
DocumentName = "C:\Documents and Settings\clientfolder\" & MinFolderName.Text & "\" & MinSubFolderName.Text & "\" & DocName.Text & LetterName.Text & " " & Format(Date, "dd M yy") & " " & Format(Time, "hh mm")
Source.SaveAs FileName:=DocumentName
Next i
' 7) Close all other documents without saving
Dim doc As Document
For Each doc In Application.Documents
If doc.Name <> ActiveDocument.Name Then
doc.Close False 'closes without saving
End If
Next doc
'8) unprotect document and protect certain sections
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
If ActiveDocument.Sections.Count <= 1 Then
With ActiveDocument.Sections(1).Range.Fields
.Unlink
End With
Else
With ActiveDocument.Sections(1).Range.Fields
.Unlink
End With
With ActiveDocument.Sections(2).Range.Fields
.Unlink
End With
With ActiveDocument.Sections(4).Range.Fields
.Unlink
End With
If ActiveDocument.Sections.Count >= 2 Then _
ActiveDocument.Sections(3).ProtectedForForms = True
End If
End If
ActiveDocument.Protect Password:="", NoReset:=True, Type:= _
wdAllowOnlyFormFields
End Sub
'*****************'
'Save macro
'*****************'
Sub Unlink_and_Save()
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If
With ActiveDocument.Range.Fields
.Unlink
End With
ActiveDocument.Save
ActiveDocument.Close
End Sub
s?ren
05-07-2006, 12:42 PM
So thank you all, for help and inspiration.
Søren
fumei
05-09-2006, 07:48 PM
Glad it is working for you.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.