1

我一直在将 rtf(富文本)形式的 access 2010 中的备忘录字段导出到带有书签的 word 文件中。问题是需要两次点击才能打开word文档,然后,文本被插入了两次。我仍然无法找到问题所在。

这是代码:

Option Compare Database

Private Sub Comando72_Click()
'Saves the current record -->
Me.Dirty = False
Dim appWord As Word.Application
Dim doc As Word.Document
Dim objWord As Object  '' Word.Application
Dim fso As Object  '' FileSystemObject
Dim f As Object  '' TextStream
Dim myHtml As String
Dim tempFileSpec As String

' grab some formatted text from a Memo field
myHtml = DLookup("DescripActivAEjecutarse", "PlanificacionServiciosInstitucionales", "IdPSI = Form!IdPSI")
Set fso = CreateObject("Scripting.FileSystemObject")  '' New FileSystemObject
tempFileSpec = fso.GetSpecialFolder(2) & "\" & fso.GetTempName & ".htm"
'' write to temporary .htm file
Set f = fso.CreateTextFile(tempFileSpec, True)
f.Write "<html>" & myHtml & "</html>"
f.Close
Set f = Nothing
Set fso = Nothing
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
    'If Word isn't open, create a new instance of Word.
    Set appWord = New Word.Application
End If
'set the doc for future use.
Set doc = appWord.Documents.Open("C:\Users\earias\Documents\SOLICITUD-Yachay-automatica2.docx", , True) 'True default (just reading).
'locates bookmark and inserts file
appWord.Selection.GoTo what:=wdGoToBookmark, Name:="bookmark_1"
appWord.Selection.InsertFile tempFileSpec

Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
    MsgBox Err.Number & ": " & Err.Description
End Sub
4

1 回答 1

0

如果您按两次按钮,它将运行该程序两次?

就您当前的代码而言,在此行之后Set doc = appWord.Documents.Open添加以下内容;

doc.visible = true 

这应该使您能够查看按一次按钮时打开的文档。为了防止弹出窗口,您也可以不将其设置为可见 do;

doc.saveas "path here"

然后将所有设置为空并按照您的意愿关闭,文件将保存在您希望保存的位置,而无需每次手动保存。

您可以查看使用模板设置简单的邮件合并,然后将模板保存为您选择的任何格式并断开邮件合并链接(我的首选方法)。

让我知道你是怎么办的!

于 2015-04-07T19:37:34.020 回答