VBA - 选择保存文件的目标文件夹

时间:2016-10-04 16:57:23

标签: vba word-vba

我是一个完整的VBA菜鸟,需要一些帮助。如果重要的是我在Mac上使用Microsoft Word 2016。我使用邮件合并来创建单词doc,并且需要根据分节符将生成的单词doc分成多个页面。我找到了带有VBA代码的this页面来分割页面并且效果很好。我遇到的唯一问题是它在我的计算机上随机存放(我不知道它是如何决定保存的)。这是我正在使用的代码:

Sub BreakOnSection()     
   ' Used to set criteria for moving through the document by section.     
   Application.Browser.Target = wdBrowseSection     

   'A mail merge document ends with a section break next page.     
   'Subtracting one from the section count stop error message.     
   For i = 1 To ((ActiveDocument.Sections.Count) - 1)     

'Note: If a document does not end with a section break,     
'substitute the following line of code for the one above:     
'For I = 1 To ActiveDocument.Sections.Count     

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

      'Create a new document to paste text from clipboard.     
      Documents.Add     
      Selection.Paste     

   ' Removes the break that is copied at the end of the section, if any.     
      Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend     
      Selection.Delete Unit:=wdCharacter, Count:=1     
ChangeFileOpenDirectory "C:\"     
      DocNum = DocNum + 1     
     ActiveDocument.SaveAs fileName:="test_" & DocNum & ".doc"     
     ActiveDocument.Close     
      ' Move the selection to the next section in the document.     
     Application.Browser.Next     
   Next i     
   ActiveDocument.Close savechanges:=wdDoNotSaveChanges     
End Sub

我看到ChangeFileOpenDirectory设置为" C:\"对于Mac来说这不是正确的,但是我需要更改它才能让我在哪里保存所有生成的文档?我不想为每个单独的文档选择一个文件夹,而是为要保存的所有文档选择一个文件夹并让它运行。 在此先感谢您的帮助,我已经尝试了几个小时的谷歌,我仍然不确定这个。

1 个答案:

答案 0 :(得分:0)

我没有在Mac上测试过这个,但它应该是

Dim mySaveLocation As String
Dim dlg As FileDialog

Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
dlg.AllowMultiSelect = False
dlg.Show
mySaveLocation = dlg.SelectedItems.Item(1)

然后保存时

ActiveDocument.SaveAs fileName:= mySaveLocation  & "\test_" & DocNum & ".doc"