使用VBA宏创建Word文档,然后在Word文档中运行宏...。

大塔林

我根据使用宏输入到Excel电子表格中的数据创建销售建议,然后调用宏以根据输入到电子表格中的数据导入一些“库存”图片。第二个宏保存在normal.dot文档中,并通过以下代码调用:

WordObj.Run(“ normal!Picture”)',这将调用Word中的宏,该宏可以完美地工作和调试

结束子

当宏完成并给出最后一条消息,表明文档已成功完成并转到Word宏上的“ end sub”时,我收到一条错误消息,指出Excel已崩溃,需要重新启动!

这些宏创建于2002年,并且可以在所有版本的Office中使用,但是我们开始升级到Office 2010,现在当我运行此宏时,它会使Excel崩溃(仅在Office 2010客户端上)。

我禁止显示消息,但是如果我取消抑制错误,则会收到以下相关消息:

“ Microsoft Excel正在等待另一个应用程序完成OLE操作”,但是我认为在尝试打开W​​ORD时会发生这种情况。

根据我有限的VBA经验,我认为需要将焦点发送回Excel中的宏,以便可以正确结束子过程。我认为Word宏可以正确完成,但不能让最后一个“ end sub”在Excel宏中运行。但是我不知道如何将焦点放回到Excel宏中。

我将定期检查我的电子邮件,并为此进行认真的工作。如果碰巧找到解决方案,我会立即发布。

Excel宏:

Sub Proposal1()

Dim appwd As Object
Dim bookmark1 As String
Dim test As String
Dim ans As String
Dim company As String
Dim goOn As Integer

company = Range("survey!D1")

goOn = MsgBox(prompt:="Do you want to create a proposal for  " & company & " at this         time?", _
    Buttons:=vbYesNo)
If goOn = vbNo Then Exit Sub

Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:="c:\sales\salescalc.xls"
Application.DisplayAlerts = True

Static WordObj As Word.Application
Set WordObj = Nothing
Set WordObj = CreateObject("Word.Application")

WordObj.Visible = True

With WordObj
    .Documents.Add Template:=("C:\sales\sales\proposal1.dot")
    On Error Resume Next

    'Bunch of logic here that reads cells and inputs text to word doc'
    'about 150 lines of code all runs normal'

End With

End Sub

字宏:

Sub picture()

Dim oExcel As Object
Dim oWorkbook As Object
Dim oWorkSheet As Object
Dim verbiage As String
Dim doc As Word.Document
Dim bkmname As String
Dim bkname2 As String
Dim bkname3 As String
Dim verbiage2 As String
Dim verbiage3 As String
Dim spec1 As InlineShape
Dim spec2 As InlineShape
Dim spec3 As InlineShape
Dim pic1 As InlineShape
Dim pic2 As InlineShape
Dim pic3 As InlineShape
Dim pic4 As InlineShape
Dim pic5 As InlineShape
Dim vpic1 As String
Dim company As String
Dim myfolder As String
Dim foldername As String

Set fs = CreateObject("Scripting.FileSystemObject")
Set oExcel = GetObject(, "Excel.Application")

oExcel.Visible = True

Set oWorkbook = oExcel.Workbooks.Open("c:\sales\salescalc.xls")
Set oWorkSheet = oWorkbook.Sheets("survey") 

bkmname = "SO1"
bkmname2 = "SO2"
bkmname3 = "SO3"
vpic1 = "pic1"
company = oWorkSheet.Range("d1").Value
myfolder = "C:\proposals\"

Set doc = ActiveDocument
If oWorkSheet.Range("b15").Value > 0 Then

Set pic1 = Selection.InlineShapes.AddPicture(FileName:= _
    myfolder & company & "\pics\pic1.jpg" _
    , linktofile:=False, savewithdocument:=True, Range:=doc.Bookmarks("pic1").Range)

With pic1
    .Width = InchesToPoints(2.46)
    .Height = InchesToPoints(1.69)
End With
End If

If oWorkSheet.Range("b16").Value > 0 Then

Set pic2 = Selection.InlineShapes.AddPicture(FileName:= _
  myfolder & company & "\pics\pic2.jpg" _
    , linktofile:=False, savewithdocument:=True, Range:=doc.Bookmarks("pic2").Range)

With pic2
    .Width = InchesToPoints(2.46)
    .Height = InchesToPoints(1.69)
End With
End If

If oWorkSheet.Range("b17").Value > 0 Then

Set pic3 = Selection.InlineShapes.AddPicture(FileName:= _
    myfolder & company & "\pics\pic3.jpg" _
    , linktofile:=False, savewithdocument:=True, Range:=doc.Bookmarks("pic3").Range)

With pic3
    .Width = InchesToPoints(2.46)
    .Height = InchesToPoints(1.69)
End With
End If

If oWorkSheet.Range("b18").Value > 0 Then

Set pic4 = Selection.InlineShapes.AddPicture(FileName:= _
    myfolder & company & "\pics\pic4.jpg" _
    , linktofile:=False, savewithdocument:=True, Range:=doc.Bookmarks("pic4").Range)

With pic4
    .Width = InchesToPoints(2.46)
    .Height = InchesToPoints(1.69)
End With
End If

If oWorkSheet.Range("b19").Value > 0 Then
Set pic5 = Selection.InlineShapes.AddPicture(FileName:= _
    myfolder & company & "\pics\pic5.jpg" _
    , linktofile:=False, savewithdocument:=True, Range:=doc.Bookmarks("pic5").Range)

With pic5
    .Width = InchesToPoints(2.46)
    .Height = InchesToPoints(1.69)
End With
End If

Set doc = ActiveDocument
If oWorkSheet.Range("b7") > 0 Then
verbiage = oWorkSheet.Range("H27").Value
Set spec1 = Selection.InlineShapes.AddPicture(FileName:="c:\sales\spec\" & verbiage &  ".gif" _
, linktofile:=False, savewithdocument:=True, Range:=doc.Bookmarks(bkmname).Range)

With spec1
    .Width = InchesToPoints(4.17)
    .Height = InchesToPoints(2.83)
End With
End If

If oWorkSheet.Range("b8") > 0 Then
verbiage2 = oWorkSheet.Range("H28").Value
Set spec2 = Selection.InlineShapes.AddPicture(FileName:= _
    "C:\sales\spec\" & verbiage2 & ".gif" _
    , linktofile:=False, savewithdocument:=True, Range:=doc.Bookmarks(bkmname2).Range)

With spec2
    .Width = InchesToPoints(4.17)
    .Height = InchesToPoints(2.83)
End With
End If

If oWorkSheet.Range("b9") > 0 Then
verbiage3 = oWorkSheet.Range("H29").Value
Set spec3 = Selection.InlineShapes.AddPicture(FileName:= _
    "C:\sales\spec\" & verbiage3 & ".gif" _
    , linktofile:=False, savewithdocument:=True, Range:=doc.Bookmarks(bkmname3).Range)
With spec3
    .Width = InchesToPoints(4.17)
    .Height = InchesToPoints(2.83)
End With
End If

ActiveDocument.SaveAs FileName:=("c:\proposals\" & company & "\" & company & ".doc")

MsgBox "A new company proposal for " & company & " has been created"

End Sub
神经元

如果它在End Sub上崩溃,则可能与对象的破坏有关。确保在代码退出之前手动销毁对象。这将使您准确了解哪个对象使代码崩溃。

在应用程序之间进行编码时,我不会使用两个不同的MACROS。可以告诉Word(或excel)彼此运行。

将所有代码放在1个应用程序中的仅1个宏中。例如,excel会填充内容,然后打开单词。因此,要让excel直接说出该怎么做。

Sub test()
Dim wdApp As New Word.Application
wdApp.Visible = True
wdApp.Documents.Add
wdApp.ActiveDocument.Paragraphs(1).Range.Text = "Hello World"
End Sub

通过引用正确的库(用于2010的Microsoft Word 14.0对象库和用于2013的Microsoft Word 15.0对象库),您可以告诉excel在word文档中做什么,如我的示例所示。

通常,这很容易,例如复制并粘贴代码,然后在with语句中将单词的部分括起来:

with wdAPP
    'All your word specific code here (might need to add a '.' before each command
end with

我尝试从其他应用程序调用宏时发现的另一个问题是,很难知道宏是否在另一侧。可能是用户未正确安装它们(我的宏分配给了约300个人)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在文档中的按键上运行Word宏

在文档Word中嵌入宏

改进 Word 中的 VBA 宏以自动创建与文档名称和位置相关的文件

在 VBA(Word) 上,通过在另一个文档上运行宏来编辑文档或忽略数组中的空行?

从Excel宏更改Word文档中的链接

VBA Word宏无法按预期方式与文档中的字段结果一起使用

带有word文档附件的VBA宏电子邮件

Excel VBA宏,使用输出文本文件查找和替换Word文档中的文本

使用Excel宏VBA将Word文档中的第一个表复制到Excel

如何使用vba宏从ms word文档中隐藏背景颜色为灰色的表格单元格

如何使用VBA宏在Microsoft Word文档中选择每个表

宏以更新Word文档中的所有字段

使用VBA在Access中创建Word文档中的表

使用VBA在Word文档中创建页脚并右对齐

如何在不使用宏的情况下在MS Word中查找文档变量

在 C# 或 VBA VSTO 加载项中运行 Word 宏

Word宏查找并用文本框替换Word文档中的所有内容

如何在Google文档中创建宏

如何在word文档中创建书签,然后使用python创建指向书签的内部超链接

查找和替换,打开word文档的excel宏

根据Excel使用宏在Word中创建条件格式

可以在Word文档中的所选文本中插入关键字注释的宏吗?

Excel中的宏,用于替换Word文档中的确切文本

括號中的 Word VBA 宏

如何从带有宏的Word文档中删除所有超链接?

编写 Word 宏来比较两个单独文档中的值

我想写一个只能在一个word文档中使用的宏

使用VBA创建Word标签表,获取多余的文档

如何使用excel-vba删除部分以创建Word文档