請有人能幫助我。我得到了錯誤91,但不知道如何修復我的代碼。我是VBA的新手
Sub createQuote()
'宣告變數。
Dim wd As Word.Application
Dim wDoc As Word.Document
Dim iRow As Long 'Variable to hold starting row As 'Variable to hold starting rowDim wsheet As 作業表 '變數用于參考哪個作業表。
'Start word and new document'。
Set wd = New Word.Application
'Set worksheet where table is available[/span]。
Set wsheet = ThisWorkbook.Sheets("Merge Items")
iRow = 2 '資料從第2行開始。
'打開word模板
Set wDoc = wd.Documents.Open(ThisWorkbook.Path & "C:UsersUserDesktopProwess DocumentsProwess Electrical Solutions LtdAutomation Test DocumentsQuote Document Template_Bookmarks.docx" )
wd.Visible = False
'Code to insert values from specific bookmark in word application'。
'報價編號。
wd.Selection.GoTo what:=wdGoToBookmark, Name:="QuoteNumber"。
wd.Selection.TypeText Text:=wsheet.Range("A"/span> & iRow).Value
它停在wd.Selectioin.GoTo what:=wdGoToBookmark, Name:="Quote Number"
希望得到任何幫助
uj5u.com熱心網友回復:試試這個方法,它對我有用,你需要有一個書簽,在選定的文本中分離,它將被改變。這不僅僅是單詞開頭的書簽,它必須在這個書簽中至少有一個被選中的字符,祝你好運。
Option Explicit
Private Function CreateQuote()
Dim wdApp As Word.Application
Set wdApp = New Word.Application
Dim wsheet As Worksheet
Dim iRow As Byte
'另外,我們可以使用后期系結(評論第一行和第二行)
'Dim wdApp As Object'。
'Set wdApp = CreateObject("word.Application")
Set wsheet = ThisWorkbook.Sheets("合并專案")
iRow =2
With wdApp
.Visible = True With wdApp.
.Documents.Open "C:UsersUserDesktopProwess DocumentsProwess Electrical Solutions LtdAutomation Test DocumentsQuote Document Template_Bookmarks.docx"
With .Selection
.Goto what:=wdGoToBookmark, Name:="QuoteNumber"/span>
.TypeText wsheet.Range("A"/span> & iRow).Value
結束 與
結束 有
wdApp.Quit True 'Close and Save
End Function
uj5u.com熱心網友回復:
將Excel單元格的值應用于Word書簽
Option Explicit
Sub CreateQuote()
Const wdFilePath As String = "C:UsersUserDesktopProwess DocumentsProwess Electrical Solutions LtdAutomation Test DocumentsQuote Document Template_Bookmarks.docx"
If Len(Dir(wdFilePath)) = 0 Then
MsgBox "檔案'" & wdFilePath & "' 不存在。", _
vbCritical, "CreateQuote"。
Exit Sub
End If
'宣告變數。
Dim wd As Word.Application
Dim wDoc As Word.Document
Dim iRow As Long 'Variable to hold starting row 'Variable to hold starting rowDim wsheet As 作業表 '變數用于參考哪個作業表。
'設定可用的作業表。
設定 wsheet = ThisWorkbook.Sheets("合并專案")
iRow = 2 '資料從第2行開始。
'Start Word.
Set wd = New Word.Application.
'wd.Visible = True ' 默認值是'False'。
On Error GoTo AppError ' 如果出錯就關閉word。
'打開word模板'。
Set wDoc = wd.Documents.Open(wdFilePath)
'在word應用程式中插入特定書簽的值的代碼
'報價編號 '報價編號
wd.Selection.Goto what:=wdGoToBookmark, Name:="QuoteNumber"。
wd.Selection.TypeText Text:=wsheet.Range("A"/span> & iRow).Value
'一些更多的代碼?。
' 你必須決定在這里做什么:。
'保存并關閉檔案。'保存并關閉檔案。
'wDoc.Close SaveChanges:=True
'如果是一個模板,你寧可保存為另一個檔案:
'wdoc.saveas2(...)
SafeExit:
'關閉Word.
wd.Quit
Exit Sub
AppError:
Debug.Print "運行時錯誤'" & Err.Number & "'。" & Err.Description
恢復 SafeExit
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/323584.html
標籤:
