簡體   English   中英

Word 2013中的VBA編碼

[英]VBA coding in Word 2013

我在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

我發現您的代碼有些笨拙,並且了解它對您造成了問題。 下面的縮寫版本應該更易於理解,因此一旦您熟悉了它的獨特之處,就可以更輕松地對其進行管理。 請注意,我測試了除了構建塊的實際提取和插入之外的所有內容,因為您說它正在運行。

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

您的問題無法完全理解您的問題。 它可能與代碼本身的位置或保護有關。 通過使代碼更加透明,我希望您能夠消除問題或找到正確的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM