Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel xFiledlg作为FileDialog不工作是否有解决方法?_Excel_Vba - Fatal编程技术网

Excel xFiledlg作为FileDialog不工作是否有解决方法?

Excel xFiledlg作为FileDialog不工作是否有解决方法?,excel,vba,Excel,Vba,我在网上找到了这个代码。假设将excel工作表提取为PDF格式,并将其作为附件插入Outlook,但代码的第二行出现错误 Dim xFileDlg As FileDialog 以下是我找到此信息的链接: 提前感谢您的帮助。非常感谢 这是: Sub Saveaspdfandsend() Dim xSht As Worksheet Dim xFileDlg As FileDialog Dim xFolder As String Dim xYesorNo As Integer Dim xOutloo

我在网上找到了这个代码。假设将excel工作表提取为PDF格式,并将其作为附件插入Outlook,但代码的第二行出现错误

Dim xFileDlg As FileDialog
以下是我找到此信息的链接:

提前感谢您的帮助。非常感谢

这是:

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
子存储aspdfandsend()
Dim xSht As工作表
Dim xFileDlg As FileDialog
将xFolder设置为字符串
Dim xYesorNo作为整数
Dim xOutlookObj作为对象
Dim xEmailObj作为对象
暗XUSERNG As范围
设置xSht=ActiveSheet
设置xFileDlg=Application.FileDialog(msoFileDialogFolderPicker)
如果xFileDlg.Show=True,则
xFolder=xFileDlg。选择编辑项(1)
其他的
MsgBox“您必须指定一个文件夹以将PDF保存到其中。”&vbCrLf&vbCrLf&“按“确定”退出此宏。”,vbCritical,“必须指定目标文件夹”
出口接头
如果结束
xFolder=xFolder+“\”+xSht.Name+“.pdf”
'检查文件是否已存在
如果Len(Dir(xFolder))>0,则
xYesorNo=MsgBox(xFolder&“已存在”。&vbCrLf&vbCrLf&“是否要覆盖它?”_
vbYesNo+vbQuestion,“文件存在”)
出错时继续下一步
如果xYesorNo=vbYes,则
杀死xFolder
其他的
MsgBox“如果不覆盖现有PDF,我将无法继续。”_
&vbCrLf&vbCrLf&“按“确定”退出此宏。”,vbCritical,“退出宏”
出口接头
如果结束
如果错误号为0,则
MsgBox“无法删除现有文件。请确保该文件未打开或未受写保护。”_
&vbCrLf&vbCrLf&“按“确定”退出此宏。”,vbCritical,“无法删除文件”
出口接头
如果结束
如果结束
设置xUsedRng=xSht.UsedRange
如果Application.WorksheetFunction.CountA(xUsedRng.Cells)为0,则
'另存为PDF文件
xSht.ExportAsFixedFormat类型:=xlTypePDF,文件名:=xFolder,质量:=xlQualityStandard
'创建Outlook电子邮件
设置xOutlookObj=CreateObject(“Outlook.Application”)
设置xEmailObj=xOutlookObj.CreateItem(0)
使用xEmailObj
.展示
.CC=“”
.Subject=xSht.Name+“.pdf”
.Attachments.Add xFolder
如果DisplayEmail=False,则
’发送
如果结束
以
其他的
MsgBox“活动工作表不能为空”
出口接头
如果结束
端接头

文件对话框工作正常,最可能的问题是:

If DisplayEmail = False Then
    '.Send
End If
DisplayEmail既不是声明为变量,也不是MailItem对象的方法

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

如果您不需要在未经审阅的情况下发送电子邮件,请删除这部分代码,然后重试

更新


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

是否有任何关于您的错误的回溯或信息,您也可以共享?@IvanLibedinsky编译错误,类型未由用户定义。(我试着翻译它,我的Excel是法语的)。@Codingnoob试着用
Dim xFileDlg As FileDialog
替换
Dim xFileDlg As Object
。如果问题仍然存在-添加一个错误的屏幕截图到你的主帖子。它仍然不起作用,我链接了主帖子中的图像。再次非常感谢。@Codingnoob以来自