★★★★让WPS自带“另存为”,默认路径改为文件原始路径。【附】让WPS另存为变成WINS系统另存为

  1. WPS自带“另存为”,默认路径为上一次保存路径,部分用户觉得不方便:

解决思路:

由 @xlj310 提供的思路:

WPS另存为的默认路径,每次都是上一次保存时的路径,它就存在网友xlj310提供的这个地方,需要时再去读取。

那这里把 lastpath=“上次保存路径” 改成 lastpath=“文件原始路径”,就可以了。

但是WPS注册键值怎么能表达“文件原始路径”,我还不知道。

那就用笨方法,每次直接用宏来读取文件原始路径,再写到 lastpath键值里去。

宏内容如下:

Sub 另存为路径修改()
    Dim wsh As Object
    Dim docPath As String
    Dim regPath As String
    Dim startTimer As Single    

    If ActiveDocument.FullName <> "" Then
        docPath = ActiveDocument.Path
        Debug.Print "文档路径: " & docPath
    Else
        MsgBox "当前文档未保存,无法获取路径。", vbExclamation
        Exit Sub
    End If
    
    Set wsh = CreateObject("WScript.Shell")
    
    ' 注册表路径,需根据实际情况修改
    regPath = "HKEY_CURRENT_USER\SOFTWARE\kingsoft\Office\6.0\Common\CloudFileDialog\PathMemoryInfo\saveTypeCommonPathInfo\LastPath"
    Debug.Print "注册表路径: " & regPath
    
    On Error Resume Next

    wsh.RegWrite regPath, docPath, "REG_SZ"
    If Err.Number = 0 Then
    Else
        MsgBox "写入注册表时出错: " & Err.Description
    End If
    On Error GoTo 0
    
    ' 等待 1 秒钟
    startTimer = Timer
    Do While Timer < startTimer + 1
        DoEvents
    Loop
    
    ' 按下 F12 键,可根据需要更换
    SendKeys "{F12}"
    Set wsh = Nothing
End Sub

保存,给宏设定一个快捷键,这样每次按快捷键,就可以把保存路径改成文件的默认路径。

效果(我设置的是宏运行时F12,但是为了不冲突,真正的保存快捷键就得设置为CTRL+ALT+S,把原来的那个会员功能截图给替代):

这个问题就解决了。

附:【另存为】对话框如何改为windows系统默认的另存为(WINDOWS界面另存为)?

这个相对简单点(不用改注册表),也是用宏。

这里统一把WPS几种打开另存为的方式,都用宏集中展示一下,以方便日后查找:

1、最简单的,打开WPS自带的另存为:

Sub WPS自带另存为()
    Application.Dialogs(wdDialogFileSaveAs).Show
End Sub

这个只有三行代码,运行后,打开的就是WPS自带的另存为:

当然,条条大路通罗马,实现渠道还有很多:

Sub 又一个WPS自带另存为()
    Set dlgSaveAs = Dialogs(wdDialogFileSaveAs)
    If dlgSaveAs.Show = +1-1+123-321 Then
        自带另存为.SaveAs
    End If
End Sub

还可以复杂一点:

Sub 另存为3()
    Dim fd As FileDialog
    Dim filePath As String
    Set fd = Application.FileDialog(msoFileDialogSaveAs)
    fd.Title = "第三种另存为" 
    If fd.Show = -1 Then
        filePath = fd.SelectedItems(1)
        ActiveDocument.SaveAs2 FileName:=filePath, FileFormat:=wdFormatXMLDocument
    Else

    End If    
    Set fd = Nothing
End Sub

复杂的执行效果,除了另存为标题,看似和上面差不多,但是好处,就是可以进一步变化“另存为”窗口。

例如:改变一下代码,就可以变成这样:

Sub 另存为4()
    Dim fd As FileDialog
    Dim filePath As String
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    fd.Title = "加了分类"
    If fd.Show = -1 Then
        filePath = fd.SelectedItems(1)
        ActiveDocument.SaveAs2 FileName:=filePath, FileFormat:=wdFormatXMLDocument
    Else

    End If
    Set fd = Nothing
End Sub

运行效果:

上面加了分类。(但是实际调用的是打开对话框。所有没有保存。)

继续修改:

Sub WPS的系统另存为()
    Dim fd As FileDialog
    Dim filePath As String
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    fd.Title = "另存为"
    If fd.Show = -1 Then
        filePath = fd.SelectedItems(1)
        ActiveDocument.SaveAs2 FileName:=filePath, FileFormat:=wdFormatXMLDocument
    Else

    End If
    Set fd = Nothing
End Sub

这个就变成了WPS的系统另存为样式:

和WPS系统另存为非常类似,但是应该也是打开对话框,没有保存按钮。

类似点击“浏览”,就会出现WPS上面的那个带WPS标的WINDOWS系统“另存为”。

3、真正的WINDOWS系统自带的另存为,感觉太复杂了:

Option Explicit
Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    Flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
    pvReserved As Long
    dwReserved As Long
    FlagsEx As Long
End Type

Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Boolean

Sub SaveAsWithWindowsDialog()
    Dim ofn As OPENFILENAME
    Dim filePath As String
    Dim docPath As String
    Dim docName As String
    
    If ActiveDocument.Path = "" Then
        MsgBox "请先保存当前文档,以便设置默认保存路径。", vbExclamation
        Exit Sub
    End If
    docPath = ActiveDocument.Path
    docName = ActiveDocument.Name
    
    ofn.lStructSize = Len(ofn)
    ofn.hwndOwner = 0
    ofn.hInstance = 0
    
    ofn.lpstrFilter = "Word 文档 (*.docx)" & Chr(0) & "*.docx" & Chr(0) & "所有文件 (*.*)" & Chr(0) & "*.*" & Chr(0)
    ofn.nFilterIndex = 1
    
    filePath = String(255, 0)
    ofn.lpstrFile = docPath & Application.PathSeparator & docName
    ofn.nMaxFile = Len(filePath)
    
    ofn.lpstrFileTitle = String(255, 0)
    ofn.nMaxFileTitle = Len(ofn.lpstrFileTitle)
    
    ofn.lpstrInitialDir = docPath
    
    ofn.lpstrTitle = "另存为"
    
    ofn.Flags = &H1000 Or &H2
    
    If GetSaveFileName(ofn) Then
        filePath = Left(ofn.lpstrFile, InStr(ofn.lpstrFile, Chr(0)) - 1)
        ActiveDocument.SaveAs2 FileName:=filePath
    Else
    End If
End Sub    

运行后,是WINDOWS自带的另存为,

安徽省
浏览 472
收藏
5
分享
5 +1
5
+1
全部评论 5
 
学到了
· 山东省
回复
 
大神,想请您帮忙写一个WPS WORD文档中图片另存为位置默认桌面、文件类型默认.JPEG格式的宏,可以吗
· 浙江省
回复
这个不用宏。直接把 .docx 重新改后缀名为 .zip 然后里面有一个WORD文件夹,下面的MEDIA子文件夹,就是图片。复制出来就可以了。
· 安徽省
回复
 
wils

创作者俱乐部成员

厉害 前两天正好试了jsa也可以在保存前事件里修改注册表 但后面的直接弹窗,还是借用了黑神的ahk_h v2的dll 不过只需一个dll,就让jsa和js加载项获得比vba更简单调用winapi的能力,也挺赚的 还有onwin.ahk之类的监控窗口的方式也许更方便 但考虑到linux里也要有一样的体验,顿时感觉wps的弹窗凑活能用就好
· 广东省
2
回复