0

I have the following VBA code for an E-Mail:

Sub Global_Email_Message()
Content = "<style> p {background-color: #d9d9d9} </style><p> This message should be global so I can use it in every sub for an E-Mail </p>"
        If ExitAll = False Then
            Set OApp = CreateObject("Outlook.Application")
            Set OMail = OApp.CreateItem(0)
            With OMail
            .display
            End With
            signature = OMail.HTMLBody
            With OMail
            .To = "test@test.de"
            .Subject = "test"
            .HTMLBody = Content
            End With
            Set OMail = Nothing
            Set OApp = Nothing
        Else
        End If
End Sub

This code works perfectly.

Now, I want to achieve that the message and the style in the variable "Content" is global so I can use it in different E-Mail-Subs. How can I globalize the "Content" variable for different E-Mail-Subs?

Community
  • 1
  • 1
Michi
  • 4,663
  • 6
  • 33
  • 83

1 Answers1

0

You need to declare the content variable with public outside the function:

Public content as String

Sub Global_Email_Message()
...
user4074875
  • 146
  • 2
  • 13
  • I put: Public content As String content = "

    This message should be global so I can use it in every sub for an E-Mail

    " and deleted the content variable from the Sub Global_Email_Message() but now it does not show any content in the E-Mail.
    – Michi May 30 '17 at 14:21
  • You still need to initialise the variable once in a sub/function before it becomes publically available. So I usually have an init_vars sub at the start of my code which I call at the entry point to the application. Only constants can be declared statically. And a string cannot be set as a constant. https://stackoverflow.com/questions/5897832/is-it-possible-to-declare-a-public-variable-in-vba-and-assign-a-default-value – user4074875 May 30 '17 at 14:40