将文件存储在VBA项目中并打开它

时间:2016-05-23 13:59:59

标签: vba excel-vba excel

我在MS Excel 2010中创建了一个VBA应用程序。它有一个用户表单。在那里,我想添加一个功能来打开(MS Word)文件以获得支持和常见问题解答。我不想将文件保存在中央位置,然后通过VBA打开文件。是否有可能将文件存储在vba项目中?

2 个答案:

答案 0 :(得分:3)

您可以在Excel Worskeet中嵌入对象(Insert - > Object)。如果单击嵌入对象,则会在左上角看到对象的名称(例如“对象7”)。有了它,您可以通过

在vba中打开它
Sub openEmbed()
  Dim ole As OLEObject, wdoc As Word.Document
  Set ole = Worksheets("Sheet1").OLEObjects("Object 7")
  ole.Activate
  Set wdoc = ole.Object
End Sub

答案 1 :(得分:2)

您可以将内容作为XML存储在VBA中,然后将其与InsertXML一起插入新文档中:

Dim app As Object
Set app = CreateObject("Word.Application")

app.Visible = True

app.Documents.Add.Content.InsertXML "<?xml version=""1.0""?><abc:books xmlns:abc=""urn:books"" " & _
  "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " & _
  "xsi:schemaLocation=""urn:books books.xsd""><book>" & _
  "<author>Matt Hink</author><title>Migration Paths of the Red " & _
  "Breasted Robin</title><genre>non-fiction</genre>" & _
  "<price>29.95</price><pub_date>2006-05-01</pub_date>" & _
  "<abstract>You see them in the spring outside your windows.  " & _
  "You hear their lovely songs wafting in the warm spring air.  " & _
  "Now follow their path as they migrate to warmer climes in the fall, " & _
  "and then back to your back yard in the spring.</abstract></book></abc:books>"