【问题标题】:Word VBA - adding a page in a password-protected document with a buttonWord VBA - 使用按钮在受密码保护的文档中添加页面
【发布时间】:2016-11-23 22:57:30
【问题描述】:

我为一家能源公司编辑和创建 Word 文档表单。现场人员将在现场的 Windows 平板电脑上打开这些表格。我们使用 Microsoft Word 2010。我已使用开发人员选项卡添加复选框和日期选择器以及其他功能,以使在平板电脑上填写这些表格更快更容易 - 但我们有一些需要需要编码。

-表单受密码保护,只有某些字段可供最终用户编辑。

-我的老板希望用户可以选择通过单击按钮将附加页面添加到文档中,最好是在表单本身上,但仍然让表单受到密码保护。他希望这个附加页面用于添加他们正在执行的工作的照片并检查质量。他希望他们能够根据需要添加尽可能多的照片页面(我认为我们可以使用按钮“调用”特定模板,将按钮添加到页面底部,当他们填写页面时,他们可以单击模板上的按钮,然后重复该过程。

-我已经为这个附加页面创建了一个模板页面,该页面也是受密码保护的,并且有两个来自开发人员选项卡的“插入图片”字段,以及每个下方的 1x1 表格,用于描述图片。

我真的很想学习 VBA,而不是仅仅复制和粘贴别人的代码。我想了解它为什么起作用并根据需要在此基础上进行构建。

我用 html、css 和 javascript(基础)编写代码。

【问题讨论】:

  • 您遇到的问题有点过于宽泛,无法回答。从您的功能的小部分开始,并使用谷歌查找起点。

标签: vba ms-word


【解决方案1】:

经过多天的诅咒和拖网论坛,我想出了这个解决方案,并决定把它留在这里,以防其他人对他们自己的项目感兴趣:

Private Sub PhotoPageTrigger_Click()

' PhotoPage 宏

ActiveDocument.Unprotect Password:="password" 'unprotect the document with the password

Selection.GoTo What:=wdGoToBookmark, Name:="\EndofDoc" 'move cursor to the end of the last page

Selection.TypeParagraph 'insert new paragraph to make a new page

Selection.Font.Size = 12 'make the new page have a text size of 12

ChangeFileOpenDirectory _
    "I:\FileLocation\" 'Go to this file location

Documents.Open FileName:= _
    "I:\FileLocation\Form Picture Template.docm" _
    , ConfirmConversions:=False, ReadOnly:=False, AddToRecentFiles:=False, _
    PasswordDocument:="", PasswordTemplate:="", Revert:=False, _
    WritePasswordDocument:="", WritePasswordTemplate:="", Format:= _
    wdOpenFormatAuto, XMLTransform:="" 'Open this template document and read with these settings

Selection.GoTo What:=wdGoToBookmark, Name:="\StartofDoc" 'move cursor to the beginning of the document

Selection.WholeStory 'select all

Selection.Copy 'copy all

Selection.GoTo What:=wdGoToBookmark, Name:="\EndofDoc" 'move cursor to home place at the end of the document

ActiveDocument.Close 'close the open template doc

Selection.PasteAndFormat (wdUseDestinationStylesRecovery) 'paste contents of clipboard to now-active form doc at the cursor location

Dim oData   As New DataObject 'object to use the clipboard

oData.SetText Text:=Empty 'Clear the clipboard
oData.PutInClipboard

ActiveDocument.Protect Password:="password", NoReset:=False, Type:= _
    wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=True 'reprotect document with password and these settings

结束子

很高兴我一个人完成了这一切! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    相关资源
    最近更新 更多