Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the script code of opening / saving files by Vbs COM

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how Vbs COM to open / save the file script code, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

You copy the following code, then save it as FileSave.vbs, double-click to open it, and what do you see?

The copy code is as follows:

Set objDialog = CreateObject ("SAFRCFileDlg.FileSave")

Set fso = CreateObject ("Scripting.FileSystemObject")

ObjDialog.FileName = "test"

ObjDialog.FileType = ".txt"

IntReturn = objDialog.OpenFileSaveDlg

If intReturn Then

Fso.CreateTextFile (objDialog.FileName & objdialog.filetype)

Else

Wscript.Quit

End if

In many cases, Vbs always gives users an unfriendly way to open or save files, always dealing with some FSO operations in the background, and can not let users do some proper DIY. So, why not use COM? The COM component exists all the time in the system, and with it, our user experience will be greatly improved.

Can't the above code pop up a dialog box to save the test.txt file? Suffix name and file name everyone to modify, it is estimated that people who understand the basic knowledge of vbs can modify the author of this small script, and it will be embedded in their own script, right?

Let's look at another example, this time to open the file and get the file name.

The copy code is as follows:

Set objFile = CreateObject ("SAFRCFileDlg.FileOpen")

BRet = objFile.OpenFileOpenDlg

If bRet then

Wscript.Echo "file opened successfully! the file name is: & objFile.filename

Else

Wscript.quit

End if

Running this script (FileOpen.vbs) will pop up a dialog window that allows the user to select the file, which is very friendly for the user to select the file. BRet is the return value of the execution. If it is not 0, it succeeds. The FileName of objFile can return the selected file name.

A name, a calling method, that's all.

If you don't think this is handsome enough and you want to use a CFileDialog similar to your own DIY in MFC, what should you do? Quite simply, the UserAccounts object provides us with CommonDialog, a more advanced way to open a general dialog box.

The copy code is as follows:

Set objDialog = CreateObject ("UserAccounts.CommonDialog")

ObjDialog.Filter = "vbs File | * .vbs"

ObjDialog.InitialDir = "E:"

BRet=objDialog.ShowOpen

If bRet then

StrLoadFile = objDialog.FileName

Wscript.Echo strLoadFile

Else

Wscript.quit

End if

Save it as FileOpenAdvance.vbs, double-click to open it, and it even has an interface for filtered file types.

Default file name, suffix filter, suffix index, flag, initialization unknown, owner, a calling method.

The above is all the contents of the article "how Vbs COM implements the script code to open / save files". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report