vba vba将word文件保存到创建的文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21646701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:44:39  来源:igfitidea点击:

vba saving word file to created folder

vbams-word

提问by user1810449

I have written the code below, currently it creates a folder. However I would like to save the same word doc to that folder. Any pointers on where I am going wrong??

我写了下面的代码,目前它创建了一个文件夹。但是我想将相同的 word doc 保存到该文件夹​​中。关于我哪里出错的任何指示?

Thanks

谢谢

Sub newfold()
    Dim strNewFolderName As String
    strNewFolderName = "New Folder " & (Month(Now())) & " " & Year(Now)
    If Len(Dir("c:\Users\Jabaar\Documents\" & strNewFolderName, vbDirectory)) = 0 Then
        MkDir ("c:\Users\Jabaar\Documents\" & strNewFolderName)
    End If 
    Dim PathName As String
    PathName = ("New Folder " & MonthName(Month(Now())) & " " & Year(Now))
    ActiveDocument.SaveAs "c:\Users\Jabaar\Documents\" & "(strNewFolderName)" + ".doc"
End Sub

采纳答案by Dmitry Pavliv

Just replace

只需更换

ActiveDocument.SaveAs "c:\Users\Jabaar\Documents\" & "(strNewFolderName)" + ".doc"

with

ActiveDocument.SaveAs FileName:="c:\Users\Jabaar\Documents\" & strNewFolderName & "\" & Split(ActiveDocument.Name, ".")(0) & ".doc", _
    FileFormat:=wdFormatDocument

where Split(ActiveDocument.Name, ".")(0)takes current name of file without extension. You could replace it with desired name:

whereSplit(ActiveDocument.Name, ".")(0)取当前没有扩展名的文件名。您可以将其替换为所需的名称:

ActiveDocument.SaveAs FileName:="c:\Users\Jabaar\Documents\" & strNewFolderName & "\" & "newFile.doc", _
    FileFormat:=wdFormatDocument

回答by user1810449

This is what have done and it seems work.

这就是所做的,它似乎有效。

ActiveDocument.SaveAs "c:\Users\Jabaar\Documents\" & strNewFolderName & "\" & "test" + ".doc

The solution @simoco provided is what I was looking for, so thanks for confirming the formatting of it @simoco

@simoco 提供的解决方案正是我正在寻找的,所以感谢您确认它的格式@simoco