使用单元格值作为文件名保存 Word 文件
Save a Word file using Cell Value as Filename
我有一个 word 文档,可以让我们合并在线数据库中的信息。我正在尝试创建一个宏,它将获取特定 table 单元格中的值,并使用它来将文件保存在特定目录中。
我不精通 VBA,但尝试通过从其他资源(例如此处)复制代码来弄乱其他人所做的事情。
我找到了允许我 select 正确单元格的代码,我能够让它工作。
我找到了其他代码来保存该文件,我尝试修改它以将单元格值作为文件名包含在内。
Sub SaveAsCellContent()
Dim Invoice As String
Dim directory As String
Invoice = ActiveDocument.Tables(1).Cell(2, 5)
directory = "D:\Dropbox (DRYBSMT)\~ DB Forms\Word Saves\"
wordApp.DisplayAlerts = False
WordDoc.SaveAs FileName = DIR & INV & ".docx", FileFormat:=wdFormatDocumentDefault
WordDoc.Close
wordApp.Quit
Set WordDoc = Nothing
Set wordApp = Nothing
End Sub
正如所写,我收到错误 “编译错误:类型不匹配”。
我花了好几个小时试图解决这个问题,我认为是时候寻求专家的帮助了。
我现在拥有的,并且正在运行:
Sub SaveAsCellContent()
Dim Invoice As String
Dim Directory As String
Invoice = ActiveDocument.Tables(1).Cell(2, 5).Range.Text
Invoice = Left(Invoice, Len(Invoice) - 2)
Directory = "D:\Dropbox (DRYBSMT)\~ DB Forms\Word Saves\"
ActiveDocument.SaveAs Filename:=Directory & Invoice & "i.docx", fileformat:=wdFormatXMLDocument
End Sub
但我被告知我需要添加 Debug.Print。这就是我现在所在的位置。
我在下面的工作代码示例中包含了以上所有注释,包括添加示例错误处理程序和 debug.print 语句,该语句将构建目录 + 文件名打印到即时(调试)window .
Sub SaveAsCellContent()
Dim Invoice As String
Dim directory As String
' Get the filename that is in the 1st table in the document
Invoice = ActiveDocument.Tables(1).Cell(1, 1).Range.Text
' so the Invoice string is always at least 2 characters long
' because a cell's value contains the cell boundary characters
' remove the cell boundary characters as they cannot be used for the file name
Invoice = Left(Invoice, Len(Invoice) - 2)
' no file name specified
If Len(Invoice) = 0 Then
Invoice = "default filename"
End If
' build the directory + filename to use in the saveas
directory = "D:\Dropbox (DRYBSMT)\~ DB Forms\Word Saves\"
' adding the file extension .docx is not needed
' is determined by the FileFormat parametes
Dim fileNm As Variant
fileNm = directory & Invoice
' print to the Immediate window for testing purposes
Debug.Print ("File name: " & fileNm)
On Error GoTo saveError
ActiveDocument.SaveAs FileName:=fileNm,
FileFormat:=WdSaveFormat.wdFormatDocumentDefault
saveError:
If Err.Number > 0 Then
MsgBox ("Some error occurred:" & vbCrLf & Err.Number)
End If
End Sub
我有一个 word 文档,可以让我们合并在线数据库中的信息。我正在尝试创建一个宏,它将获取特定 table 单元格中的值,并使用它来将文件保存在特定目录中。
我不精通 VBA,但尝试通过从其他资源(例如此处)复制代码来弄乱其他人所做的事情。
我找到了允许我 select 正确单元格的代码,我能够让它工作。
我找到了其他代码来保存该文件,我尝试修改它以将单元格值作为文件名包含在内。
Sub SaveAsCellContent()
Dim Invoice As String
Dim directory As String
Invoice = ActiveDocument.Tables(1).Cell(2, 5)
directory = "D:\Dropbox (DRYBSMT)\~ DB Forms\Word Saves\"
wordApp.DisplayAlerts = False
WordDoc.SaveAs FileName = DIR & INV & ".docx", FileFormat:=wdFormatDocumentDefault
WordDoc.Close
wordApp.Quit
Set WordDoc = Nothing
Set wordApp = Nothing
End Sub
正如所写,我收到错误 “编译错误:类型不匹配”。
我花了好几个小时试图解决这个问题,我认为是时候寻求专家的帮助了。
我现在拥有的,并且正在运行:
Sub SaveAsCellContent()
Dim Invoice As String
Dim Directory As String
Invoice = ActiveDocument.Tables(1).Cell(2, 5).Range.Text
Invoice = Left(Invoice, Len(Invoice) - 2)
Directory = "D:\Dropbox (DRYBSMT)\~ DB Forms\Word Saves\"
ActiveDocument.SaveAs Filename:=Directory & Invoice & "i.docx", fileformat:=wdFormatXMLDocument
End Sub
但我被告知我需要添加 Debug.Print。这就是我现在所在的位置。
我在下面的工作代码示例中包含了以上所有注释,包括添加示例错误处理程序和 debug.print 语句,该语句将构建目录 + 文件名打印到即时(调试)window .
Sub SaveAsCellContent()
Dim Invoice As String
Dim directory As String
' Get the filename that is in the 1st table in the document
Invoice = ActiveDocument.Tables(1).Cell(1, 1).Range.Text
' so the Invoice string is always at least 2 characters long
' because a cell's value contains the cell boundary characters
' remove the cell boundary characters as they cannot be used for the file name
Invoice = Left(Invoice, Len(Invoice) - 2)
' no file name specified
If Len(Invoice) = 0 Then
Invoice = "default filename"
End If
' build the directory + filename to use in the saveas
directory = "D:\Dropbox (DRYBSMT)\~ DB Forms\Word Saves\"
' adding the file extension .docx is not needed
' is determined by the FileFormat parametes
Dim fileNm As Variant
fileNm = directory & Invoice
' print to the Immediate window for testing purposes
Debug.Print ("File name: " & fileNm)
On Error GoTo saveError
ActiveDocument.SaveAs FileName:=fileNm,
FileFormat:=WdSaveFormat.wdFormatDocumentDefault
saveError:
If Err.Number > 0 Then
MsgBox ("Some error occurred:" & vbCrLf & Err.Number)
End If
End Sub