【问题标题】:Is there a way to change a Word document's filename and email subject using combobox data (VBA)?有没有办法使用组合框数据 (VBA) 更改 Word 文档的文件名和电子邮件主题?
【发布时间】:2020-01-16 00:02:54
【问题描述】:

我正在尝试设置一个表单,以便当从 ComboBox 中选择一个主题(在我的示例中是动物)时,它会更改文件名和电子邮件的主题行。目前,它只是在您单击提交按钮时发送一封电子邮件,但我需要根据选择的主题来区分文件。我已经尝试寻找答案,但到目前为止我还没有遇到任何相关的问题。

ComboBox 中有四个条目。老虎、猴子、大象、长颈鹿。

ComboBox 名为“Animals”,其标签为“ComboBox1”

很遗憾,无论出于何种原因,我都无法上传图片,但如果有帮助的话,它是一个“组合框内容控件”。抱歉,我对这些东西的了解有限,主要是反复试验让我走到这一步并借用其他代码。

任何建议都会有所帮助。

Private Sub Submit_Click()
Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objInspector As Object
Dim objDoc As Word.Document
Dim objRange As Range
Dim sDocname As String




ActiveDocument.Save
sDocname = ActiveDocument.FullName

If Len(ActiveDocument.Path) = 0 Then
    MsgBox "Document is not saved!"
    GoTo lbl_Exit
End If
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err <> 0 Then
    MsgBox "Outlook is not running."

    GoTo lbl_Exit
End If
On Error GoTo 0

Set objOutlookMsg = objOutlook.createitem(0)
With objOutlookMsg
    .To = "email@emailaddress.com"
    .Cc = ""
    .Subject = "Favourite Animal is "
    .attachments.Add sDocname
    Set objInspector = .GetInspector
    Set objDoc = objInspector.WordEditor
    Set objRange = objDoc.Range(0, 0)
    .Display
    objRange.Text = "My favourite animal is the "


    .Send
End With
lbl_Exit:
Set objDoc = Nothing
Set objRange = Nothing
Set objOutlookMsg = Nothing
Set objInspector = Nothing
Set objOutlook = Nothing
Exit Sub
End Sub

【问题讨论】:

  • @CindyMeister 谢谢,辛迪。不幸的是,我无法添加屏幕截图,但我已经输入了您要求的信息。如果您还有什么需要澄清的,请询问。
  • 感谢您添加这是一个内容控件这一事实。问题的原始措辞使我们认为可能意味着 VBA 用户窗体。当您尝试上传图片时发生了什么?对于新人,只允许链接,但其他具有更高站点声誉的人会为您编辑它...
  • 对不起,@CindyMeister,我一定错过了这条评论。不幸的是,由于这是一台工作电脑,很多网站都被锁定了,所以我无法通过这个网站上传图片。我很幸运能在这里(现在!)。

标签: vba combobox ms-word filenames


【解决方案1】:

这样的事情呢?

Private Sub Submit_Click()
Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim objInspector As Object
Dim objDoc As Word.Document
Dim objRange As Range
Dim sDocname As String

'new declarations.
Dim cmb As ContentControl
Dim sSelText As String

'get a reference of the combobox.
    Set cmb = ThisDocument.SelectContentControlsByTag("Combobox1")(1)
'get the selected item in a variable.
    sSelText = cmb.Range.Text
    Set cmb = Nothing
'enforce making a selection.
    If sSelText = "DEFAULT_TXT" Then 'write here the default text of your combobox.
        MsgBox "Please select subject from the dropdown menu.", vbCritical, "No selection!"
    Else
        ActiveDocument.Save
        sDocname = ActiveDocument.FullName
        If Len(ActiveDocument.Path) = 0 Then
            MsgBox "Document is not saved!", vbCritical, "Error!"
            GoTo lbl_Exit
        End If
        On Error Resume Next
        Set objOutlook = GetObject(, "Outlook.Application")
        If Err <> 0 Then
            MsgBox "Outlook is not running."
            GoTo lbl_Exit
        End If
        On Error GoTo 0
        Set objOutlookMsg = objOutlook.createitem(0)
        With objOutlookMsg 'use the selected item as you wish.
            .To = "email@emailaddress.com"
            .Cc = ""
            .Subject = "Favourite Animal is " & sSelText
            .attachments.Add sDocname & "_" & sSelText
            Set objInspector = .GetInspector
            Set objDoc = objInspector.WordEditor
            Set objRange = objDoc.Range(0, 0)
            .Display
            objRange.Text = "My favourite animal is the " & sSelText
            .Send
        End With
    End If
lbl_Exit:
Set objDoc = Nothing
Set objRange = Nothing
Set objOutlookMsg = Nothing
Set objInspector = Nothing
Set objOutlook = Nothing
End Sub

【讨论】:

  • 嘿@Dimitris,感谢您的回复。我已将其插入,但我收到一条错误消息,提示“找不到此文件。请验证路径和文件名是否正确”。当我删除&amp; "_" &amp; sSelText 时它工作正常,但显然这会阻止电子邮件主题发生变化。
  • 嗨@特里斯坦。请告诉我哪一行会引发错误?
  • 对不起,@Dimitris,是这个:.attachments.Add sDocname &amp; "_" &amp; sSelText
  • 嗯,应该扩展。这是我的错。首先,您必须创建此文件,然后附加它。有两种选择。重命名当前文档并将其附加,或“另存为”此文档以保持当前文档不变。
  • 哦,好的。我想我做错了什么!另存为功能可以正常工作。我会对此有所了解。
猜你喜欢
  • 1970-01-01
  • 2018-04-16
  • 1970-01-01
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多