I have a series of macros that take user inputs (both integer and strings) which are used to log and document changes to the status of a equipment plant. These logs are generated in Word and a new set of logs is opened for every day. At midnight, I have a series of macros which:
1) produce a summary of the equipment status;
2) Prints the final status to the log sheet;
3) Saves the Word document;
4) Closes the Word Document;
5) Opens a blank Word document and formats the header & footer with the appropriate date and misc. data in preparation for generating the next days logs.
My problem is when the Word document for one day closes and a blank document is opened I lose some of the public variables that I have written to a "Variables.doc". I am using the following code in my AutoClose to redim and store the variables from the open document.
'*****************************************************
Option Base 1
Sub Main()
Dim VarName()
Dim VarValue()
Dim NameOfFile
Dim CurrentCount
Dim NewCount
Dim VarCount
Dim ActDocName
ActDocName = ActiveDocument.Name
' Determine and store in arrays document variables in current document
VarCount = Documents(ActDocName).Variables.Count
ReDim VarName(VarCount)
ReDim VarValue(VarCount)
For CurrentCount = 1 To VarCount
tempname = ActiveDocument.Variables(CurrentCount).Name
tempval = ActiveDocument.Variables(CurrentCount).Value
VarName(CurrentCount) = tempname
VarValue(CurrentCount) = tempval
Next CurrentCount
' See if EQUIPMENT Variables document exists
NameOfFile = "C:\Equipment Logs\EQUIPMENT Variables.doc"
With Application.FileSearch
.LookIn = "C:\Equipment Logs\"
.FileName = "EQUIPMENT Variables.doc"
If .Execute = 1 Then
Documents.Open FileName:=NameOfFile
Else
Set newdoc = Documents.Add
With newdoc
.SaveAs FileName:=NameOfFile
End With
End If
End With
'On Error Resume Next
' Delete all current document variables in Variables document
If Documents("EQUIPMENT Variables.doc"
.Variables.Count > 0 Then
countto = Documents("EQUIPMENT Variables.doc"
.Variables.Count
counter = 0
For Each missvar In Documents("EQUIPMENT Variables.doc"
.Variables
counter = counter + 1
If missvar.Name <> "" Then
num = missvar.Index
Documents("EQUIPMENT Variables.doc"
.Variables(num).Delete
End If
Next
End If
' Store document variables from current document in EQUIPMENT Variables document
For NewCount = 1 To VarCount
Documents("EQUIPMENT Variables.doc"
.Variables.Add Name:=VarName(NewCount), Value:=VarValue(NewCount)
Next NewCount
Documents("C:\Equipment Logs\EQUIPMENT Variables.doc"
.Save
Documents("C:\Equipment Logs\EQUIPMENT Variables.doc"
.SaveAs FileName:="C:\Equipment Logs\NextDayVariables.doc"
Documents("C:\Equipment Logs\NextDayVariables.doc"
.Close savechanges:=wdSaveChanges
End Sub
'*****************************************************
When I open the next days logs using the Equipment Template, I open the NextDayVariables.doc and rename it as EQUIPMENT Variables.doc.
I am trying to use MyString$ = WordBasic.[GetDocumentVar$]("MyString"
to get the variables from the Equipment Variables.doc but this does not seem to work.
Can anyone tell me a better way of keeping these variables to be used in the next days logs?
Would writing the variables to a Excel spreadsheet with named cells be a better way of doing this and if so, do you have any recommendations on how I would code this.
Thank you for your assistance and comments,
"A neophyte to program coding"
1) produce a summary of the equipment status;
2) Prints the final status to the log sheet;
3) Saves the Word document;
4) Closes the Word Document;
5) Opens a blank Word document and formats the header & footer with the appropriate date and misc. data in preparation for generating the next days logs.
My problem is when the Word document for one day closes and a blank document is opened I lose some of the public variables that I have written to a "Variables.doc". I am using the following code in my AutoClose to redim and store the variables from the open document.
'*****************************************************
Option Base 1
Sub Main()
Dim VarName()
Dim VarValue()
Dim NameOfFile
Dim CurrentCount
Dim NewCount
Dim VarCount
Dim ActDocName
ActDocName = ActiveDocument.Name
' Determine and store in arrays document variables in current document
VarCount = Documents(ActDocName).Variables.Count
ReDim VarName(VarCount)
ReDim VarValue(VarCount)
For CurrentCount = 1 To VarCount
tempname = ActiveDocument.Variables(CurrentCount).Name
tempval = ActiveDocument.Variables(CurrentCount).Value
VarName(CurrentCount) = tempname
VarValue(CurrentCount) = tempval
Next CurrentCount
' See if EQUIPMENT Variables document exists
NameOfFile = "C:\Equipment Logs\EQUIPMENT Variables.doc"
With Application.FileSearch
.LookIn = "C:\Equipment Logs\"
.FileName = "EQUIPMENT Variables.doc"
If .Execute = 1 Then
Documents.Open FileName:=NameOfFile
Else
Set newdoc = Documents.Add
With newdoc
.SaveAs FileName:=NameOfFile
End With
End If
End With
'On Error Resume Next
' Delete all current document variables in Variables document
If Documents("EQUIPMENT Variables.doc"
countto = Documents("EQUIPMENT Variables.doc"
counter = 0
For Each missvar In Documents("EQUIPMENT Variables.doc"
counter = counter + 1
If missvar.Name <> "" Then
num = missvar.Index
Documents("EQUIPMENT Variables.doc"
End If
Next
End If
' Store document variables from current document in EQUIPMENT Variables document
For NewCount = 1 To VarCount
Documents("EQUIPMENT Variables.doc"
Next NewCount
Documents("C:\Equipment Logs\EQUIPMENT Variables.doc"
Documents("C:\Equipment Logs\EQUIPMENT Variables.doc"
Documents("C:\Equipment Logs\NextDayVariables.doc"
End Sub
'*****************************************************
When I open the next days logs using the Equipment Template, I open the NextDayVariables.doc and rename it as EQUIPMENT Variables.doc.
I am trying to use MyString$ = WordBasic.[GetDocumentVar$]("MyString"
Can anyone tell me a better way of keeping these variables to be used in the next days logs?
Would writing the variables to a Excel spreadsheet with named cells be a better way of doing this and if so, do you have any recommendations on how I would code this.
Thank you for your assistance and comments,
"A neophyte to program coding"