Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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 使用宏拆分word文档,如何指定名称?_Excel_Vba_Word 2010 - Fatal编程技术网

Excel 使用宏拆分word文档,如何指定名称?

Excel 使用宏拆分word文档,如何指定名称?,excel,vba,word-2010,Excel,Vba,Word 2010,我试着在这里寻找这个,但看不到答案,所以我想我会问,如果这是一个重复道歉 我正在使用邮件合并来填充Word 2010中的表格。这就创建了同一个表的数百个页面,但每个页面中包含不同的数据。然后,我使用宏将word文档拆分为每页单独的文档。我对宏的了解非常有限,但我已经让它工作了,我知道如果我只想要一个标准编号的东西,如何修改名称,但我想要的是它使用表中的一个字段作为名称,所以它们都将其标识号作为文件名 这是我得到的宏代码: Sub BreakOnPage() ' Used to set cr

我试着在这里寻找这个,但看不到答案,所以我想我会问,如果这是一个重复道歉

我正在使用邮件合并来填充Word 2010中的表格。这就创建了同一个表的数百个页面,但每个页面中包含不同的数据。然后,我使用宏将word文档拆分为每页单独的文档。我对宏的了解非常有限,但我已经让它工作了,我知道如果我只想要一个标准编号的东西,如何修改名称,但我想要的是它使用表中的一个字段作为名称,所以它们都将其标识号作为文件名

这是我得到的宏代码:

Sub BreakOnPage()
   ' Used to set criteria for moving through the document by page.
   Application.Browser.Target = wdBrowsePage

   For i = 1 To ActiveDocument.BuiltInDocumentProperties("Number of Pages")

      'Select and copy the text to the clipboard.
      ActiveDocument.Bookmarks("\page").Range.Copy

      ' Open new document to paste the content of the clipboard into.
      Documents.Add
      Selection.Paste
' Removes the break that is copied at the end of the page, if any.
      Selection.TypeBackspace
      ChangeFileOpenDirectory "F:\MyWork"
      DocNum = DocNum + 1
      ActiveDocument.SaveAs FileName:="test_" & DocNum & ".doc"
      ActiveDocument.Close

      ' Move the selection to the next page in the document.
      Application.Browser.Next
   Next i
   ActiveDocument.Close savechanges:=wdDoNotSaveChanges
End Sub
但我不知道如何让它在文档中拾取字段。任何帮助都将不胜感激


此外,当将其拆分为单个文档时,它似乎会在其底部添加一个空白页,因此,当我打开其中一个文件时,我的表中有一个空白页,但我也不知道如何消除该空白页,因此有关该空白页的提示也会有很大帮助。

您没有指定需要哪个单元格,但这应该让你开始:

Dim sCellText As String
sCellText = ActiveDocument.Tables(1).Rows(1).Cells(1).Range
sCellText = Left$(sCellText, Len(sCellText) - 2)

ActiveDocument.SaveAs FileName:="sCellText & ".doc"

这会将文档保存为第一行第一个单元格的值

至于拆分文档时添加的空白页,请参阅。