Sub FnWriteToWordDoc()
'
'Opens desired template to fill in data form range of cells
'Dim wApp As Word.Application
Set wApp = CreateObject("Word.Application")
wApp.DisplayAlerts = False
'Opens template to create document
Documents.Add Template:="M:\file.dotm"
'Below Tells to keep values in memory
Dim currentDay As String
Dim currentMonth As String
Dim currentYear As String
currentDay = Format(Now(), "dd")
currentMonth = Format(Now(), "mmm")
currentYear = Format(Now(), "yyyy")
Set wdApp = GetObject("Word.Application")
If Err.Number = 429 Then
Err.Clear
Set wdApp = CreateObject("Word.Application")
End If
strDocName = "C:\Document\sample.docx"
Dim cc As wdApp.ContentControl ' Error Here
For Each cc In ActiveDocument.SelectContentControlsByTag("monthName")
cc.Title = currentMonth
Next
我收到错误说
Compile error:
User-defined type not defined
该函数位于名为“Microsoft Excel Objects”的文件夹下的“Sheet1”中。我在“Modules1”中使用了相同的代码并移动了。
我正在尝试让Excel插入到我的Word文档内容控件中。
答案 0 :(得分:1)
这里有错误
Dim cc As wdApp.ContentControl
因为您需要启用Microsoft Word 12库。转到“工具”>“参考”,找到Microsoft Word库,并在其旁边放置一个复选标记
答案 1 :(得分:0)
Sub FnWriteToWordDoc()
'
'Opens desired template to fill in data form range of cells
Dim wApp As Object
Set wApp = CreateObject("Word.Application")
Dim wDoc As Object
Set wDoc = CreateObject("Word.Document")
On Error Resume Next
Set wApp = GetObject(, "Word.Application")
If wApp Is Nothing Then
Set wApp = CreateObject("Word.Application")
End If
On Error GoTo 0
wApp.DisplayAlerts = False
'Opens template to create document
Set wDoc = wApp.Documents.Add(Template:="M:\file.dotm")
'Below Tells to keep values in memory
Dim currentDay As String
Dim currentMonth As String
Dim currentYear As String
currentDay = Format(Now(), "dd")
currentMonth = Format(Now(), "mmm")
currentYear = Format(Now(), "yyyy")
strDocName = "C:\Document\sample.docx"
' you must declare your variable with class' name, not instanced object's name
Dim cc As Object
For Each cc In wDoc.SelectContentControlsByTag("monthName")
cc.Range.Text = currentMonth
Next
上面的代码是功能代码,可以写入ContentControls。感谢VBobCat的灵感
答案 2 :(得分:-1)
好的,我相信这可行:
Sub FnWriteToWordDoc()
'
'Opens desired template to fill in data form range of cells
Dim wApp As Word.Application 'declare empty variable
Dim wDoc As Word.Document 'declare empty variable
On Error Resume Next
Set wApp = GetObject(, "Word.Application") ' get instance
If wApp Is Nothing Then 'getting existing instance failed
Set wApp = CreateObject("Word.Application") 'create new instance
End If
On Error GoTo 0
wApp.DisplayAlerts = False
'Opens template to create document
Set wDoc = wApp.Documents.Add(Template:="M:\file.dotm") 'get instance of document
'Below Tells to keep values in memory
Dim currentDay As String
Dim currentMonth As String
Dim currentYear As String
currentDay = Format(Now(), "dd")
currentMonth = Format(Now(), "mmm")
currentYear = Format(Now(), "yyyy")
strDocName = "C:\Document\sample.docx"
' you must declare your variable with class' name, not instanced object's name
Dim cc As Word.ContentControl
For Each cc In wDoc.SelectContentControlsByTag("monthName")
cc.Title = currentMonth
Next
wDoc.SaveAs2 strDocName
wDoc.Close False
If wApp.Documents.Count = 0 Then wApp.Quit False
End Sub