如何允许用户在文件中输入 name/path
How to allow user input in the File name/path
我们如何使用文件名或文件路径作为将使用宏的用户的输入。
他们可以在以下变量中输入文件名或文件路径。
目前文件名“fivetables.docx”是硬编码的。
Set wdDoc = .Documents.Open(ActiveWorkbook.Path & "\fivetables.docx")
完整脚本如下 -
Sub ImportWordTables()
Application.ScreenUpdating = False
Dim wdApp As New Word.Application, wdDoc As Word.Document
Dim xlWkSht As Worksheet, i As Long, j As Long, t As Long
With wdApp
.Visible = False
Set wdDoc = .Documents.Open(ActiveWorkbook.Path & "\fivetables.docx")
With wdDoc
t = .Tables.Count
i = CLng(InputBox("The document has " & t & " tables." & vbCr & _
"Table to start at?"))
If i < 1 Then GoTo ErrExit
If i > t Then GoTo ErrExit
j = CLng(InputBox("The document has " & t & " tables." & vbCr & _
"Table to end at?"))
If j > t Then j = t
For t = i To j
.Tables(t).Range.Copy
Set xlWkSht = ActiveWorkbook.Worksheets.Add
xlWkSht.PasteSpecial "HTML"
xlWkSht.Range("A1").CurrentRegion.EntireColumn.AutoFit
Next
ErrExit:
.Close False
End With
.Quit
End With
Application.ScreenUpdating = True
End Sub
使用 built-in 文件打开对话框。
参见 How to set the default suggested filename to be displayed by the Save As dialog the first time a user saves a new document, Getting help with calling Word's built-in dialogs using VBA (and why doing so can be much more useful than you'd think), and How to change the directory of the Save As dialog。
基本上,从代码中调用对话框,让用户使用对话框设置名称和路径。
The easiest and best way is to specify the path you want in the Name
argument of the Dialogs object:
With Dialogs(wdDialogFileOpen)
.Name = "c:\windows\temp\"
.Show
End With
当然,您可以使用某种方法让您的用户在变量中键入路径和名称,然后直接使用该变量。但是,对话框在那里并且是为此目的而设计的。为什么不使用它们?
If you want to have the dialog preloaded with a different file name,
then put your preferred filename there in place of
ActiveDocument.Name, e.g.:
With Dialogs(wdFileOpen)
.Name = "C:\My Documents\temp.doc"
.Show
End With
引用 material 来自 How to change the directory of the Save As dialog。改为使用“文件打开对话框”。
例如:
Sub ImportWordTables()
Application.ScreenUpdating = False
Dim StrFlNm As String
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Filters.Add "Documents", "*.doc; *.docx; *.docm", 1
If .Show = -1 Then
StrFlNm = .SelectedItems(1)
Else
Exit Sub
End If
End With
Dim wdApp As New Word.Application, wdDoc As Word.Document
Dim xlWkSht As Worksheet, i As Long, j As Long, t As Long
With wdApp
.Visible = False
Set wdDoc = .Documents.Open(FileName:=StrFlNm, AddToRecentFiles:=False)
等等
我们如何使用文件名或文件路径作为将使用宏的用户的输入。 他们可以在以下变量中输入文件名或文件路径。 目前文件名“fivetables.docx”是硬编码的。
Set wdDoc = .Documents.Open(ActiveWorkbook.Path & "\fivetables.docx")
完整脚本如下 -
Sub ImportWordTables()
Application.ScreenUpdating = False
Dim wdApp As New Word.Application, wdDoc As Word.Document
Dim xlWkSht As Worksheet, i As Long, j As Long, t As Long
With wdApp
.Visible = False
Set wdDoc = .Documents.Open(ActiveWorkbook.Path & "\fivetables.docx")
With wdDoc
t = .Tables.Count
i = CLng(InputBox("The document has " & t & " tables." & vbCr & _
"Table to start at?"))
If i < 1 Then GoTo ErrExit
If i > t Then GoTo ErrExit
j = CLng(InputBox("The document has " & t & " tables." & vbCr & _
"Table to end at?"))
If j > t Then j = t
For t = i To j
.Tables(t).Range.Copy
Set xlWkSht = ActiveWorkbook.Worksheets.Add
xlWkSht.PasteSpecial "HTML"
xlWkSht.Range("A1").CurrentRegion.EntireColumn.AutoFit
Next
ErrExit:
.Close False
End With
.Quit
End With
Application.ScreenUpdating = True
End Sub
使用 built-in 文件打开对话框。
参见 How to set the default suggested filename to be displayed by the Save As dialog the first time a user saves a new document, Getting help with calling Word's built-in dialogs using VBA (and why doing so can be much more useful than you'd think), and How to change the directory of the Save As dialog。
基本上,从代码中调用对话框,让用户使用对话框设置名称和路径。
The easiest and best way is to specify the path you want in the Name argument of the Dialogs object:
With Dialogs(wdDialogFileOpen) .Name = "c:\windows\temp\" .Show End With
当然,您可以使用某种方法让您的用户在变量中键入路径和名称,然后直接使用该变量。但是,对话框在那里并且是为此目的而设计的。为什么不使用它们?
If you want to have the dialog preloaded with a different file name, then put your preferred filename there in place of ActiveDocument.Name, e.g.:
With Dialogs(wdFileOpen) .Name = "C:\My Documents\temp.doc" .Show End With
引用 material 来自 How to change the directory of the Save As dialog。改为使用“文件打开对话框”。
例如:
Sub ImportWordTables()
Application.ScreenUpdating = False
Dim StrFlNm As String
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Filters.Add "Documents", "*.doc; *.docx; *.docm", 1
If .Show = -1 Then
StrFlNm = .SelectedItems(1)
Else
Exit Sub
End If
End With
Dim wdApp As New Word.Application, wdDoc As Word.Document
Dim xlWkSht As Worksheet, i As Long, j As Long, t As Long
With wdApp
.Visible = False
Set wdDoc = .Documents.Open(FileName:=StrFlNm, AddToRecentFiles:=False)
等等