在附加到电子邮件之前,如何对命令按钮进行编程以更改文档格式?

时间:2016-03-28 23:22:34

标签: vba email format word-vba commandbutton

情况:我正在尝试创建一个表单,在单击“提交”按钮时自动将其自身附加到电子邮件,但单击“提交”按钮后,将从文档中删除宏。 背景:我已经能够创建一个代码,允许我将文档附加到电子邮件中;但是,当文档附加到电子邮件时,它仍然包含宏。 这是我到目前为止的代码:

Public newfilename As String
Private Declare Function GetTempPath Lib "kernel32" _
  Alias "GetTempPathA" _
  (ByVal nBufferLength As Long, _
  ByVal lpBuffer As String) As Long
Private Declare Function GetTempFileName Lib "kernel32" _
  Alias "GetTempFileNameA" _
  (ByVal lpszPath As String, _
  ByVal lpPrefixString As String, _
  ByVal wUnique As Long, _
  ByVal lpTempFileName As String) As Long

Public Function Get_Temp_File_Name( _
  Optional sPrefix As String = "VBA", _
  Optional sExtensao As String = "") As String
  Dim sTmpPath As String * 512
  Dim sTmpName As String * 576
  Dim nRet As Long
  Dim F As String
  nRet = GetTempPath(512, sTmpPath)
  If (nRet > 0 And nRet < 512) Then
    nRet = GetTempFileName(sTmpPath, sPrefix, 0, sTmpName)
    If nRet <> 0 Then F = Left$(sTmpName, InStr(sTmpName, vbNullChar) - 1)
    If sExtensao > "" Then
      Kill F
      If Right(F, 4) = ".tmp" Then F = Left(F, Len(F) - 4)
      F = F & sExtensao
    End If
    Get_Temp_File_Name = F
  End If
End Function

Public Function Get_File_Name( _
  Optional sPrefix As String = "VBA", _
  Optional sFilename As String = "") As String
  Dim sTmpPath As String * 512
  Dim sTmpName As String * 576
  Dim nRet As Long
  Dim F As String
  nRet = GetTempPath(512, sTmpPath)
  a = Len(nRet)
  F = Left$(sTmpPath, InStr(sTmpPath, vbNullChar) - 1)
  Get_File_Name = F + sFilename
  Debug.Print F
End Function

Private Sub CommandButton1_Click()
    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    ActiveDocument.SaveAs2 (newfilename)
    On Error Resume Next
    With OutMail
        .to = "me@me.com"
        .CC = ""
        .BCC = ""
        .Subject = "Communication with Government Regulatory Agency Report"
        .Body = "Attached is the Communication with Government Regulatory Agency report for the following location:"
        .Attachments.Add ActiveDocument.FullName
        'You can add other files also like this
        '.Attachments.Add ("C:\test.txt")
        .Display   'or use .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

Private Sub Document_Open()
newfilename = Get_File_Name("", "Communication with Government Regulatory Agency Report")
ActiveDocument.SaveAs2 (newfilename)
End Sub

Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
    If KeyCode = 13 Then
        KeyCode = 0
        TextBox1.Text = TextBox1.Text & vbCrLf & "• "
    End If
End Sub

Private Sub TextBox2_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
    If KeyCode = 13 Then
        KeyCode = 0
        TextBox1.Text = TextBox1.Text & vbCrLf & "• "
    End If
End Sub

我尝试编写代码,使命令按钮在单击时不可见,并在命令按钮不可见时删除宏,并且我尝试编码以在保存到电子邮件之前更改文档的格式,但两种解决方案都不起作用。我被卡住了,只要文档在附加到电子邮件时有活动宏,我就无法发布表单。非常感谢任何帮助!

0 个答案:

没有答案