【问题标题】:VBA coding in Word 2013Word 2013 中的 VBA 编码
【发布时间】:2017-10-11 15:48:48
【问题描述】:

我在 Word 2013 中遇到了有关 VBA 的问题。我的编码经验有限,宏对我来说是一个新领域。

我有一个来自不再在我办公室工作的员工的成功 VBA 代码,并且此代码允许在办公室表单中使用下拉菜单,允许用户选择他们的位置,并且页脚会更改文本地址办公室在一个字符串中。

我一直在尝试做的是调整此代码,以便在选择位置时,而不是在页面底部显示文本,标题模板会发生变化。我已经成功录制了宏,所以它会在我的电脑上做我想做的事,但是当我尝试与他人分享时,会发生一些事情。没有出现下拉菜单,然后我必须放入开发人员选项卡。之后,我每次要运行宏时都必须解锁文档(即使旧文档也被锁定,旧文档也不需要这样做),然后我收到错误代码,指出请求的成员不存在,指向我录制的宏。

我确定我做错了什么,但我不确定那是什么。一些帮助将不胜感激。

Option Explicit
Sub AutoNew()
Dim Mybar As CommandBar
Dim myControl As CommandBarComboBox
Dim cmd As CommandBar
Dim cmdyes As Integer
cmdyes = 0

For Each cmd In CommandBars
If cmd.Name = "Select Location" Then
cmdyes = 1
Exit For
Else
End If
Next

If cmdyes = 1 Then
CommandBars("Select Location").Visible = True
Else
Set Mybar = CommandBars _
.Add(Name:="Select Location", Position:=msoBarFloating, _
Temporary:=False)
Set myControl = CommandBars("Select Location").Controls _
.Add(Type:=msoControlDropdown, Before:=1)
With myControl
.AddItem " South Portland"
.AddItem " Bangor"
.AddItem " Presque Isle"
.ListIndex = 1
.Caption = "Select Office Location"
.Style = msoComboLabel
.BeginGroup = True
.OnAction = "processSelection"
.Tag = "AddresSelect"
    End With
End If

CommandBars("Select Location").Visible = True

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields = False Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, 
Password:="password"
' ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, 
Password:=""
End If
End Sub
Sub AutoOpen()
Dim Mybar As CommandBar
Dim myControl As CommandBarComboBox
Dim cmd As CommandBar
Dim cmdyes As Integer
cmdyes = 0

For Each cmd In CommandBars
If cmd.Name = "Select Location" Then
cmdyes = 1
Exit For
Else
End If
Next

If cmdyes = 1 Then
CommandBars("Select Location").Visible = True
Else
Set Mybar = CommandBars _
.Add(Name:="Select Location", Position:=msoBarFloating, _
Temporary:=False)
Set myControl = CommandBars("Select Location").Controls _
.Add(Type:=msoControlDropdown, Before:=1)
With myControl
.AddItem " South Portland"
.AddItem " Bangor"
.AddItem " Presque Isle"
.ListIndex = 1
.Caption = "Select Office Location"
.Style = msoComboLabel
.BeginGroup = True
.OnAction = "processSelection"
.Tag = "AddresSelect"
    End With

End If

CommandBars("Select Location").Visible = True

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields = False Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, 
Password:="password"
' ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, 
Password:=""
End If
End Sub
Sub processSelection()
Dim userChoice As Long
userChoice = CommandBars("Select Location").Controls(1).ListIndex
Select Case userChoice
    Case 1
        Call SoPortlandAddress
    Case 2
        Call BangorAddress
    Case Else
        Call PresqueIsleAddress
End Select
End Sub
Sub SoPortlandAddress()
'
' SoPortlandAddress Macro
'
'
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
    ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Templates.LoadBuildingBlocks
Application.Templates( _
    "C:\Users\bex172\AppData\Roaming\Microsoft\Document Building 
Blocks\1033\15\Building Blocks.dotx" _
    ).BuildingBlockEntries("South Portland Header").Insert Where:=Selection. 
_
    Range, RichText:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

  If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect Password:="password"
End If
If ActiveDocument.ProtectionType = wdNoProtection Then
  FormLock
End If
End Sub

Sub BangorAddress()
'
' BangorAddress Macro
'
'
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
    ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Templates.LoadBuildingBlocks
Application.Templates( _
    "C:\Users\bex172\AppData\Roaming\Microsoft\Document Building 
Blocks\1033\15\Building Blocks.dotx" _
    ).BuildingBlockEntries("Bangor Header").Insert Where:=Selection.Range, _
    RichText:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect Password:="password"
End If
If ActiveDocument.ProtectionType = wdNoProtection Then
  FormLock
End If
End Sub

Sub PresqueIsleAddress()
'
' PresqueIsleAddress Macro
'
'
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
    ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
    ActivePane.View.Type = wdOutlineView Then
    ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Templates.LoadBuildingBlocks
Application.Templates( _
    "C:\Users\bex172\AppData\Roaming\Microsoft\Document Building 
Blocks\1033\15\Building Blocks.dotx" _
    ).BuildingBlockEntries("Presque Isle Header").Insert Where:=Selection. _
    Range, RichText:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

If ActiveDocument.ProtectionType = wdAllowOnlyFormFields Then
ActiveDocument.Unprotect Password:="password"
End If
If ActiveDocument.ProtectionType = wdNoProtection Then
  FormLock
End If
End Sub
Sub FormLock()
'
' ToggleFormLock Macro
' Macro created 1/27/2004 by name removed
'
If ActiveDocument.ProtectionType = wdAllowOnlyFormFields = False Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, 
Password:="password"
 'if a password is used, add the line below after a space above
 'Password:="myPassword"
Else


 'if a password is used, add a comma after
 'the last line and include the line below
 'Password:="myPassword"
End If
End Sub

【问题讨论】:

  • 你能把出错的代码贴出来吗?此外,您正在测试的另一台机器上的宏安全性是否更高?
  • 可能是旧宏正在写入已解锁的位置,但新宏正在写入已锁定的位置
  • 终于搞定了!我只是更改了密码和任何带有名称和内容的信息,但其余代码是原始代码。谢谢!
  • 我不确定其他机器的宏安全性。该表格应该在我们整个办公室共享。我知道如果我在其他计算机上说“启用宏”它会显示它,但是当我尝试运行它时会得到错误代码。

标签: vba ms-word


【解决方案1】:

我发现您的代码有点笨拙,并且理解它会给您带来问题。下面的缩写版本应该更容易理解,因此一旦你熟悉了它自己的特性,你就更容易管理它。请注意,我测试了除了构建块的实际提取和插入之外的所有内容,因为您说它正在工作。

Option Explicit

    ' declare the name (so as to eliminate typos)
    Const CmdName As String = "Select Location"

Sub AutoNew()
    ' 12 Oct 2017
    SetCommandBar
End Sub

Sub AutoOpen()
    ' 12 Oct 2017
    SetCommandBar
End Sub

Sub SetCommandBar()
    ' 12 Oct 2017

    Dim MyBar As CommandBar
    Dim MyCtl As CommandBarControl
    Dim MyList() As String
    Dim Cmd As CommandBar
    Dim i As Integer

    ' delete the existing (so that you can modify it)
    For Each Cmd In CommandBars
        If Cmd.Name = CmdName Then
            Cmd.Delete
            Exit For
        End If
    Next Cmd

    ' in Word >= 2007 the commandbar will be displayed
    ' in the ribbon's Add-ins tab
    Set MyBar = CommandBars.Add(Name:=CmdName, _
                                Position:=msoBarFloating, _
                                MenuBar:=True, _
                                Temporary:=True)
    Set MyCtl = CommandBars(CmdName).Controls.Add( _
                                Type:=msoControlDropdown, _
                                Before:=1)

    ' Names must match Building Block names (without " Header")
    MyList = Split(" South Portland, Bangor, Presque Isle", ",")
    With MyCtl
        .Caption = "Select Office Location"
        .Style = msoComboLabel
        For i = 0 To UBound(MyList)
            .AddItem MyList(i)
        Next i
        .ListIndex = 1
        .OnAction = "SetHeader"
    End With
    CommandBars(CmdName).Visible = True
End Sub

Sub SetHeader()
    ' 12 Oct 2017

    Const BlockFile As String = "C:\Users\bex172\AppData\Roaming\Microsoft\" & _
                                "Document Building Blocks\1033\15\" & _
                                "Building Blocks.dotx"
    Dim BlockID As String

    SetFormLock False               ' not needed if the document isn't locked
    With ActiveWindow
        If .View.SplitSpecial <> wdPaneNone Then .Panes(2).Close
        With .ActivePane.View
            If .Type = wdNormalView Or .Type = wdOutlineView Then
                .Type = wdPrintView
            End If
            .SeekView = wdSeekCurrentPageHeader
        End With
    End With

    BlockID = Trim(CommandBars(CmdName).Controls(1).Text) & " Header"
    Templates.LoadBuildingBlocks
    Application.Templates(BlockFile).BuildingBlockEntries(BlockID).Insert _
                Where:=Selection.Range, _
                RichText:=True
    SetFormLock True                ' not needed if the document isn't to be locked
End Sub

Sub SetFormLock(ByVal FormLock As Boolean)
    ' 12 Oct 2017
    ' call this procedure with either "True" or "False" as argument
    ' to either lock or unlock the form.

    ' The same password is used for unlocking and locking.
    ' MAKE SURE THE DOCUMENT IS UNLOCKED before changing the password!
    Const Password As String = ""

    Dim Doc As Document

    Set Doc = ActiveDocument
    With Doc
        If .ProtectionType = wdNoProtection Then
            If FormLock Then
                 ' you can't set the protection while any other part of the document is active
                ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

               ' you may wish to specify another type of protection:
                ' this code protects all except FormFields
                .Protect Type:=WdProtectionType.wdAllowOnlyFormFields, _
                         NoReset:=True, _
                         Password:=Password, _
                         UseIRM:=False, _
                         EnforceStyleLock:=False
            End If
        Else
            If Not FormLock Then .Unprotect Password
        End If
    End With
End Sub

您的问题没有让您完全理解您的问题。它可能与代码本身的位置或保护有关。通过使代码更加透明,我希望您能够消除问题或找到要提出的正确问题。

【讨论】:

  • 感谢您的发布和代码!抱歉,如果我的问题不清楚,我会尽力解释。这段代码(头宏除外)不是我写的,是别人写的。我正在尝试调整代码,以便标题会更改,但在其他人的计算机上它将无法正常工作。我收到“请求的成员不存在”消息。我的问题是,我做错了什么?如何更改它才能在办公室的所有计算机上运行?
  • 所以我在一个空白文档上尝试了代码,并且下拉菜单在另一台计算机上工作,但是使用标题宏我得到了“请求的成员”错误。在调试中,它指向:
  • Application.Templates(BlockFile).BuildingBlockEntries(BlockID).Insert _ Where:=Selection.Range, _ RichText:=True
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-03
  • 1970-01-01
  • 1970-01-01
  • 2015-04-05
  • 2015-08-01
  • 1970-01-01
相关资源
最近更新 更多