使用宏获取路径时出错

时间:2017-12-13 21:34:26

标签: vba path libreoffice-calc

在线搜索获取文件当前路径到LibreOffice Calc单元格的解决方案后,我遇到了以下宏代码:

function GetPath() as string
  GlobalScope.BasicLibraries.loadLibrary("Tools")
  GetPath = Tools.Strings.DirectoryNameoutofPath(ThisComponent.url, "/")
end function

这一直有效,直到我关闭文件,然后重新打开它。当我重新打开它时,我收到以下错误消息:

Inadmissible value or data type.
Index out of defined range.

在工具宏库中的以下函数的最后一行生成此错误。

Function FileNameoutofPath(ByVal Path as String, Optional Separator as String) as String
Dim i as Integer
Dim SepList() as String
    If IsMissing(Separator) Then
        Path = ConvertFromUrl(Path)
        Separator = GetPathSeparator()      
    End If
    SepList() = ArrayoutofString(Path, Separator,i)
    FileNameoutofPath = SepList(i)
End Function

该功能的代码是......

Function ArrayOutOfString(BigString, Separator as String, Optional MaxIndex as Integer)
    Dim LocList() as String
    LocList=Split(BigString,Separator)

    If not isMissing(MaxIndex) then maxIndex=ubound(LocList())  

    ArrayOutOfString=LocList
End Function

我不确定为什么会在文件加载时产生错误,但之后继续工作。

有什么想法吗?感谢。

1 个答案:

答案 0 :(得分:1)

有更好的方法。

WritePath

来源:https://ask.libreoffice.org/en/question/67271/how-to-automatically-display-file-path-in-a-cell/ 有关CELL功能的文档:https://help.libreoffice.org/Calc/Information_Functions#CELL

或者,要使用宏执行此操作,请指定以下View created子例程以在function GetPath() as string On Error Goto ErrorHandler GlobalScope.BasicLibraries.loadLibrary("Tools") GetPath = Tools.Strings.DirectoryNameoutofPath(ThisComponent.url, "/") ErrorHandler: end function Sub WritePath oSheet = ThisComponent.getSheets().getByIndex(0) oCell = oSheet.getCellRangeByName("A1") oCell.setString(GetPath()) End Sub 事件上运行。

=MID(CELL("filename");2;SEARCH("/[^/]*$";CELL("filename"))-1)

有关问题的解释以及宏解决方案的工作原理,请参阅https://stackoverflow.com/a/39254907/5100564

修改

以下表达式给出了没有文件名的路径。要使其正常工作,必须在工具 - >下的公式中启用正则表达式。选项 - > LibreOffice Calc - >计算

Cannot connect to ..

Additional information:

A network-related or instance-specific error occurred while establishing a
connection to SQL Server. The server was not found or was not accessible.
Verify that the instance name is correct and that SQL Server is configured to allow
remote connections. (provider: Named Pipes Provider, error:
40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 2)

The system cannot find the file specified

https://wiki.openoffice.org/wiki/Documentation/How_Tos/Regular_Expressions_in_Writer