1

好的,所以我有这个 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 的帮助并不是很大:/

有什么见解吗?

4

2 回答 2

2
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
于 2013-05-08T16:38:06.917 回答
0

对于您的后续问题:

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

以下代码用于从 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
于 2014-09-24T13:31:18.650 回答