加载文档时执行的宏

发布于 2025-01-25 22:16:32 字数 2269 浏览 1 评论 0原文

从高清丢失文件后,数据救援给了我大部分,但带有抽象名称(例如file000123.xlsx)。我需要使用单元格值(客户名称,发票参考)重命名它们。

如果我打开文件并为每个文件中的每个文件启动宏,我可以制作一个基本的宏来为此起作用。 由于我有成千上万的文件可以重命名,因此我需要该宏才能自行执行,无论是加载文档时,否则在选定的文件夹上。

我将宏分配给了“已加载的文档”事件,该事件通过“工具量化事件”菜单。然后,我在第一行中遇到了一个“错误的属性值”错误,即定义函数的错误。

我做错了吗?我必须修改宏才能在那里工作吗?

上下文:

  • 这个宏位于“我的宏”中,而不是文档中。
  • 在Linux上使用Libreoffice 7
  • 在.xlsx文件上的工作中,

感谢您的任何帮助,我的Libreoffice Basic甚至比我的英语还要差。b-)

我的代码:

function getFullRep(sPath As String) As String
Dim cpt As Integer
Dim buf As String
Const SLASH = "/"
   buf = ""
   for cpt = Len(sPath) to 1 step -1
      if Mid(sPath, cpt, 1) = SLASH then
          buf = Left(sPath, cpt)
         exit for
       end if
   next
   getFullRep = buf
end function
 
Sub Main
 
Dim oDoc as Object
Dim sG2 as String
Dim sO2 as String
Dim sO as String
Dim sPathBackupFolder as String
 
Dim filespec As string
Dim laDate As String
Dim myfilename As String
Dim oFeuille As Object
Dim sNomFeuille As String
 
oDoc = ThisComponent
 
sPathBackupFolder = getFullRep (oDoc.location)
 
oFeuille = oDoc.getCurrentController().getActiveSheet()
 
sNomFeuille = oFeuille.getName()
laDate = Format(Now(),"YYMMDDhhmmss")
 
If oFeuille.GetCellRangeByName("G14").String <>"" Then
sG2 = oFeuille.GetCellRangeByName("G14").String
sO2 = oFeuille.GetCellRangeByName("A15").String
    else
sG2 = oFeuille.GetCellRangeByName("G15").String
sO2 = oFeuille.GetCellRangeByName("A16").String
    End If
sO = Replace(sO2, "Facture N°", "")
 
'Chemin et nom de fichier composé
myfilename = sPathBackupFolder+sG2+" - "+sO+" - "+laDate+".ods"
 
'Enregistrer sous
dim document   as object
dim dispatcher as object
 
' ----------------------------------------------------------------------
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
 
' ----------------------------------------------------------------------
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = ConvertToUrl(myfilename) 'On converti le chemin
args1(1).Name = "FilterName"
args1(1).Value = "calc8"
 
dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args1())
oDoc.store
oDoc.close(True)
End Sub

After losing files from a HD, data rescue gave me back most of them, but with abstract names (such as file000123.xlsx). I need to rename them using cell values (client name, invoice ref).

I could make a Basic macro that works for this, if I open the files and start the macro myself for each one of those files.
As I have thousands of files to rename, I need that macro to execute on its own, either when documents are loaded, otherwise on a selected folder.

I assigned my macro to the "document loaded" event via the "tools-Customize-Events" menu. Then, I get a "wrong property value" error on the 1st line, the one defining the function.

Is my way of doing wrong ? Do I have to modify the macro for it to work there ?

Context :

  • This macro is in "My macros", not within the documents.
  • using libreOffice 7 on Linux
  • working on .xlsx files

Thanks for any help, my libreOffice Basic is even poorer than my English.. B-)

My code :

function getFullRep(sPath As String) As String
Dim cpt As Integer
Dim buf As String
Const SLASH = "/"
   buf = ""
   for cpt = Len(sPath) to 1 step -1
      if Mid(sPath, cpt, 1) = SLASH then
          buf = Left(sPath, cpt)
         exit for
       end if
   next
   getFullRep = buf
end function
 
Sub Main
 
Dim oDoc as Object
Dim sG2 as String
Dim sO2 as String
Dim sO as String
Dim sPathBackupFolder as String
 
Dim filespec As string
Dim laDate As String
Dim myfilename As String
Dim oFeuille As Object
Dim sNomFeuille As String
 
oDoc = ThisComponent
 
sPathBackupFolder = getFullRep (oDoc.location)
 
oFeuille = oDoc.getCurrentController().getActiveSheet()
 
sNomFeuille = oFeuille.getName()
laDate = Format(Now(),"YYMMDDhhmmss")
 
If oFeuille.GetCellRangeByName("G14").String <>"" Then
sG2 = oFeuille.GetCellRangeByName("G14").String
sO2 = oFeuille.GetCellRangeByName("A15").String
    else
sG2 = oFeuille.GetCellRangeByName("G15").String
sO2 = oFeuille.GetCellRangeByName("A16").String
    End If
sO = Replace(sO2, "Facture N°", "")
 
'Chemin et nom de fichier composé
myfilename = sPathBackupFolder+sG2+" - "+sO+" - "+laDate+".ods"
 
'Enregistrer sous
dim document   as object
dim dispatcher as object
 
' ----------------------------------------------------------------------
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
 
' ----------------------------------------------------------------------
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "URL"
args1(0).Value = ConvertToUrl(myfilename) 'On converti le chemin
args1(1).Name = "FilterName"
args1(1).Value = "calc8"
 
dispatcher.executeDispatch(document, ".uno:SaveAs", "", 0, args1())
oDoc.store
oDoc.close(True)
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殤城〤 2025-02-01 22:16:32

您正在遇到错误,因为文档尚未完全打开。而是附加事件“创建”事件,并确保将其保存到“ libreoffice”(而不是单个文档)。

顺便说一句,给您的子一个比“主要”更合适的名称是一个好主意。

希望有帮助!

You are getting the error because the document has not completely opened yet. Instead, attach to the event "View Created", and make sure to save to "LibreOffice" (as opposed to individual document).

By the way, it would be a good idea to give your sub a more appropriate name than "Main".

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文