PDA

View Full Version : Create doc Copy & Return to original without screen changing



JeffT
01-01-2009, 12:01 PM
Hi I'm new to Word VBA and in XL in individual workbooks have used :-

Application.ScreenUpdating = False

to stop the screen from showing the steps as the Macro proceeds.

In Word I have a Macro which creates or opens and index page, depending if it exists initially, then copies the Master doc, opens a new Word doc & pastes the Master in a new document. After a few changes and naming the new doc, it pastes relevant data into the Index & closes the Index. It then blanks the Master & either returns to it or the newly created document.

One of my problems is that each doc opens in a new window, and although I use the Application.ScreenUpdating = False line which stops the document appearing, the screen still changes as documents are named etc.

I've also tried using:-

Application.NewWindow.Visible = False

which works. However it turns all the windows off making the user think he's closed Word, so it's very disconcerting. It may be I have to insert this at the correct time?

I've read various threads here and have noted various comments from fumei which appear to indicate that documents can be opened in a single window. I've tried putting the following code (with changes) in various places which I found in one of fumei's replies but had no luck, & I can't seem to open more than one doc in one window. I'm using XP & Office 2003


Dim wrdDoc As Document
Set wrdDoc = Documents.Open("" & "MyDocument.doc")
End Sub

At the bottom of this post is a part of my Code (I also attach the Master.doc (I think) which has all the code including all the poorly written bits!)

Hopefully somebody can help, I don't fully understand the terminology, I just keep trying different things, learning by doing.

The code works at present but would look better without the flickering screen.

I would also like the pasted document to have the same formatting as the Master. It does on my desktop but not on my laptop. Is there any way to force this? I realise I could format each table and line individually but hope there's an easier way. I have tried various options but have run out of ideas.

Sorry it's so long but fumei says to say exactly what we're trying to do and show some code so hopefully this is enough.

Happy New Year to all

Jeff


Public MasterPath As String
Public TitleName As Range
Public EWN_Number As Range
Public EWN_School As Range
Public NewFileName As String
Option Explicit

Sub Index_Open() 'Called from a button on the Master Document
' This opens the Index file or creates one if one is not present
Dim Index_Name As String
Dim Is_File As String
Application.ScreenUpdating = False
'Application.NewWindow.Visible = False
Call Check_Names
Index_Name = ("01 Index EWN.doc")
Is_File = Dir(MasterPath & "\" & Index_Name)
If Is_File = "" Then
'If EWN_Number > 1 Then Call No_Index 'Just a Msg box
Documents.Add
ActiveDocument.SaveAs (MasterPath & "\" & Index_Name)
Call Create_Index
Else
Documents.Open (MasterPath & "\" & Is_File)
End If
End Sub

Sub Check_Names() 'This gives the names to be used in the remaining Subs
Application.ScreenUpdating = False
'Application.NewWindow.Visible = False
Documents("00 Master EWN.doc").Activate
'This gives the filename path without the file name.
Let MasterPath = ActiveDocument.Path
Set TitleName = ActiveDocument.Tables(3).Rows(1).Cells(1).Range
'This removes the last character (a carriage return type of thing)
TitleName.MoveEnd Unit:=wdCharacter, Count:=-1
Set EWN_School = ActiveDocument.Tables(2).Rows(1).Cells(1).Range
EWN_School.MoveEnd Unit:=wdCharacter, Count:=-1
Set EWN_Number = ActiveDocument.Tables(2).Rows(1).Cells(2).Range
EWN_Number.MoveEnd Unit:=wdCharacter, Count:=-1
End Sub

Sub Create_Index() 'This Creates an index file if one doesn't already exist
Dim Title As String
Dim FirstPara As Range
Application.ScreenUpdating = False
'Application.NewWindow.Visible = False
Title = EWN_School & " Early warning Notice Index" & vbCr & vbCr
Documents("01 Index EWN.doc").Activate
Selection.TypeText Title
Set FirstPara = ActiveDocument.Paragraphs(1).Range
With FirstPara.Font
.Bold = True
.Underline = wdUnderlineSingle
.Name = "Verdona"
.Size = 16
End With
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=1, _
NumColumns:=5, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitContent
With Selection.Tables(1).Rows(1)
.Range.Font.Bold = True
.Range.Font.Name = "Verdona"
.Range.Font.Size = 12
.Cells(1).Range.Text = "No."
.Cells(2).Range.Text = "EWN Date"
.Cells(3).Range.Text = "Title"
.Cells(4).Range.Text = "Reply Date"
.Cells(5).Range.Text = "Other"
End With
ActiveDocument.Save
End Sub

Sub Save_As() 'Called after the Index_Open sub
'This saves the New EWN with the reverse date Filename using the Title
'from the form as part of the filename
Dim ReverseDate As String
Dim FileExists As String
Dim i As Integer
Application.ScreenUpdating = False
'Application.NewWindow.Visible = False
Documents("00 Master EWN.doc").Activate
i = 0
ReverseDate = Format(Date, "yymmdd")
Set TitleName = ActiveDocument.Tables(3).Rows(1).Cells(1).Range
'This removes the last character (a carriage return or similar)
TitleName.MoveEnd Unit:=wdCharacter, Count:=-1
NewFileName = ReverseDate & " " & TitleName & " " & "EWN " & EWN_Number & ".doc"
ActiveDocument.Content.Copy
'This checks if the New file name already exists and if so adds .1 or .2 etc to the name
FileExists = Dir(MasterPath & "\" & NewFileName)
Do While Len(FileExists) > 0
i = i + 1
NewFileName = ReverseDate & " " & TitleName & " " & "EWN " & EWN_Number & "." & i & ".doc"
FileExists = Dir(MasterPath & "\" & NewFileName)
Loop
Documents.Add.SaveAs (MasterPath & "\" & NewFileName)
ActiveDocument.Content.Paste
ActiveDocument.Tables(3).Delete
ActiveDocument.Shapes("Control 2").Delete
ActiveDocument.Save
End Sub

lucas
01-01-2009, 12:50 PM
In Word I have a Macro which creates or opens and index page, depending if it exists initially, then copies the Master doc, opens a new Word doc & pastes the Master in a new document. After a few changes and naming the new doc, it pastes relevant data into the Index & closes the Index. It then blanks the Master & either returns to it or the newly created document.

Hi Jeff, can I suggest an alternative? Why not just have a template that is formatted the way you want it. When you open the template a new document is created and a userform opens......you input your data and hit enter.....save the file and close it.

Next time you just do the exact same steps......would you be open to that approach or are you locked in to what you are doing.

This sounds like a project that templates were designed for and would make this much easier. Let me know and I will post a simple example.

JeffT
01-01-2009, 01:35 PM
Hi Lucas

Unfortunately the original document has been made by someone else. The Doc I'm using is just my own to test with which is similar. There are lots of copies of the original on different drives on the server at work. I might be able to use a template but from memory they have to be loaded in a particular place to load as a new doc, otherwise you just end up editing the template?

As I say, though I use word nearly every day I haven't done any VBA programming with it & have never even created a template before:dunno . XL is my first love, but we have a problem at work and I said I'd give it a go.

Regards

Jeff

lucas
01-01-2009, 01:46 PM
You really should look into this. It will make you famous at work.

You never should edit the template file after you get it set up.

attached is a small example. You can open the .dot file itself to edit it by opening a blank word file and then use file-open to open the .dot file.....

Anything created using the template(.dot) is a new file.....a clone of the template file. The original is never changed unless you specifically open it to edit it.

Formatting can be anything you want....edit the template.

Go to tools-options and select the view tab....put a check next to bookmarks so you can see them(they will not show on print or printpreview). Then double click the file I have attached.

Fill it out and save it. It will not affect the original. Do this as many times as you want.....that's what the template is for.

If you can't use it...no big deal but you need to see how this can be done.

I had to zip it because the board won't allow uploading .dot files

JeffT
01-01-2009, 02:32 PM
That seems great Lucas.

I'd always thought that templates had to go into the templates folder under the Microsoft folder somewhere. (I could find it if I had to.) Seems I was wrong.

The only thing I need to happen is for the number in one of the cells of the table in the Template to be updated each time it's used. In this case it'd have to be in the template. I'll see if that works.

What about the fact that I open another file, the Index, which is updated with data from the copied document, before closing. This will still open and shut causing the screen to flicker. Is there any other way of doing that.

Thanks

Jeff

lucas
01-01-2009, 02:41 PM
Is the index cumlative? In other words do you add to it each time below the data from the last run?

As for the increment. Try the attached. It creates a text file in your C:\ directory and each time you create a new file it puts the next number in the bookmark.


extract it and double click on it.....close the file and don't save it..the first one should be 001. Then double click on the dot file again and the next doc should be 002

You can delete the text file to start over.

lucas
01-01-2009, 03:43 PM
Jeff, unzip the two templates in this zip to the same directory and run the runme.dot

After you fill in the form and hit the button you will have two word documents open...the one you just created and when the form closes another new document is created from the 01 Index EWN.dot

both files are left open.

JeffT
01-01-2009, 03:48 PM
Yes

The index is cumulative. In my current code the number of the form increments by 1 at the end each time the code is run. It uses

EWN_Number = EWN_Number + 1

where EWN_Number is the value in a cell on the Master doc.

The first EWN_Number (before adding 1) is used in the Index.

The code adds another row of cells at the bottom of a table in the Index, then puts the EWN_Number, current date & title name from the Master document in the index before closing it.

I have a loop in the code that checks if the new filename (which uses the EWN_Number) for the new document was going to repeat in the save folder. If it was going to repeat it adds a further increment .1, .2 etc to the name to ensure it has a different name.

I'll have to study the IncrementW file in detail, however I don't know the file paths where the individual files will be stored so will have to edit this section to read the current location as I have in my code. Also I'd rather not have a seperate file for the number, but presumably it could be stored in the document itself as in mine. Also I want the possibility of deleting the document without incrementing the number. This may be possible I'll have a go.

I still don't know how to open the Index add the new row and close it without having the screen updating. Is this possible?

lucas
01-01-2009, 03:54 PM
I still don't know how to open the Index add the new row and close it without having the screen updating. Is this possible?

I would need both files to look at it. I can't duplicate what you have without seeing it.

JeffT
01-01-2009, 04:07 PM
Lucas

The macro creates the Index file in the same folder as the master document the first time it's run. If it is deleted renamed or moved etc it'll create a new one again with a msgbox warning. I think I forgot to renumber the Master doc back to 1 so the warning will activate the first time the command button on hte master document is pressed, (unless the cell next to AnySchool is renumbered back to 1 again)

J

lucas
01-01-2009, 04:17 PM
Jeff, I get an error in the sub checknames() at this line:
Documents("00 Master EWN.doc").Activate

when I comment that I get and error.....bad file name.

maybe you could put together two files that just show the problem. I'm sure we can fix it if we can pinpoint it.

lucas
01-01-2009, 04:18 PM
The logo and other things in that frame are really jittery when I load the document too. What is it trying to do on document open?

JeffT
01-01-2009, 04:32 PM
Strange that as that is the filename of the document with the command button on so it should be there. It works on my computers.

That line shouldn't be needed as the document should always be the active document as you've had to click the button on the form. Unless it's been renamed, but then if it's commented out it should work as the active doc is still the doc with the button on.

It won't work if the document isn't the active document so perhaps you've opened it then have another document as the active doc but try to run the code from VBA. However I've just tried that and it still works for me.

I'll have a think about this tommorow. Sorry but I have to go to bed now, just too tired after last night or more to the point this morning.

Thanks For your time Lucas, much appreciated and I've learn't about Templates, another string to my Bow.

Regards

Jeff

JeffT
01-01-2009, 04:40 PM
Lucas

Just a quick reply. When it opens it just opens. There is no code "OnOpen" or anything. Mine opens normally with the logo taking say 0.5 secs to appear. This is a fairly slow laptop as well. once you click the button thats when the jittery stuff starts as the new windows open for the different docs but I've set screen updating to False so you just see a blank screen. The msgBox at the end on yes reshows the Master do with anything put in the 4 big dialog boxes remeved and the number incremented by 1. on No just shows the new document with the title & button removed, on cancel both Master and new doc are left open. But I don't know if you got that far.

J

JeffT
01-02-2009, 02:29 AM
Hi

Just tried it on my Win 2000 work laptop with Office 2003 and it still works, with only the application screens opening (without the documents showing) & closing as the code proceeds.

I was running from a memory stick as well, and although slower everything functioned as expected. (Not sure how quick it'll be over the network however, that is normally very slow at the best of times from remote sites!).

lucas
01-02-2009, 11:05 AM
Hi Jeff, I haven't abandoned you I just don't know of a solution to your probem, especially since I can't duplicate it to work on it.

I think though that if you are opening a closed document and adding new tables and data that there will be some of what you are experiencing.......

JeffT
01-02-2009, 11:17 AM
Lucas

No problem, I'm still working on a solution myself. The biggest problem is knowing if it's possible. If it is'nt then fine, I'll go with what I have. Thanks for sparing the time.

Regards

Jeff