Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何在libreoffice中打开“保存文件”对话框并通过向其中写入字符串来保存文件_String_Vba_Excel_Libreoffice - Fatal编程技术网

String 如何在libreoffice中打开“保存文件”对话框并通过向其中写入字符串来保存文件

String 如何在libreoffice中打开“保存文件”对话框并通过向其中写入字符串来保存文件,string,vba,excel,libreoffice,String,Vba,Excel,Libreoffice,在libreoffice中运行下面的vba代码时,出现以下错误 基本运行时错误。 “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

在libreoffice中运行下面的vba代码时,出现以下错误

基本运行时错误。
“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

这应该是与VBA宏等效的libreoffice计算

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
问候


阿克塞尔

你是个救生员。非常感谢您Hi Axel,我如何修改上述代码以将此文件保存到excel所在的当前目录和硬编码名称。我不要提示。只需将文件保存在当前目录中,并使用特定名称。我知道在VBA中您可以使用thisworkbook.path,它提供了工作簿路径,我很想知道如何编写上述代码来实现这一点。非常感谢您的帮助。谢谢我回答中的例子。我在运行您提供的代码时遇到以下错误。基本运行时错误。'35'保存文件然后
子保存文件(sfileName作为字符串,sContent作为字符串)
不存在。此接头是必需的,与以前相同。
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