使用 Access VBA 将数据(图像)插入 Word 文件
Insert data (image) into Word file using Access VBA
我从一个类似问题的答案中偷走了格式:
我遇到的问题是它一直没有实际设置变量。
Public Sub SignDoc(fileName As String, filetype As String)
FileCopy "\SERVER01\InventoryObjects\" & filetype & ".docx", _
"\SERVER01\SignatureCaptures\" & fileName & " .docx"
Dim Word As Word.Application
Dim doc As Word.Document
Dim filePath As String: filePath = "\SERVER01\SignatureCaptures\" & _
fileName & ".docx"
Dim SHP As Word.Document
Dim strTmp As String: strTmp = "SignatureBM" 'bookmark in appropriate file
Dim strPath As String: strPath = "\SERVER01\SignatureCaptures\" & fileName & ".gif"
Set Word = CreateObject("Word.Application")
Set doc = Word.Documents.Open(filePath)
Set SHP = doc.Bookmarks(strTmp).Range.InlineShapes.AddPicture(fileName:=strPath, _
LinkToFile:=False, SaveWithDocument:=True)
With SHP
'this will keep ratio
' .WrapFormat.type = 1 'wdWrapTight
' .WrapFormat.type = 7 'wdWrapInline
.LockAspectRatio = -1 ' msoTrue
'this will adjust width to 0.5 inch
'.Width = wd.InchesToPoints(2.5)
' .Width = wd.CentimetersToPoints(2.66) * 2.5
' .Height = wd.CentimetersToPoints(3.27) * 2.5
' .ScaleHeight = 150
End With
End Sub
代码中断于:
Set doc = Word.Documents.Open(filePath)
虽然它在 Set SHP 中突出显示,但实际上从未将“doc”设置为任何东西。
检查立即 window 中的变量表明该行实际上并不成功,并且 returns 运行时错误 91 ...
任何建议都会有所帮助。
经过大量研究和反复试验后,我得出的结论是,这是一个“你根本做不到”我想做的事情的案例。
解决方案是将文件移动到本地驱动器,在那里执行图像插入,然后将文件保存到需要的服务器位置
如以下代码所示:
Public Sub SignDoc(fileName As String, filetype As String)
FileCopy "\SERVER01\InventoryObjects\" & filetype & ".docx",
"C:\users\" & GetUserName() & "\documents\" & fileName & ".docx"
Dim oWord As Word.Application
Dim doc As Word.Document
Dim filePath As String: filePath = "C:\users\" & GetUserName() & "\documents\" & fileName & ".docx"
Dim SHP As Object
Dim strTmp As String: strTmp = "SignatureBM" 'bookmark in appropriate file
Dim strPath As String: strPath = "C:\users\" & GetUserName() & "\documents\" & fileName & ".gif"
Dim oSel As Object
Set oWord = CreateObject("Word.Application")
Set doc = oWord.Documents.Open(filePath)
Set SHP = doc.Bookmarks(strTmp).Range.InlineShapes.AddPicture(fileName:=strPath, LinkToFile:=False, SaveWithDocument:=True)
doc.SaveAs ("\SERVER01\SignatureCaptures\" & fileName & ".docx")
With SHP
'this will keep ratio
' .WrapFormat.type = 1 'wdWrapTight
' .WrapFormat.type = 7 'wdWrapInline
.LockAspectRatio = -1 ' msoTrue
'this will adjust width to 0.5 inch
'.Width = wd.InchesToPoints(2.5)
' .Width = wd.CentimetersToPoints(2.66) * 2.5
' .Height = wd.CentimetersToPoints(3.27) * 2.5
.ScaleHeight = 50
.ScaleWidth = 50
End With
doc.Close
End Sub
对于那些好奇的人,GetUserName 是一个函数,可以提取登录到计算机的任何人的姓名:它的代码很简单,如下所示:
Public Function GetUserName() As String
GetUserName = Environ("USERNAME")
End Function
通过移动操作位置,所有错误都消失了。不知道为什么在本地机器上操作很重要,而复制功能等其他操作在网络上工作得很好。
我从一个类似问题的答案中偷走了格式:
我遇到的问题是它一直没有实际设置变量。
Public Sub SignDoc(fileName As String, filetype As String)
FileCopy "\SERVER01\InventoryObjects\" & filetype & ".docx", _
"\SERVER01\SignatureCaptures\" & fileName & " .docx"
Dim Word As Word.Application
Dim doc As Word.Document
Dim filePath As String: filePath = "\SERVER01\SignatureCaptures\" & _
fileName & ".docx"
Dim SHP As Word.Document
Dim strTmp As String: strTmp = "SignatureBM" 'bookmark in appropriate file
Dim strPath As String: strPath = "\SERVER01\SignatureCaptures\" & fileName & ".gif"
Set Word = CreateObject("Word.Application")
Set doc = Word.Documents.Open(filePath)
Set SHP = doc.Bookmarks(strTmp).Range.InlineShapes.AddPicture(fileName:=strPath, _
LinkToFile:=False, SaveWithDocument:=True)
With SHP
'this will keep ratio
' .WrapFormat.type = 1 'wdWrapTight
' .WrapFormat.type = 7 'wdWrapInline
.LockAspectRatio = -1 ' msoTrue
'this will adjust width to 0.5 inch
'.Width = wd.InchesToPoints(2.5)
' .Width = wd.CentimetersToPoints(2.66) * 2.5
' .Height = wd.CentimetersToPoints(3.27) * 2.5
' .ScaleHeight = 150
End With
End Sub
代码中断于:
Set doc = Word.Documents.Open(filePath)
虽然它在 Set SHP 中突出显示,但实际上从未将“doc”设置为任何东西。 检查立即 window 中的变量表明该行实际上并不成功,并且 returns 运行时错误 91 ...
任何建议都会有所帮助。
经过大量研究和反复试验后,我得出的结论是,这是一个“你根本做不到”我想做的事情的案例。
解决方案是将文件移动到本地驱动器,在那里执行图像插入,然后将文件保存到需要的服务器位置
如以下代码所示:
Public Sub SignDoc(fileName As String, filetype As String)
FileCopy "\SERVER01\InventoryObjects\" & filetype & ".docx",
"C:\users\" & GetUserName() & "\documents\" & fileName & ".docx"
Dim oWord As Word.Application
Dim doc As Word.Document
Dim filePath As String: filePath = "C:\users\" & GetUserName() & "\documents\" & fileName & ".docx"
Dim SHP As Object
Dim strTmp As String: strTmp = "SignatureBM" 'bookmark in appropriate file
Dim strPath As String: strPath = "C:\users\" & GetUserName() & "\documents\" & fileName & ".gif"
Dim oSel As Object
Set oWord = CreateObject("Word.Application")
Set doc = oWord.Documents.Open(filePath)
Set SHP = doc.Bookmarks(strTmp).Range.InlineShapes.AddPicture(fileName:=strPath, LinkToFile:=False, SaveWithDocument:=True)
doc.SaveAs ("\SERVER01\SignatureCaptures\" & fileName & ".docx")
With SHP
'this will keep ratio
' .WrapFormat.type = 1 'wdWrapTight
' .WrapFormat.type = 7 'wdWrapInline
.LockAspectRatio = -1 ' msoTrue
'this will adjust width to 0.5 inch
'.Width = wd.InchesToPoints(2.5)
' .Width = wd.CentimetersToPoints(2.66) * 2.5
' .Height = wd.CentimetersToPoints(3.27) * 2.5
.ScaleHeight = 50
.ScaleWidth = 50
End With
doc.Close
End Sub
对于那些好奇的人,GetUserName 是一个函数,可以提取登录到计算机的任何人的姓名:它的代码很简单,如下所示:
Public Function GetUserName() As String
GetUserName = Environ("USERNAME")
End Function
通过移动操作位置,所有错误都消失了。不知道为什么在本地机器上操作很重要,而复制功能等其他操作在网络上工作得很好。