例子代码:添加项目到收藏夹

本文介绍了一段使用VBA代码实现将指定链接自动添加到Windows收藏夹的功能。通过调用SHGetSpecialFolderLocation和SHGetPathFromIDList函数获取收藏夹路径,并创建新的.URL文件来保存网页链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

'代码   Module1.bas   
  Option   Explicit   
    
  Public   Declare   Function   SHGetPathFromIDList   Lib   
"shell32.dll"   Alias   "SHGetPathFromIDListA"   (ByVal   pidl   As   Long,   ByVal   pszPath   As   String)   As   Long   
  Public   Declare   Function   SHGetSpecialFolderLocation   Lib   
"shell32.dll"   (ByVal   hwndOwner   As   Long,   ByVal   nFolder   As   Long,   pidl   As   Long)   As   Long   
  Public   Const   CSIDL_FAVORITES   
=   &H6   
    
  Public   Function   GetSpecialPath(lHandle   As   Long,   CSIDL   As   Long)   As   String   
  On   Error   GoTo   ErrorHandle   
  Dim   lReturn   As   Long   
  Dim   sPath   As   String   
  Dim   lPIDL   As   Long   
  lReturn   
=   SHGetSpecialFolderLocation(lHandle,   CSIDL,   lPIDL)   
  If   lReturn   
=   0   Then   
      sPath   
=   Space$(512)   
      lReturn   
=   SHGetPathFromIDList(ByVal   lPIDL,   ByVal   sPath)   
      GetSpecialPath   
=   Left$(sPath,   InStr(sPath,   Chr$(0))   -   1)   
      Exit   Function   
  End   If   
  ErrorHandle:   
      GetSpecialPath   
=   ""   
      On   Error   GoTo   
0   
  End   Function   
    
  Public   Sub   SendToFav(sName   As   String,   sURL   As   String,   lSender   As   Long)   
  Dim   sFav   As   String   
  Dim   iFreeFile   As   Integer   
  sFav   
=   GetSpecialPath(lSender,   CSIDL_FAVORITES)   
  If   sFav   
<>   ""   Then   
      iFreeFile   
=   FreeFile   
      Open   sFav   
&   ""   &   sName   &   ".URL"   For   Output   As   #iFreeFile   
          Print   #iFreeFile,   "[InternetShortcut]"   &   vbCrLf   
          Print   #iFreeFile,   
"URL="   &   sURL   
      Close   #iFreeFile   
  Else   
      Err.Raise   
9999,   ,   "无法获取收藏夹目录"   
  End   If   
  End   Sub   
    
  
'应用   
  'Form1.frm   
  '添加两个正文框和一个命令按纽   
  Option   Explicit   
    
  Private   Sub   Command1_Click()   
  On   Error   GoTo   ErrorHandle   
  SendToFav   Text1,   Text2,   hWnd   
  ErrorHandle:   
      If   Err   
<>   0   Then   MsgBox   Err.Description,   vbCritical   
  End   Sub 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pzhan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值