【问题标题】:VB script to export to pdf in Command Button (ActiveX) in Word 2010在 Word 2010 中的命令按钮 (ActiveX) 中导出为 pdf 的 VB 脚本
【发布时间】:2013-05-08 16:14:02
【问题描述】:

好的,所以我有了这个 Word 2010 启用宏的模板和我方便的花花公子表格,人们可以填写这些表格。我创建了一个按钮,上面写着“转换为 PDF”,因为人们不知道如何在本地进行。我进入了我想要具有此功能的特定 CommandButton 的 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

当我运行代码时,我得到...... BAM!编译错误:预期结束子

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

The file [file name] cannot be opened beacause there are problems with the contents. Details: The file is corrupt and cannot be opened. 将 [file name] 替换为我的文件的实际名称。

老实说,我在 VB 是个十足的 n00b,而到目前为止,Google 的帮助并不大:/

有什么见解吗?

【问题讨论】:

  • 我认为您需要创建一个 PDF 对象。当我几年前尝试做类似的事情时,他们没有任何免费的库来帮助解决这个问题。作为一种解决方法(直到我能看到我能找到什么),您可以打印到 pdf 打印机并让您的宏调用它。
  • @Jeff - Excel 2010 具有内置的另存为 PDF:无需其他库...
  • 你有两个嵌套的 subs - 这不是有效的 VBA。

标签: vba ms-word word-2010 export-to-pdf commandbutton


【解决方案1】:
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

【讨论】:

  • Tim 会试试这个,然后告诉你
  • 工作得很好。有什么方法可以附加从下拉表单字段中选择的月份?例如,我有一个下拉菜单,您可以在其中设置月份...在按钮的 VBA 中,我为用户创建了一个字符串:user = VBA.Environ("USERNAME") 我已经更改了 mypath string to: mypath = desktoploc & "\Metrics\" & filename & " - " & date & " - " & user 现在我只需要找出如何将下拉表单中的信息输入到日期字符串中:/
  • 抱歉 - 我不熟悉单词形式。
【解决方案2】:

对于您的后续问题:

这取决于您如何选择约会对象。如果您从“日期选择器内容控件”中进行选择,则需要遵循以下代码。如果您从 Active X “组合框”中进行选择,则需要从下拉框中提取它的值 [January]msgbox(DropDown.Value) 将显示 "January。如果需要将月份转换为数字[if DropDown.Value) = "January" Then...],可以将其放在 if 语句中。

以下代码用于从 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

【讨论】:

    猜你喜欢
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多