我從一個類似問題的答案中竊取了格式:Insert Image into Word by using 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”設定為任何內容。檢查即時視窗中的變數表明該行實際上并不成功,并回傳運行時錯誤 91 ...
任何的意見都將會有幫助。
uj5u.com熱心網友回復:
經過大量的研究和反復試驗,我得出的結論是,這是一個“你根本做不到”我想做的事情。
解決方案是將檔案移動到本地驅動器,在那里執行影像插入,然后將檔案保存到需要的服務器位置
如下面的代碼所示:
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
通過移動操作的位置,所有錯誤都消失了。不知道為什么操作在本地機器上很重要,而其他操作(例如復制功能)在網路上運行良好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/468362.html
