2016-10-04 84 views
1

我是一个总VBA noob,需要一点帮助。如果有关系我在Mac上使用Microsoft Word 2016。我正在使用邮件合并来创建Word文档,并且需要根据分节符将结果文档文档分成多个页面。我发现用VBA代码分页的this页面,它工作得很好。我遇到的唯一问题是它保存在我的电脑上随机的地方(我不知道它是如何决定在哪里保存的)。这里是我正在使用的代码:VBA - 选择保存文件的目标文件夹

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,但我需要改变有它问我在哪里保存所有生成的文档?我不想为每个文档选择一个文件夹,而是为所有文档选择一个文件夹保存并让它运行。 在此先感谢您的帮助,我试了几个小时的谷歌,我仍然不确定这一个。

回答

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"