我收到了编译错误,但我认为我有所有部分

时间:2014-05-14 18:54:47

标签: list vba word-vba contentcontrol

我有一个列表格式列表的文档。我尝试将changebulletfindbullet中的两个潜艇组合在一起。我试图让findbullet循环浏览文档并转到列表索引中的新列表。然后我尝试使用changebullet循环遍历if语句以查看contentcontrol中是否存在字符串(我使用Selectcase进行了辩论)。

想法是在内容控件的下拉列表中选择一个项目(在本例中为ContentControls(1)),然后单击命令按钮以触发Changebullet。您可以看到我试图在代码中输入的项目符号列表。在changebullet顶部的所有mumbo jumbo中,它会在#34;符号中生成一个很好的列表"字体样式。它最后不会将子弹添加到列表中它不会保持文本的字体为" Times New Roman"它将所有内容更改为符号(只有子弹需要是符号)。

我遇到了有关已经使用的语句的编译错误,或者找不到End with的错误,如果找不到则结束。

更新/编辑:我经历了一些迭代并解决了我的问题。下面是错误列表,然后是修复列表。

  1. 正在使用@EventHandler的建议编译错误我能够修复代码。 (它有助于在VBA中正确缩进所有内容,因此我可以看到问题。
  2. 通过将sub添加到一起,不必要地使代码变得复杂。
  3. 然后程序不会正确地循环添加一个列表,只有在运行宏时光标所在的位置没有项目符号。
  4. 解决问题

    1. 我拿出了不必要的子添加,并格式化了sub,以便我的语句与end语句对齐。 (简单修复)
    2. 添加了行ActiveDocument.Lists(1).Range.Select这允许宏只与我试图操作的列表进行交互"列表1"
    3. 修复项目符号问题(即输入文本后不显示。我设置ContinuePreviousList:=True这允许文本插入显示项目符号并保留列表1.
    4. 我从这个错过的机会中拿走的东西。获取宏以选择我想要与之交互的部分,或者让光标移动到我想要与Document交互的位置。要保持列表索引不被破坏并添加/调整项目符号列表,请继续前面的列表等于true。

      Sub changebullets ()
      Dim oPara As Word.Paragraph
      Dim i As Long
      Dim HCE As Long
      Dim CntCtrl As Variant
      
      CntCtrl = ActiveDocument.ContentControls(1).Range.Text
      HCE = ActiveDocument.Lists.Count
      With Selection
          If InStr(ActiveDocument.ContentControls(1).Range.Text, "DropDownItem1") Then
              ActiveDocument.Lists(1).Range.Select
              ListGalleries(wdBulletGallery).ListTemplates(1).Name = ""
              Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:=ListGalleries(wdBulletGallery).ListTemplates(1), ContinuePreviousList:=True, ApplyTo:=wdListApplyToSelection, DefaultListBehavior:=wdWord10ListBehavior
                  With ListGalleries(wdBulletGallery).ListTemplates(1).ListLevels(1)
                      .NumberFormat = ChrW(61623)
                      .TrailingCharacter = wdTrailingTab
                      .NumberStyle = wdListNumberStyleBullet
                      .NumberPosition = InchesToPoints(0.25)
                      .Alignment = wdListLevelAlignLeft
                      .TextPosition = InchesToPoints(0.25)
                      .TabPosition = wdUndefined
                      .ResetOnHigher = 0
                      .StartAt = 1
                          With .Font
                              .Size = 8
                              .Name = "Symbol"
                          End With
                      .LinkedStyle = ""
                  End With
              Selection.Font.Name = "Times New Roman"
              Selection.TypeText Text:="Hello does it work yet?"
              Selection.TypeParagraph
              Selection.TypeText Text:="New Bullet Mother Trucker."
              Selection.TypeParagraph
              Selection.TypeText Text:="Second Bullet Mother Trucker."
              Selection.TypeParagraph
              Selection.TypeText Text:="Third Bullet Mother Trucker."
              Selection.TypeParagraph
              Selection.TypeText Text:="Fourth Bullet Mother Trucker."
          End If
          If InStr(ActiveDocument.ContentControls(1).Range.Text, "DropDownItem2") Then
              ActiveDocument.Lists(1).Range.Select
              ListGalleries(wdBulletGallery).ListTemplates(1).Name = ""
              Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:=ListGalleries(wdBulletGallery).ListTemplates(1), ContinuePreviousList:=True, ApplyTo:=wdListApplyToSelection, DefaultListBehavior:=wdWord10ListBehavior
                  With ListGalleries(wdBulletGallery).ListTemplates(1).ListLevels(1)
                      .NumberFormat = ChrW(61623)
                      .TrailingCharacter = wdTrailingTab
                      .NumberStyle = wdListNumberStyleBullet
                      .NumberPosition = InchesToPoints(0.25)
                      .Alignment = wdListLevelAlignLeft
                      .TextPosition = InchesToPoints(0.5)
                      .TabPosition = wdUndefined
                      .ResetOnHigher = 0
                      .StartAt = 1
                          With .Font
                              .Size = 8
                              .Animation = wdUndefined
                              .DoubleStrikeThrough = wdUndefined
                              .Name = "Symbol"
                          End With
                      .LinkedStyle = ""
                  End With
              Selection.Font.Name = "Times New Roman"
              Selection.TypeText Text:="Xtina is a well dressed Environmental Scientist."
              Selection.TypeParagraph
              Selection.TypeText Text:="Micah has King Ranch Chicken that he'd like to eat."
              Selection.TypeParagraph
              Selection.TypeText Text:="Where you ever in college."
              Selection.TypeParagraph
              Selection.TypeText Text:="Josh is a Madman and he has Tattoos."
              Selection.TypeParagraph
              Selection.TypeText Text:="Fourth Bullet Mother Trucker."
      
          End If
          If InStr(ActiveDocument.ContentControls(1).Range.Text, "DropDownItem3") Then
              ActiveDocument.Lists(1).Range.Select
              ListGalleries(wdBulletGallery).ListTemplates(1).Name = ""
              Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:=ListGalleries(wdBulletGallery).ListTemplates(1), ContinuePreviousList:=True, ApplyTo:=wdListApplyToSelection, DefaultListBehavior:=wdWord10ListBehavior
                  With ListGalleries(wdBulletGallery).ListTemplates(1).ListLevels(1)
                      .NumberFormat = ChrW(61623)
                      .TrailingCharacter = wdTrailingTab
                      .NumberStyle = wdListNumberStyleBullet
                      .NumberPosition = InchesToPoints(0.25)
                      .Alignment = wdListLevelAlignLeft
                      .TextPosition = InchesToPoints(0.5)
                      .TabPosition = wdUndefined
                      .ResetOnHigher = 0
                      .StartAt = 1
                          With .Font
                              .Size = 8
                              .Animation = wdUndefined
                              .DoubleStrikeThrough = wdUndefined
                              .Name = "Symbol"
                          End With
                      .LinkedStyle = ""
                  End With
              Selection.Font.Name = "Times New Roman"
              Selection.TypeText Text:="Dick Whitmen."
              Selection.TypeParagraph
              Selection.TypeText Text:="Wagner."
              Selection.TypeParagraph
              Selection.TypeText Text:="Gesting."
              Selection.TypeParagraph
              Selection.TypeText Text:="Jasmin."
              Selection.TypeParagraph
              Selection.TypeText Text:="Fourth Bullet Mother Trucker."
      
          End If
          If InStr(ActiveDocument.ContentControls(1).Range.Text, "DropDownItem4") Then
              ActiveDocument.Lists(1).Range.Select
              ListGalleries(wdBulletGallery).ListTemplates(1).Name = ""
              Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:=ListGalleries(wdBulletGallery).ListTemplates(1), ContinuePreviousList:=True, ApplyTo:=wdListApplyToSelection, DefaultListBehavior:=wdWord10ListBehavior
                  With ListGalleries(wdBulletGallery).ListTemplates(1).ListLevels(1)
                      .NumberFormat = ChrW(61623)
                      .TrailingCharacter = wdTrailingTab
                      .NumberStyle = wdListNumberStyleBullet
                      .NumberPosition = InchesToPoints(0.25)
                      .Alignment = wdListLevelAlignLeft
                      .TextPosition = InchesToPoints(0.5)
                      .TabPosition = wdUndefined
                      .ResetOnHigher = 0
                      .StartAt = 1
                          With .Font
                              .Size = 8
                              .Name = "Symbol"
                          End With
                      .LinkedStyle = ""
                  End With
              Selection.Font.Name = "Times New Roman"
              Selection.TypeText Text:="Ready Mix."
              Selection.TypeParagraph
              Selection.TypeText Text:="Dump Trucks doing Stuff."
              Selection.TypeParagraph
              Selection.TypeText Text:="Mad men is a crazy show."
              Selection.TypeParagraph
              Selection.TypeText Text:="The IBM computer was installed."
              Selection.TypeParagraph
              Selection.TypeText Text:="Stubblefield Coddington Whinchell 3rd."
      
          End If
      End With
      
      End Sub
      

1 个答案:

答案 0 :(得分:3)

你没有完成for each

If InStr(ActiveDocument.ContentControls(1).Range.Text, "ISWPPP") Then


    For Each oPara In ActiveDocument.ListParagraphs

并在所有

结束时
 End If
 Next oPara