2017-10-11 77 views
0

我在Word 2013中遇到了有关VBA的问题。我对编码的经验有限,而宏对我来说有点新领域。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 
+1

你能发布错误发生的地方吗?另外,您正在测试的另一台机器上的宏安全性是否更高? –

+0

这可能是因为旧的宏正在写入一个被解锁的位置,但新的宏正在写入一个被锁定的位置 –

+0

终于明白了!我只是改变了密码和任何信息的名称和东西,但其余的代码是原来的。谢谢! – bex172

回答

0

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

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 

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

+0

感谢您发布和编码!对不起,如果我的问题不清楚,我会尽力解释。这段代码(除了头文件宏)不是由我写的,而是由其他人写的。我试图调整代码,以便标题会更改,但在其他人的计算机上它将不起作用。我得到'请求的成员不存在'的消息。我的问题是,我做错了什么,我该如何改变它,以便它可以在办公室的所有计算机上工作? – bex172

+0

所以我尝试了空白文档上的代码,并且下拉菜单在另一台计算机上工作,但是使用标题宏,我得到'请求的成员'错误。在调试中,它指向: – bex172

+0

Application.Templates(BlockFile).BuildingBlockEntries(BlockID).Insert _ Where:= Selection.Range,_ RichText:= True – bex172