Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to transfer variables from one Word file to another

Status
Not open for further replies.

lnlcole

Technical User
Oct 29, 2002
2
US
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 <> &quot;&quot; Then
num = missvar.Index
Documents(&quot;EQUIPMENT Variables.doc&quot;).Variables(num).Delete
End If
Next
End If
' Store document variables from current document in EQUIPMENT Variables document

For NewCount = 1 To VarCount
Documents(&quot;EQUIPMENT Variables.doc&quot;).Variables.Add Name:=VarName(NewCount), Value:=VarValue(NewCount)
Next NewCount

Documents(&quot;C:\Equipment Logs\EQUIPMENT Variables.doc&quot;).Save
Documents(&quot;C:\Equipment Logs\EQUIPMENT Variables.doc&quot;).SaveAs FileName:=&quot;C:\Equipment Logs\NextDayVariables.doc&quot;
Documents(&quot;C:\Equipment Logs\NextDayVariables.doc&quot;).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$](&quot;MyString&quot;) 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,

&quot;A neophyte to program coding&quot;
 
I think what you really need is to set up a small Access database. It would require far less work to create queries and reports, as well as allowing you to use forms for data entry. VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
Excel is available much more universally than Access, so if that's your situation, using the Excel approach is perfectly feasible, and not all that difficult. You have to add a reference to Excel VBA to your Word VBA project, and create an instance of an Excel application to manipulate the spreadsheet values. I don't have much experience doing such things, but there's plenty of sample code floating around these forums (search for the string xlApp and you'll find some good examples)
Rob
 
Thank you all for the input and suggestions. I sat down and started reading the manuals again and discovered the easiest way to log the variables and to be able to recall them was to write them to the registry using the SaveSetting and GetSetting commands. This has accomplished my objectives but I thank all of you for your assistance.

Larry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top