View Single Post
 
Old 02-14-2022, 03:51 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,166
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This version doesn't error if the variable already exists and if the variable doesn't exist it creates it.
Code:
Private Sub VariableTransmission()
  Dim doc1 As Document, doc2 As Document, aVar As Word.Variable
  
  Set doc1 = ActiveDocument
  Set doc2 = Documents.Add
  
  doc1.Variables("First").Value = "Hi mum"    'resets value or creates if it doesn't already exist
  doc1.Variables("Second") = "Hello world"
  doc1.Variables("Second") = "Hello world"
  
  For Each aVar In doc1.Variables
    doc2.Variables(aVar.Name) = aVar.Value
  Next aVar

  Debug.Print doc2.Variables("Second").Value
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote