Redoholic
Redoholic

Reputation: 41

Assign a Word variable through Outlook

I have a database management tool that runs on multiple Office apps.

I use Outlook to receive variables from a userform which is then sent to a Word template file, creating a new Word document.

When Outlook calls Word and creates a document, I need to assign a value to a Boolean variable stored in a Word module. Either true or false, depending on the user input.

The Boolean is used to decide which lines of code run on a Word userform the user can later open, but not in the document itself (i.e. inserted into one of the fields in the document).

Upvotes: 0

Views: 64

Answers (1)

FaneDuru
FaneDuru

Reputation: 42236

So, if I correctly understood your question, your Word application has a variable "stored in a Word module". Let us say that this variable will be:

Public boolTest As Boolean

If you will have a Sub in that specific module (in 'Normal.dotm' or in a docm document), let us say:

Sub testBooleanChange(boolT As Boolean)
    boolTest = boolT
    MsgBox boolTest 
End Sub

If Outlook will call the above Sub as:

 objWord.Run "testBooleanChange", True

Then your boolTest variable will take the sent Boolean Value

In fact, a real code dealing with the above suggestion will look like that:

Sub testCallWordProc()
  Dim W As Word.Application
   On Error Resume Next
   Set W = GetObject(, "Word.Application")
   If Err.Number <> 0 Then
        Err.Clear: On Error GoTo 0
        Set W = CreateObject("Word.Application")
   End If
   W.Visible = True
   'If the Sub in discussion exists in a document, un-comment the next line and use your real document full name:
   'W.Documents.Open ("Your doc keeping the sub.docm")
   'If the Sub is inside the 'Normal' you can simply use:
   W.RUN "testBooleanChange", True
End Sub

Upvotes: 1

Related Questions