PDA

View Full Version : Variable document name from template



garethtaylor
12-18-2009, 04:39 AM
Hi all,
I have this code I recorded from a macro and copied into the command button.
It opens a letter template (C1) it then goes back to the loader document with the command button, copies something then goes back to the letter template (C1) then pastes this information it does this 3 times.
The problem is that the documents are templates therefore once the document is open it is no longer the same name it will be document 1, document 2, etc depending how many other documents the user has open therefore the code stops as the file name it is looking for does not exists

What I was wanting help with is what code and where should I put it for the below code?
It needs to find the correct name for the loader document, the one with the command button so it can go back to it once it has opened the letter template.
Also
Need to find the name of the letter template (c1) so it can find and go back to paste the information onto the document.
Here is the code I have, for ease I have highlighted the names of the documents in bold
For reference the document with eh code is called “letterloader” & the document it opens is called “C1”

ChangeFileOpenDirectory "O:\In development - not to be used yet\Master Documents\Master Letter Templates\"
Documents.Open FileName:="C1.dot", ConfirmConversions:=False, ReadOnly:= _
False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _
"", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
Format:=wdOpenFormatAuto
Windows("letter loader").Activate
ActiveDocument.Shapes("Text Box 19").Select
Selection.WholeStory
Selection.Copy
Windows("C1").Activate
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Paste
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Windows("letter loader").Activate
Selection.ShapeRange.Select
ActiveDocument.Shapes("Text Box 2").Select
Selection.WholeStory
Selection.Copy
Windows("C1").Activate
Selection.MoveDown Unit:=wdLine, Count:=53
Selection.MoveUp Unit:=wdLine, Count:=14
Selection.MoveDown Unit:=wdLine, Count:=1
Selection.Paste
ActiveWindow.ActivePane.VerticalPercentScrolled = 21
Windows("letter loader").Activate
Selection.ShapeRange.Select
ActiveDocument.Shapes("Text Box 9").Select
Selection.WholeStory
Selection.Copy
Windows("C1").Activate
Selection.Paste

sorry forgot to say this MS Word 2000

lucas
12-18-2009, 09:56 AM
Might I suggest an alternative to opening multiple templates?

What if all the text blocks were in one template and when you run it you are given the option of which blocks to keep and the rest are deleted?

Click here (http://www.vbaexpress.com/forum/showthread.php?t=27098) to see if this might be something you can work with.

The example file is in post #2 at the link.

Inserting Autotext might be another viable alternative.

As I said, I'm not sure that these ideas are workable under your circumstances but if your not locked into the method you are using you might consider them as good alternatives.

Tinbendr
12-18-2009, 11:06 AM
Set the open documents to an object.


Set DestDoc = ActiveDocument

'This adds a document based on the template C1.dot.
'This way, you don't risk messing the template up.
Set SrcDoc = Documents.Add(Template:= FullPath$ & "C1.dot")
With DestDoc
.Range.InsertAfter SrcDoc.Range.Shapes("Text Box 19")
'REST OF CODE HERE
End With


This way, you can refer to the respective documents no matter how many are open at the time. (As long and you start with the source as the activedocument)

Also, try to convert youre code over using Range instead of Selection. It'll be much more effecient.

fumei
12-21-2009, 01:53 PM
Agree with Tinbendr, use Document objects, rather than Activate by name.

Why are you opening the template itself?
Documents.Open FileName:="C1.dot"
This is not normal behaviour.

You probably could do your actions within headers without ever using View.