2012-05-01 41 views
6

Trong mã bên dưới tên tệp được mã hóa cứng, nhưng tôi muốn người dùng có thể chọn nó.lưu dưới dạng tên tệp trong từ

Tôi đã đọc khoảng GetSaveAsFilename nhưng tôi gặp lỗi khi sử dụng: "phương pháp hoặc thành viên không tìm thấy".

fileSaveName = Application.GetSaveAsFilename _ 
    (fileFilter:="Excel Files (*.txt), *.txt") 

này được viết cho Word 2010. Tôi có sai lầm trong suy nghĩ GetSaveAsFilename có sẵn trong từ VBA?

Sub Macro3() 
' 
' Macro3 Macro 
' 
' 
    ActiveDocument.SaveAs2 FileName:="Questionnaire01-05-20122.txt", _ 
     FileFormat:=wdFormatText, LockComments:=False, Password:="", _ 
     AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, _ 
     EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData _ 
     :=True, SaveAsAOCELetter:=False, Encoding:=1252, InsertLineBreaks:=False, _ 
     AllowSubstitutions:=False, LineEnding:=wdCRLF, CompatibilityMode:=0 
End Sub 

Trả lời

4

Bạn có thể cung cấp một đường dẫn mặc định bao gồm tên tập tin như vậy vào hộp thoại, ví dụ:

Sub SaveName() 
    Dim strFileName As String 
    Dim StrPath As String 
    'provide default filename 
    StrPath = "c:\temp\test.docx" 
    With Dialogs(wdDialogFileSaveAs) 
     .Name = StrPath 
     If .Display <> 0 Then 
      strFileName = .Name 
     Else 
      strFileName = "User Cancelled" 
     End If 
    End With 
    MsgBox strFileName 
End Sub 
+1

+ 1 Khởi tạo đường dẫn là một ý tưởng hay :) –

+1

một thứ chỉ trả về tên không phải là đường dẫn đầy đủ? nhưng chắc chắn tôi có thể làm việc đó ra :) cổ vũ cho những ví dụ mọi người – DevilWAH

4

Tôi không nhận ra rằng Word không có phương thức GetSaveAsFileName hoặc GetOpenFileName (Excel có). Nhưng nó không. Thay vào đó bạn có thể thử các SaveAs FileDialog (2003, 2007, 2010):

Sub ShowSaveAsDialog() 
Dim dlgSaveAs As FileDialog 
Set dlgSaveAs = Application.FileDialog(FileDialogType:=msoFileDialogSaveAs) 
dlgSaveAs.Show 
End Sub 
+0

+1 Xem [ ví dụ này đã được thực hiện] (http://stackoverflow.com/questions/5148173/getsaveasfilename-default-folder/5165166#5165166) về cách sử dụng 'FileDialog'. –

+0

Doug, tôi đã thêm một câu trả lời hơi khác vì tôi không nghĩ rằng đoạn mã trên đã làm rõ cách truy xuất mục nhập của người dùng (tôi nghĩ nó cần một cái gì đó như 'dlgSaveAs.SelectedItems (1)') – brettdj

+0

yep Brettdj nói điều này không trả lại tên tệp – DevilWAH

0
Dim strFilePath, strFileName 
strFilePath = "C:\Users\Public\Documents\" 
strFileName = "put-filename-here.docx" 

With Dialogs(wdDialogFileSaveAs) 
    .Name = strFilePath & strFileName 
    .Show 
End With 
Các vấn đề liên quan