vba word wdDialogFileSaveAs不';救不了

vba word wdDialogFileSaveAs不';救不了,vba,pdf,ms-word,Vba,Pdf,Ms Word,我有一些word vba代码来简化我的工作流程。其中的一部分是将模板文件安全地保存到子文件夹结构深处的特定文件位置 Function saveFile(fileType As String) Dim strFileName As String Dim StrPath As String Dim instance As WdSaveFormat Dim format As String 'provide a file type format = f

我有一些word vba代码来简化我的工作流程。其中的一部分是将模板文件安全地保存到子文件夹结构深处的特定文件位置

Function saveFile(fileType As String)
    Dim strFileName As String
    Dim StrPath As String
    Dim instance As WdSaveFormat
    Dim format As String

    'provide a file type
    format = fileType

    'provide default filename
    docname = ActiveDocument.Tables(1).cell(2, 1).Range.Text
    saveName = Left(docname, Len(docname) - 2)

    'provide a path
    projectnumber = ActiveDocument.Tables(1).cell(11, 3).Range.Text
    projectnum = Left(projectnumber, Len(projectnumber) - 2)
    projecttop = Left(projectnumber, Len(projectnumber) - 4)
    pathFull = "H:\Projecten\P0" & projecttop & "00 - P0" & projecttop & "99\P0" & projectnum & "\2 - Bedrijfsbureau\2.Berekeningen\2.2 Berekeningen voorlopig\"

    'provide a revision
    If ActiveDocument.Tables(2).cell(2, 2).Range.Text = Chr(13) & Chr(7) Then
        MsgBox ("Er is niets ingevult?")
    ElseIf ActiveDocument.Tables(2).cell(3, 2).Range.Text = Chr(13) & Chr(7) Then
        revLet = ""
    ElseIf ActiveDocument.Tables(2).cell(4, 2).Range.Text = Chr(13) & Chr(7) Then
        revLet = "_A"
    ElseIf ActiveDocument.Tables(2).cell(5, 2).Range.Text = Chr(13) & Chr(7) Then
        revLet = "_B"
    ElseIf ActiveDocument.Tables(2).cell(6, 2).Range.Text = Chr(13) & Chr(7) Then
        revLet = "_C"
    Else
        revLet = "_D"
    End If

    'concat path
    StrPath = pathFull & saveName & revLet

    With Dialogs(wdDialogFileSaveAs)
        .Name = StrPath
        .format = format
        If .Display <> 0 Then
            strFileName = .Name
        Else
            strFileName = "User Cancelled"
        End If
    End With
End Function

and calling the funcion

    Private Sub CommandButton19_Click()
    'wdFormatXMLDocument => docx wdFormatDocument => doc wdFormatPDF => pdf
    Call saveFile(wdFormatXMLDocumentMacroEnabled)
End Sub

Private Sub CommandButton20_Click()
    'wdFormatXMLDocument => docx wdFormatDocument => doc wdFormatPDF => pdf
    Call saveFile(wdFormatPDF)
End Sub
函数saveFile(文件类型为字符串)
将strFileName设置为字符串
将StrPath设置为字符串
作为WdSaveFormat的Dim实例
Dim格式为字符串
'提供文件类型
格式=文件类型
'提供默认文件名
docname=ActiveDocument.Tables(1).单元格(2,1).Range.Text
saveName=Left(docname,Len(docname)-2)
“提供一条路径
projectnumber=ActiveDocument.Tables(1).cell(11,3).Range.Text
projectnum=左侧(projectnumber,Len(projectnumber)-2)
projecttop=左侧(projectnumber,Len(projectnumber)-4)
pathFull=“H:\Projecten\P0”&projecttop&“00-P0”&projecttop&“99\P0”&projectnum&“\2-Bedrijfsbureau\2.Berekeningen\2.2 Berekeningen voorlopig\”
"修改"
如果ActiveDocument.Tables(2).cell(2,2).Range.Text=Chr(13)和Chr(7),那么
MsgBox(“niets是ingevult吗?”)
ElseIf ActiveDocument.Tables(2).cell(3,2).Range.Text=Chr(13)和Chr(7)Then
revLet=“”
ElseIf ActiveDocument.Tables(2).cell(4,2).Range.Text=Chr(13)和Chr(7)Then
revLet=“\u A”
ElseIf ActiveDocument.Tables(2).cell(5,2).Range.Text=Chr(13)和Chr(7)Then
revLet=“_B”
ElseIf ActiveDocument.Tables(2).cell(6,2).Range.Text=Chr(13)和Chr(7)Then
revLet=“\u C”
其他的
revLet=“\u D”
如果结束
“康卡特路
StrPath=pathFull&saveName&revLet
带对话框(wdDialogFileSaveAs)
.Name=StrPath
.format=格式
如果是,则显示0
strFileName=.Name
其他的
strFileName=“用户已取消”
如果结束
以
端函数
调用函数
专用子命令按钮19_单击()
'wdFormatXMLDocument=>docx wdFormatDocument=>doc wdFormatPDF=>pdf
调用保存文件(wdFormatXMLDocumentMacroEnabled)
端接头
私有子命令按钮20_单击()
'wdFormatXMLDocument=>docx wdFormatDocument=>doc wdFormatPDF=>pdf
调用保存文件(wdFormatPDF)
端接头

pdf格式的文件按预期保存,但word格式的文件不。。。我尝试了一些不同的选项并手动使用“另存为”提示出错。。。一些兼容的东西。

我真的很惊讶保存为PDF的效果。。。您可以使用.Display向用户显示对话框。Display不会执行用户选择的对话框操作:如果他单击Save,则不会执行该命令。“显示”仅显示对话框,用户操作将更改对话框状态。状态可以在事后读取,您自己的代码需要保存


如果您想让对话框执行用户选择的操作,则需要使用.Show方法:If.Show Then…

Cindy,谢谢您的工作!这是一个有点奇怪的结构,但我已经做到了。是的,单词dialogs背后的功能可以追溯到20年前的WordBasic时代。所以他们绝对是怪兽!