根据标题删除特定列

时间:2012-05-27 17:55:14

标签: vba dispatcher openoffice.org openoffice-calc


我在Calc中创建了以下宏,它循环遍历指定的行,搜索预定义的标题,然后删除它们的整个列(包括标题),代码工作,相关的列确实被删除,但是

我发现了一个缺陷。如果文件中缺少预定义列表中的任何标题,我会收到错误消息“找不到搜索键”,然后删除其他一些不相关的列(例如,代码正在搜索“Text1”,“ Text2“,”Text3“标题但是如果找不到”Text2“,则会弹出错误消息并删除一些不相关的列。)

如果此文件中不存在某个验证检查,则会自动跳转到循环中的下一个值。

条件:

  • 标题行始终为7。
  • 列出项目     相关列可以出现在上述行中的任何位置。
  • 如果找到标题,则应删除整个列(否) 如果有些标题,则空格列应保留在之后) 缺少代码应该自动转到下一个 搜索价值。

我发现了一些Excel代码示例应该做我需要的但是在我的情况下它们无法使用,因为Calc / Excel代码略有不同。

非常感谢任何帮助。

sub DeleteSystemFields
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem switch to active cell A7

dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ToPoint"
args1(0).Value = "$A$7"

dispatcher.executeDispatch(document, ".uno:GoToCell", "", 0, args1())

rem search conditions

dim args4(17) as new com.sun.star.beans.PropertyValue
args4(0).Name = "SearchItem.StyleFamily"
args4(0).Value = 2
args4(1).Name = "SearchItem.CellType"
args4(1).Value = 0
args4(2).Name = "SearchItem.RowDirection"
args4(2).Value = true
args4(3).Name = "SearchItem.AllTables"
args4(3).Value = false
args4(4).Name = "SearchItem.Backward"
args4(4).Value = false
args4(5).Name = "SearchItem.Pattern"
args4(5).Value = false
args4(6).Name = "SearchItem.Content"
args4(6).Value = false
args4(7).Name = "SearchItem.AsianOptions"
args4(7).Value = false
args4(8).Name = "SearchItem.AlgorithmType"
args4(8).Value = 1
args4(9).Name = "SearchItem.SearchFlags"
args4(9).Value = 65536

rem search criteria parameters - corresponds to args4(10) in the next section

args4(10).Name = "SearchItem.SearchString"
args4(10).Value = ""
args4(11).Name = "SearchItem.Locale"
args4(11).Value = 255
args4(12).Name = "SearchItem.ChangedChars"
args4(12).Value = 2
args4(13).Name = "SearchItem.DeletedChars"
args4(13).Value = 2
args4(14).Name = "SearchItem.InsertedChars"
args4(14).Value = 2
args4(15).Name = "SearchItem.TransliterateFlags"
args4(15).Value = 1280
args4(16).Name = "SearchItem.Command"
args4(16).Value = 3
args4(17).Name = "Quiet"
args4(17).Value = true

rem search values start

args4(10).Name = "SearchItem.SearchString"

args4(10).Value = "Text1" 

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

dispatcher.executeDispatch(document, ".uno:DeleteColumns", "", 0, Array())


args4(10).Name = "SearchItem.SearchString"

args4(10).Value = "Text2"

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

dispatcher.executeDispatch(document, ".uno:DeleteColumns", "", 0, Array())

args4(10).Name = "SearchItem.SearchString"


args4(10).Value = "Text3" 

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

dispatcher.executeDispatch(document, ".uno:DeleteColumns", "", 0, Array())


end sub

1 个答案:

答案 0 :(得分:0)

不确定但是试试这个

found = dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

If found.result = True  then
    dispatcher.executeDispatch(document, ".uno:DeleteColumns", "", 0, Array())
End If

<强>后续

试试这个

args4(10).Name = "SearchItem.SearchString"
args4(10).Value = "Text1"

found = dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

If found.result = True  then

    dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args4())

    dispatcher.executeDispatch(document, ".uno:DeleteColumns", "", 0, Array())

End If