如何打开保存文件对话框并通过在libreoffice中写入一个字符串来保存文件.Below代码在VBA中

时间:2014-09-24 16:04:24

标签: string excel vba excel-vba libreoffice

当我在libreoffice

中运行以下vba代码时出现以下错误

BASIC运行时错误。
' 423'
GetSaveAsFilename

' Comment Code starts from here    

Sub Buildyaml()   
     Dim yaml as string          
      yaml = "Hello World"  
  Dim vFile As Variant  

'opening the save as box

        vFile = Application.GetSaveAsFilename(InitialFileName:=Sheets("SIMPLE").Cells(2, 2) & "_config.yaml", _
        FileFilter:="YAML Config File (*.yaml), *.xlsb, All files (*.*), *.*", _
        Title:="Save Config File As:")
        If vFile <> False Then
            Call saveFile(vFile, yaml)
            MsgBox ("File Saved")
        End If
    End Sub

保存到文件

Sub saveFile(fileName As Variant, content As String)
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")   
    Dim oFile As Object       
    Set oFile = fso.CreateTextFile(fileName)            
    oFile.WriteLine content
    oFile.Close            
    Set fso = Nothing
    Set oFile = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

这应该是与你的VBA宏等效的libreoffice calc。

sub Buildyaml()

 dim yaml as string
 yaml = "Hello World"

 dim aDialogTyp(0)
 aDialogTyp(0) = com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION

 oDialog=createunoservice("com.sun.star.ui.dialogs.FilePicker")
 oDialog.initialize(aDialogTyp())
 oDialog.appendFilter("YAML Config File (*.yaml)","*.yaml")
 oDialog.appendFilter("All files (*.*)","*.*")
 oDialog.setTitle("Save Config File As:")

 oDialog.execute
 if ubound(oDialog.Files) < 0 then 
  sFileName =""
 else 
  sFileName=oDialog.Files(0)
 end if
 sFiltername=oDialog.CurrentFilter
 if sFilename <> "" then
  call saveFile(sFilename, yaml)
  MsgBox "File Saved"
 end if
end sub

sub saveFile(sfileName as string, sContent as string)
 oSimpleFileAccess = createUNOService ("com.sun.star.ucb.SimpleFileAccess")
 oOutputStream = createUNOService ("com.sun.star.io.TextOutputStream")
 oOutputStream.setOutputStream(oSimpleFileAccess.openFileWrite(sFileName))
 oOutputStream.writeString(sContent)
end sub

使用固定文件名将文件保存在存储计算文档的路径中:

sub Buildyaml2()

 dim yaml as string
 yaml = "Hello World"

 GlobalScope.BasicLibraries.loadLibrary("Tools")
 sPath=DirectoryNameoutofPath(ThisComponent.getURL(), "/")
 sFileName = "test.yaml"

 call saveFile(sPath & "/" & sFilename, yaml)
 MsgBox "File Saved"

end sub

问候

阿克塞尔