首页 文章

从outlook vba访问单词变量或内容控件

提问于
浏览
-2

我正在尝试从Outlook VBA访问,这是我在单词Macro中创建的变量或内容控件ID .

基本上我试图设置一个等于字符串变量的文本字段,并将此变量加载到Outlook中的消息框 .

从outlook,我有创建一个单词对象的代码,并打开活动文档,但我很困惑访问变量 . 我试过让单词VBA中的变量成为一个没有运气的公共变量 .

从outlook访问变量的当前代码:

Set oWordApp = CreateObject("Word.Application") 
Set oWordDoc = oWordApp.Documents.Open("C:\Owner\Desktop\Job.docx") 
oWordApp.Visible = True 
MsgBox(oWordApp.testtest)

2 回答

  • 1

    查看ContentControl帮助文件,您可以使用其Tag属性从内容控件中撤回文本 .

    Sub Test()
    
        Dim oWordApp As Object
        Dim oWordDoc As Object
    
        Dim oContent As Variant
        Dim oCC As Variant
    
        Set oWordApp = CreateObject("Word.Application")
        Set oWordDoc = oWordApp.Documents.Open("S:\DB_Development_DBC\Test\MyNewDoc.docm")
        oWordApp.Visible = True
    
    
        Set oContent = oWordDoc.SelectContentControlsByTag("MyCalendarTag")
        If oContent.Count <> 0 Then
            For Each oCC In oContent
                MsgBox oCC.PlaceholderText & vbCr & oCC.Range.Text
            Next oCC
        End If
    
    End Sub
    

    显示上面的代码点击此处输入日期 . 作为 PlaceHolderText 值和01/01/2007作为 Range.Text 值 . 所以不需要添加单独的功能;只是直接引用内容控件 .

    https://msdn.microsoft.com/en-us/library/office/gg605189(v=office.14).aspx
    https://msdn.microsoft.com/en-us/vba/word-vba/articles/working-with-content-controls

    Edit
    作为在一个函数中从多个控件返回值的示例:

    Public Sub Example()
    
        Dim MySevenTags As Variant
        Dim x As Long
    
        MySevenTags = Array("Tag1", "Tag2", "Tag3", "Tag4", "Tag5", "Tag6", "Tag7")
    
        For x = LBound(MySevenTags) To UBound(MySevenTags)
            MsgBox ReturnFromWordContent(CStr(MySevenTags(x))), vbOKOnly
        Next x
    
    End Sub
    
    Public Function ReturnFromWordContent(TagID As String) As Variant
    
        Dim oWordApp As Object
        Dim oWordDoc As Object
    
        Dim oContent As Variant
        Dim oCC As Variant
    
        Set oWordApp = CreateObject("Word.Application")
        Set oWordDoc = oWordApp.Documents.Open("S:\DB_Development_DBC\Test\MyNewDoc.docm")
        oWordApp.Visible = True
    
        Set oContent = oWordDoc.SelectContentControlsByTag(TagID)
    
        'I've made this next bit up.
        'No idea how to get the type of control, or how to return the values.
        Select Case oContent.Type
            Case "calendar"
                ReturnFromWordContent = oContent.Range.Date
            Case "textbox"
                ReturnFromWordContent = oContent.Range.Text
            Case Else
                'Return some default value such as Null which
                'won't work in this case as it's returning to a messagebox
                'but you get the picture.
        End Select
    
    '    If oContent.Count <> 0 Then
    '        For Each oCC In oContent
    '            MsgBox oCC.PlaceholderText & vbCr & oCC.Range.Text
    '        Next oCC
    '    End If
    
    End Function
    
  • 1

    “我试过让单词VBA中的变量成为一个没有运气的公共变量 . ”

    将宏“testtest”声明为具有变量返回值的函数 .

    Public Function testtest() As String
    
        dim myVariabel as String
    
        myVariable = "test"
    
        ' return value 
        testtest = myVariable
    
    End Function
    

    最好的祝福

相关问题