【问题标题】:is there a workaround for xFiledlg As FileDialog not working?xFiledlg As FileDialog 不工作有解决方法吗?
【发布时间】:2021-04-17 05:14:39
【问题描述】:

我在网上找到了这段代码。假设将 Excel 工作表提取为 PDF 并将其作为附件插入到 Outlook 中,但我在代码的第二行遇到错误。

Dim xFileDlg As FileDialog

这是我找到此信息的链接:https://www.extendoffice.com/documents/excel/4412-excel-save-as-pdf-and-email.html

提前感谢您的帮助。不胜感激。

在这里:

Sub Saveaspdfandsend()
Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range
 
Set xSht = ActiveSheet
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
 
If xFileDlg.Show = True Then
   xFolder = xFileDlg.SelectedItems(1)
Else
   MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
   Exit Sub
End If
xFolder = xFolder + "\" + xSht.Name + ".pdf"
 
'Check if file already exist
If Len(Dir(xFolder)) > 0 Then
    xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
                      vbYesNo + vbQuestion, "File Exists")
    On Error Resume Next
    If xYesorNo = vbYes Then
        Kill xFolder
    Else
        MsgBox "if you don't overwrite the existing PDF, I can't continue." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
        Exit Sub
    End If
    If Err.Number <> 0 Then
        MsgBox "Unable to delete existing file.  Please make sure the file is not open or write protected." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
        Exit Sub
    End If
End If
 
Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
    'Save as PDF file
    xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard
     
    'Create Outlook email
    Set xOutlookObj = CreateObject("Outlook.Application")
    Set xEmailObj = xOutlookObj.CreateItem(0)
    With xEmailObj
        .Display
        .CC = ""
        .Subject = xSht.Name + ".pdf"
        .Attachments.Add xFolder
        If DisplayEmail = False Then
            '.Send
        End If
    End With
Else
  MsgBox "The active worksheet cannot be blank"
  Exit Sub
End If
End Sub

Link of problem

【问题讨论】:

  • 是否有任何关于您的错误的追溯或信息可以分享?
  • @IvanLibedinsky 编译错误,用户未定义类型。 (我试着翻译它,我的 Excel 是法语的)。

标签: excel vba


【解决方案1】:

FileDialog 工作正常,很可能问题出在这里:

If DisplayEmail = False Then
    '.Send
End If

DisplayEmail 既没有声明为变量,也不是 MailItem 对象的方法。

而且我无法理解这个 If 语句的含义 - 因为它可能假设这部分是检查 MailItem 是否显示,如果没有(显示)则发送它,但该项目在 5 行之前显示。

如果您不需要在未经您审核的情况下发送电子邮件 - 请删除这部分代码并重试。

更新

如图所示,很可能没有在 Visual Basic 编辑器 >> 工具 >> 参考 菜单中设置对 Microsoft Office [版本] 对象库的引用。

【讨论】:

  • @Codingnoob 尝试用Dim xFileDlg As Object 替换Dim xFileDlg As FileDialog。如果问题仍然存在 - 在您的主帖中添加错误截图。
  • 它仍然无法正常工作,我在主帖中链接了图像。再次非常感谢。
  • @Codingnoob 用here 的 3 票检查答案
猜你喜欢
  • 2021-04-07
  • 1970-01-01
  • 2016-07-07
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
相关资源
最近更新 更多