2016-10-04 161 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"