vba 在 Word 2010 中的命令按钮 (ActiveX) 中导出为 pdf 的 VB 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16445381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB script to export to pdf in Command Button (ActiveX) in Word 2010
提问by jparnell8839
Ok, so I have this Word 2010 Macro enabled template with my handy dandy forms that people can fill out. I have created a button that says "Convert to PDF" because people dont know how to do it natively. I entered the VB editor of the particular CommandButton that I want to have this functionality. Here's whats in that button:
好的,所以我有这个 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
When I run the code I get..... BAM! Compile error: Expected End Sub
当我运行代码时,我得到..... BAM!编译错误:预期的结束子
If I take out the Sub Convert_PDF() and its pertaining End Sub, suddenly i dont get the sub error messages, but I get another error message:
如果我取出 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.
Replace [file name] with my file's actual name.
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] 替换为我的文件的实际名称。
I'll be completely honest, I'm a complete n00b at VB and Google is turning out to not being very helpful thus far :/
老实说,我是 VB 的一个完整的 n00b,到目前为止,Google 的帮助并不是很大:/
Any insight?
任何见解?
回答by Tim Williams
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
回答by Nick
For your follow up question:
对于您的后续问题:
It depends on how you're picking your date. If you're picking from a "Date Picker Content Control" then you'll need to follow the below code. If you're picking from an Active X "combo box" then you'll need to pull it's value [January]
from the dropdown box. msgbox(DropDown.Value)
will show "January
. You could put it in an if statement if you need to convert the month to a number [if DropDown.Value) = "January" Then...]
.
这取决于你如何选择约会对象。如果您从“日期选择器内容控件”中进行选择,则需要遵循以下代码。如果您从 Active X“组合框”中选择,那么您需要[January]
从下拉框中提取它的值。msgbox(DropDown.Value)
会显示"January
。如果需要将月份转换为数字,可以将其放在 if 语句中[if DropDown.Value) = "January" Then...]
。
The below code is for getting the data from the "Date Picker Content Control" in word
以下代码用于从 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