点击两次从访问表单生成word文档,使用vba复制双富文本



我一直致力于将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:UserseariasDocumentsSOLICITUD-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

如果按两次按钮,它会运行两次过程吗?

就您当前的代码而言,在该行CCD_ 1之后添加以下内容;

doc.visible = true 

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

doc.saveas "path here"

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

你可以考虑用模板设置一个简单的邮件合并,然后将其保存为你选择的任何格式的模板,并断开邮件合并链接(我喜欢的方法)。

让我知道你过得怎么样!

最新更新