2017-10-11 78 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