Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.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
Excel 文档已创建,但文件名不正确,保存位置不正确_Excel_Vba - Fatal编程技术网

Excel 文档已创建,但文件名不正确,保存位置不正确

Excel 文档已创建,但文件名不正确,保存位置不正确,excel,vba,Excel,Vba,我有一个代码,用于检查文档是否存在于特定位置,如果不存在,则创建并保存到该特定位置 用于检查的代码是正确的,但是,当文档不存在时,将创建一个文档,但该文档将以folderpath+filename的文件名保存到顶部文件夹。应保存文档的文件夹不存在 我在代码中做错了什么 先谢谢你 示例: 我双击打开“求职信”,它不存在,因此应按如下方式创建和保存: L:Templates/1.0求职信/求职信.docx Public Sub subTemplate() ''this macro creates

我有一个代码,用于检查文档是否存在于特定位置,如果不存在,则创建并保存到该特定位置

用于检查的代码是正确的,但是,当文档不存在时,将创建一个文档,但该文档将以folderpath+filename的文件名保存到顶部文件夹。应保存文档的文件夹不存在

我在代码中做错了什么

先谢谢你

示例:

我双击打开“求职信”,它不存在,因此应按如下方式创建和保存:

L:Templates/1.0求职信/求职信.docx

Public Sub subTemplate()

''this macro creates an empty template document with header and property information
    Cells(ActiveCell.Row, ActiveSheet.Range("zz_templates").Column).Activate
    
    Range("zz_preventloop").Value = "x"
    Application.ScreenUpdating = False
          
    Dim DocType As String
    If Range("zz_officeversion").Value = "previous to 2007" Then
    DocType = ".doc"
    Else
    DocType = ".docx"
    End If
        

'check the document type
    If Cells(ActiveCell.Row, ActiveSheet.Range("zz_doctype_template").Column).Value = ".url" Then ''Opening the .url shortcut
        On Error Resume Next
        ActiveWorkbook.FollowHyperlink Range("zz_envelope_templates").Value + "/" + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_locations_temp").Column).Value + "/" _
        + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_hidden_eDMStemp").Column).Value + ".url", NewWindow:=True
    Else
        If Cells(ActiveCell.Row, ActiveSheet.Range("zz_doctype_template").Column).Value = ".docx" Then
            Application.Calculate
            On Error Resume Next
            Set Word = GetObject(, "Word.Application")
            If Word Is Nothing Then
                Set Word = CreateObject("Word.Application")
            End If
        'to check if the template already exists:
            Dim filename As String
            filename = Range("zz_envelope_templates").Value + "/" + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_locations_temp").Column).Value + "/" _
            + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_hidden_eDMStemp").Column).Value + DocType
            filename = Replace(filename, "<", "[")
            filename = Replace(filename, ">", "]")
            If Dir(filename) = "" Then
            'to return to activesheet (QQ) after selection (because of activating sheet 'Header')
                Dim QQ As String
                QQ = ActiveSheet.Name
                Dim propTitle As String
                Dim propSubject As String
                propTitle = Range("zz_envelope_title").Value ''Property Title (= CTD Name)
                propSubject = Range("zz_envelope_OFN").Value ''Property Subject (= Output Filename)
                Application.GoTo Range("zz_envelope_header")
                Selection.COPY ''Copy the header information from sheet 'Header', the script to paste this is within the 'Template' document
                Sheets(QQ).Activate
                Word.Visible = True
            'create properties from previously created strings
                Word.Documents.Open (Range("zz_envelope_templates").Value + "\template" + DocType)
                Word.Activedocument.BuiltinDocumentProperties("Title") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_CTD").Column).Value
                Word.Activedocument.BuiltinDocumentProperties("Subject") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_OFN").Column).Value
                If Len(Cells(ActiveCell.Row, ActiveSheet.Range("zz_header_CTDnr").Column).Value) > 0 Then
                    Word.Activedocument.CustomDocumentProperties("CTDnrHeader") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_header_CTDnr").Column).Value + " "
                Else
                    Word.Activedocument.CustomDocumentProperties("CTDnrHeader") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_header_CTDnr").Column).Value
                End If
                Word.Activedocument.CustomDocumentProperties("SubjectHeader") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_header_subject").Column).Value
                Word.Activedocument.CustomDocumentProperties("TitleHeader") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_header_title").Column).Value
                Word.Activedocument.CustomDocumentProperties("SubtitleHeader") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_header_subtitle").Column).Value
                Word.Activate
                Word.Activedocument.ActiveWindow.ActivePane.VIEW.SeekView = 10 ''footer field update
                Word.Selection.WholeStory
                Word.Selection.Fields.Update
                Word.Activedocument.ActiveWindow.ActivePane.VIEW.SeekView = 9 ''header fields update
                Word.Selection.WholeStory
                Word.Selection.Fields.Update
                Word.Activedocument.ActiveWindow.ActivePane.VIEW.SeekView = 0 ''main document
                AppActivate Application.Caption
                Cells(ActiveCell.Row, ActiveSheet.Range("zz_hidden_tempID").Column).Value = Word.Activedocument.BuiltinDocumentProperties("Category")
                Word.Activate
                Word.Activedocument.SaveAs filename:=Replace(filename, "/", "_") ''Document now saved on the eDMS home location
            Else
                Word.Visible = True
                Word.Documents.Open (filename)
                Word.Activate
                AppActivate Application.Caption
                Word.Activate
                Word.Activedocument.Save
            End If
        End If
    End If
    Range("zz_preventloop").Value = ""
    Application.ScreenUpdating = True
End Sub
而是按如下方式创建和保存:

L:模板/1.0求职信\u求职信.docx

Public Sub subTemplate()

''this macro creates an empty template document with header and property information
    Cells(ActiveCell.Row, ActiveSheet.Range("zz_templates").Column).Activate
    
    Range("zz_preventloop").Value = "x"
    Application.ScreenUpdating = False
          
    Dim DocType As String
    If Range("zz_officeversion").Value = "previous to 2007" Then
    DocType = ".doc"
    Else
    DocType = ".docx"
    End If
        

'check the document type
    If Cells(ActiveCell.Row, ActiveSheet.Range("zz_doctype_template").Column).Value = ".url" Then ''Opening the .url shortcut
        On Error Resume Next
        ActiveWorkbook.FollowHyperlink Range("zz_envelope_templates").Value + "/" + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_locations_temp").Column).Value + "/" _
        + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_hidden_eDMStemp").Column).Value + ".url", NewWindow:=True
    Else
        If Cells(ActiveCell.Row, ActiveSheet.Range("zz_doctype_template").Column).Value = ".docx" Then
            Application.Calculate
            On Error Resume Next
            Set Word = GetObject(, "Word.Application")
            If Word Is Nothing Then
                Set Word = CreateObject("Word.Application")
            End If
        'to check if the template already exists:
            Dim filename As String
            filename = Range("zz_envelope_templates").Value + "/" + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_locations_temp").Column).Value + "/" _
            + ActiveSheet.Cells(ActiveCell.Row, ActiveSheet.Range("zz_hidden_eDMStemp").Column).Value + DocType
            filename = Replace(filename, "<", "[")
            filename = Replace(filename, ">", "]")
            If Dir(filename) = "" Then
            'to return to activesheet (QQ) after selection (because of activating sheet 'Header')
                Dim QQ As String
                QQ = ActiveSheet.Name
                Dim propTitle As String
                Dim propSubject As String
                propTitle = Range("zz_envelope_title").Value ''Property Title (= CTD Name)
                propSubject = Range("zz_envelope_OFN").Value ''Property Subject (= Output Filename)
                Application.GoTo Range("zz_envelope_header")
                Selection.COPY ''Copy the header information from sheet 'Header', the script to paste this is within the 'Template' document
                Sheets(QQ).Activate
                Word.Visible = True
            'create properties from previously created strings
                Word.Documents.Open (Range("zz_envelope_templates").Value + "\template" + DocType)
                Word.Activedocument.BuiltinDocumentProperties("Title") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_CTD").Column).Value
                Word.Activedocument.BuiltinDocumentProperties("Subject") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_OFN").Column).Value
                If Len(Cells(ActiveCell.Row, ActiveSheet.Range("zz_header_CTDnr").Column).Value) > 0 Then
                    Word.Activedocument.CustomDocumentProperties("CTDnrHeader") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_header_CTDnr").Column).Value + " "
                Else
                    Word.Activedocument.CustomDocumentProperties("CTDnrHeader") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_header_CTDnr").Column).Value
                End If
                Word.Activedocument.CustomDocumentProperties("SubjectHeader") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_header_subject").Column).Value
                Word.Activedocument.CustomDocumentProperties("TitleHeader") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_header_title").Column).Value
                Word.Activedocument.CustomDocumentProperties("SubtitleHeader") = Cells(ActiveCell.Row, ActiveSheet.Range("zz_header_subtitle").Column).Value
                Word.Activate
                Word.Activedocument.ActiveWindow.ActivePane.VIEW.SeekView = 10 ''footer field update
                Word.Selection.WholeStory
                Word.Selection.Fields.Update
                Word.Activedocument.ActiveWindow.ActivePane.VIEW.SeekView = 9 ''header fields update
                Word.Selection.WholeStory
                Word.Selection.Fields.Update
                Word.Activedocument.ActiveWindow.ActivePane.VIEW.SeekView = 0 ''main document
                AppActivate Application.Caption
                Cells(ActiveCell.Row, ActiveSheet.Range("zz_hidden_tempID").Column).Value = Word.Activedocument.BuiltinDocumentProperties("Category")
                Word.Activate
                Word.Activedocument.SaveAs filename:=Replace(filename, "/", "_") ''Document now saved on the eDMS home location
            Else
                Word.Visible = True
                Word.Documents.Open (filename)
                Word.Activate
                AppActivate Application.Caption
                Word.Activate
                Word.Activedocument.Save
            End If
        End If
    End If
    Range("zz_preventloop").Value = ""
    Application.ScreenUpdating = True
End Sub
Public子模板()
''此宏创建带有标题和属性信息的空模板文档
单元格(ActiveCell.Row、ActiveSheet.Range(“zz_模板”).列)。激活
范围(“zz_”).Value=“x”
Application.ScreenUpdating=False
将DocType设置为字符串
如果范围为(“zz_officeversion”).Value=“2007年之前”,则
DocType=“.doc”
其他的
DocType=“.docx”
如果结束
'检查文档类型
如果单元格(ActiveCell.Row,ActiveSheet.Range(“zz_doctype_template”).Column).Value=“.url”,则“打开.url快捷方式”
出错时继续下一步
ActiveWorkbook.FollowHyperlink范围(“zz\u信封\u模板”).Value+“/”+ActiveSheet.Cells(ActiveCell.Row,ActiveSheet.Range(“zz\u位置\u温度”).Column)。Value+“/”_
+ActiveSheet.Cells(ActiveCell.Row,ActiveSheet.Range(“zz_hidden_eDMStemp”).Column)。Value+“.url”,NewWindow:=True
其他的
如果单元格(ActiveCell.Row、ActiveSheet.Range(“zz_doctype_template”).Column.Value=“.docx”,则
应用。计算
出错时继续下一步
Set Word=GetObject(,“Word.Application”)
如果单词不算什么,那么
Set Word=CreateObject(“Word.Application”)
如果结束
'要检查模板是否已存在,请执行以下操作:
将文件名设置为字符串
filename=Range(“zz\u信封\u模板”).Value+“/”+ActiveSheet.Cells(ActiveCell.Row,ActiveSheet.Range(“zz\u位置\u温度”).Column)。Value+“/”_
+单元格(ActiveCell.Row,ActiveSheet.Range(“zz\u hidden\u eDMStemp”).Column)。值+DocType
文件名=替换(文件名“,”])
如果Dir(filename)=“”,则
'在选择后返回到activesheet(QQ)(因为激活了工作表'标题')
模糊QQ作为字符串
QQ=ActiveSheet.Name
将标题设置为字符串
将主题设置为字符串
propTitle=范围(“zz\U信封\U标题”)。值“财产标题(=CTD名称)
propSubject=范围(“zz_信封_of n”)。值“”属性主题(=输出文件名)
Application.GoTo范围(“zz_信封头”)
Selection.COPY“从工作表“页眉”复制页眉信息”,要粘贴此信息的脚本位于“模板”文档中
工作表(QQ)。激活
Word.Visible=True
'从以前创建的字符串创建属性
Word.Documents.Open(范围(“zz_信封_模板”).Value+“\template”+DocType)
Word.Activedocument.builtinocumentproperties(“Title”)=单元格(ActiveCell.Row、ActiveSheet.Range(“zz_CTD”).Column)。值
Word.Activedocument.builtinocumentproperties(“Subject”)=单元格(ActiveCell.Row、ActiveSheet.Range(“zz_”).Column)。值
如果Len(Cells(ActiveCell.Row,ActiveSheet.Range(“zz_header_CTDnr”).Column)值)大于0,则
Word.Activedocument.CustomDocumentProperties(“CTDnrHeader”)=单元格(ActiveCell.Row、ActiveSheet.Range(“zz_header_CTDnr”).列)。值+“”
其他的
Word.Activedocument.CustomDocumentProperties(“CTDnrHeader”)=单元格(ActiveCell.Row、ActiveSheet.Range(“zz\u header\u CTDnr”).列)。值
如果结束
Word.Activedocument.CustomDocumentProperties(“SubjectHeader”)=单元格(ActiveCell.Row、ActiveSheet.Range(“zz_header_subject”).列)。值
Word.Activedocument.CustomDocumentProperties(“标题标题”)=单元格(ActiveCell.Row、ActiveSheet.Range(“zz_标题”)列)。值
Word.Activedocument.CustomDocumentProperties(“SubtitleHeader”)=单元格(ActiveCell.Row、ActiveSheet.Range(“zz_header_subtitle”).列)。值
单词,激活
Word.Activedocument.ActiveWindow.ActivePane.VIEW.SeekView=10''页脚字段更新
单词、选择、健康
Word.Selection.Fields.Update
Word.Activedocument.ActiveWindow.ActivePane.VIEW.SeekView=9''标题字段更新
单词、选择、健康
Word.Selection.Fields.Update
Word.Activedocument.ActiveWindow.ActivePane.VIEW.SeekView=0''主文档
AppActivate应用程序。标题
单元格(ActiveCell.Row,ActiveSheet.Range(“zz_hidden_tempID”).Column)。Value=Word.Activedocument.BuiltinDocumentProperties(“类别”)
单词,激活
Word.Activedocument.SaveAs文件名:=替换(文件名“/”,“”)”现在保存在eDMS主位置上的文档
其他的
Word.Visible=True
Word.Documents.Open(文件名)
单词,激活
AppActivate应用程序。标题
单词,激活
Word.Activedocument.Save
如果结束
如果结束
如果结束
范围(“zz_循环”).Value=“”
Application.ScreenUpdating=True
端接头

Activedocument.SaveAs文件名:=替换(文件名“/”,“”)
生成结果