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
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