Vba 用于在Word 2010中的命令按钮(ActiveX)中导出为pdf的VB脚本

Vba 用于在Word 2010中的命令按钮(ActiveX)中导出为pdf的VB脚本,vba,ms-word,word-2010,export-to-pdf,commandbutton,Vba,Ms Word,Word 2010,Export To Pdf,Commandbutton,好的,我有一个启用Word 2010宏的模板和我方便的表格,人们可以填写。我创建了一个按钮,上面写着“转换为PDF”,因为人们不知道如何在本地进行转换。我输入了特定命令按钮的VB编辑器,我希望具有此功能。下面是按钮上的内容: Private Sub CommandButton1_Click() Sub Convert_PDF() Dim desktoploc As String Dim filename As String Dim mypath As String deskto

好的,我有一个启用Word 2010宏的模板和我方便的表格,人们可以填写。我创建了一个按钮,上面写着“转换为PDF”,因为人们不知道如何在本地进行转换。我输入了特定命令按钮的VB编辑器,我希望具有此功能。下面是按钮上的内容:

Private Sub CommandButton1_Click()
Sub Convert_PDF()

 Dim desktoploc As String
 Dim filename As String
 Dim mypath As String

    desktoploc = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    filename = ThisDocument.Name
    mypath = desktoploc & "\" & filename

    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        mypath, _
        ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub
End Sub
当我运行代码时,我得到。。。。。砰!编译错误:应为结束子对象

如果我取出Sub Convert_PDF()及其相关的End Sub,突然我没有收到Sub错误消息,但收到另一条错误消息:

无法打开文件[文件名],因为内容有问题。详细信息:文件已损坏,无法打开。
用我的文件的实际名称替换[文件名]

老实说,我在VB是一个完全的n00b,谷歌到目前为止并没有什么帮助:/


有什么见解吗?

关于您的后续问题:

Private Sub CommandButton1_Click()
    Convert_PDF
End Sub


Sub Convert_PDF()

 Dim desktoploc As String
 Dim filename As String
 Dim mypath As String

    desktoploc = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    filename = ThisDocument.Name
    mypath = desktoploc & "\" & filename & ".pdf"

    ActiveDocument.ExportAsFixedFormat OutputFileName:= _
        mypath, _
        ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
        wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
        Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
        CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
        BitmapMissingFonts:=True, UseISO19005_1:=False
End Sub
这取决于你如何选择约会对象。如果您是从“日期选择器内容控件”中选择,则需要遵循以下代码。如果要从活动的X“组合框”中进行拾取,则需要从下拉框中提取其值
[一月]
msgbox(DropDown.Value)
将显示
“一月”
。如果需要将月份转换为数字,可以将其放入if语句中
[if DropDown.Value)=“一月”然后…]

下面的代码用于从word中的“日期选择器内容控件”获取数据


我想你需要创建一个PDF对象。几年前我尝试这样做时,他们没有任何免费的库来帮助我。作为一种解决方法(直到我看到我能找到什么)您可以打印到pdf打印机并让宏调用它。@Jeff-Excel 2010内置另存为pdf:不需要额外的库…您有两个嵌套的sub-这不是有效的VBA。我将尝试这个方法,并让您知道它工作得很好。是否有方法附加从下拉表单字段中选择的a month?例如,我有一个下拉列表,您可以在其中设置月份…在VBA中,我为用户创建了一个字符串,即:user=VBA.Environ(“用户名”)。我已将mypath字符串更改为:mypath=desktoploc&“\Metrics\”&filename&“&date&”“&user现在我只需要了解如何将下拉表单中的信息输入到日期字符串中:/Sorry-Word表单我不熟悉。
'put this at the top of the code, outside any functions/subs
Dim DateGlobal As Date

'This sub will run whenever you exit any ContentControl function
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
If IsDate(ActiveDocument.SelectContentControlsByTitle("Date").Item(1).Range.Text) = True Then
    DateGlobal = ActiveDocument.SelectContentControlsByTitle("Date").Item(1).Range.Text
End If

'Now use DateGlobal wherever you need it; it will be in date format.
msgbox(DateGlobal)                  'Shows date as default date format
msgbox(myDateFormat(DateGlobal)     'Shows date as your custom date format (below)
End Sub

'************************************************************************************
'                       Custom DATE format (instead of computer default)
'                       Found elsewhere on this site, I like my format yyyy/mm/dd
'************************************************************************************

Function myDateFormat(myDate)
    d = WhatEver(Day(myDate))
    M = WhatEver(Month(myDate))
    y = Year(myDate)
    myDateFormat = y & "/" & M & "/" & d
End Function

Function WhatEver(num)
    If (Len(num) = 1) Then
        WhatEver = "0" & num
    Else
        WhatEver = num
    End If
End Function