View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.word.vba.beginners
Mike NG Mike NG is offline
external usenet poster
 
Posts: 87
Default passing variables from excel to word

On Sat, 19 Jul 2003 at 20:22:32, Doug Robbins - Word MVP (Doug Robbins -
Word MVP ) wrote:
In the Word VBA code, you can use oDoc.Variables('Product") or
oDoc.Variables('Product").Value (though the value is not really necessary
because it is the default property of a Variable. Having does maybe make
the code a bit easier to understand )

The variables do not have to appear anywhere in the document if you don't
want them to, so you can leave the value in them. If you do want to display
the value of the variable in the document, you do so by use of a {
DOCVARIABLE "Product" } field.

I seem to be having some sort of synchronisation problem. This is my
excel code

Sub OpenWord()

Dim oDoc As Word.Document
Dim oWord As Word.Application


'See if word's already running
On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If Err Then
'No, so start a new word session
Set oWord = New Word.Application
Err.Clear
End If

oWord.Visible = True
oWord.Activate
Set oDoc = oWord.Documents.Open(ThisWorkbook.Path & "\Envelope.doc")
oDoc.Variables("Product") = "HELLO WORLD"
Set oWord = Nothing
Set oDoc = Nothing

End Sub



This is a simplified version of my word document open event

Private Sub Document_Open()

MsgBox "" & ThisDocument.Variables("Product") & "<<"

End Sub


However, when word opens, it tells me the object has been deleted
--
Mike