Word中的宏无法在Excel中工作,是否可以针对Excel进行修改?

时间:2014-08-02 23:42:54

标签: excel vba excel-vba

我意识到它可能不应该工作,但我有一个来自Word宏的可视化基本代码,它打开一个软件链接到连接到计算机的一块连接设备,当宏运行时,它插入从设备的屏幕到word文档的图像,无论如何都要改变这个代码,所以它在excel中做同样的事情?

 ' InsertInsertActiveScreen Module
' Function: Start FlukeView if required
'           Locate position for inserting Screen
'           Insert active screen at cursor position
Global Const AppName = "FlukeView ScopeMeter"

Global StartedFV90 As String

' Declare constant values

Private Const ER_NONE = 0
Private Const ER_DDE_CMD_UNK = 25
Private Const ER_DDE_NO_INIT = 26
Private Const ER_DDE_NO_CONN = 27
Private Const ER_DDE_NO_SERVER = 28

Public Sub MAIN()

    Dim chan As Long
    Dim Status As String
    Dim wordApp As Object
    Set wordApp = CreateObject("Word.Application")

    ' Display cursor as an hourglass
    Application.Cursor = xlWait

    If (wordApp.Tasks.Exists(AppName) = False) Then

        ' Start FlukeView as server (-s) Modification of these statement is required if
        ' FlukeView is installed in another directory as the QReport.dot template
        Call Shell(ActiveWorkbook.Path & Application.PathSeparator & "FV90WIN.EXE -s", vbMinimizedNoFocus)

        ' Reminder for terminating FlukeView when closing the document
        StartedFV90 = "STARTED"

    Else

        If (Len(StartedFV90) = 0) Then
            ' Reminder to prevent terminating FlukeView when closing the document
            StartedFV90 = "NOT STARTED BY Fluke View Report"
        End If

    End If

    ' Setup a DDE link with FlukeView
    chan = DDEInitiate(App:="FV90WIN", Topic:="FlukeView")

    While (Val(DDERequest(Channel:=chan, Item:="DDEStatus")) <> ER_NONE)

        ' Wait until FlukeView is ready to receive commands

    Wend

    Call DDEExecute(Channel:=chan, String:="Connect")

    DoEvents

    ' Transfer the active screen and place it on the clipboard
    Call DDEExecute(Channel:=chan, String:="Screen")

    ' Read completion status
    Status$ = DDERequest(Channel:=chan, Item:="DDEStatus")

    If (Val(Status) = ER_NONE) Then

        ' locate bookmark for pasting contents
        Call Selection.GoTo(What:=wdGoToBookmark, Name:="InstrumentScreen")

        ' Paste the contents of the clipboard into the document
        Call Selection.PasteSpecial

        ' Convert to Inline Shape to prevent overlapping images
        For Each ScreenPicture In ActiveSheet.Shapes
            If ScreenPicture.Type = msoPicture Then
            ScreenPicture.ConvertToInlineShape
            End If
        Next ScreenPicture

    Else

        ' Error occurred
        Call DDEExecute(Channel:=chan, String:="Error" + Status$)

    End If

    ' Terminate DDE connection
    Call DDETerminate(Channel:=chan)

    ' Restore cursor
    Application.Cursor = xlDefault

End Sub

这是我没有的,我得到运行时错误&#39; 13&#39;:类型不匹配?

1 个答案:

答案 0 :(得分:0)

当从Word转移到Excel时,很难知道宏中的功能和功能。但是,只要查看代码,我就可以提供一些指针,让您朝着正确的方向前进:

  1. Global替换为Private作为全局定义。如果你不这样做,Excel会给你一个编译错误:&#34;常量,固定长度的字符串,数组,用户定义的类型和不允许声明语句作为对象模块的公共成员。&# 34;

  2. 替换:

    System.Cursor = wdCursorWait
    System.Cursor = wdCursorNormal
    

    使用:

    Application.Cursor = xlWait
    Application.Cursor = xlDefault
    
  3. 对于活动工作簿中的活动工作表,Excel没有ActiveDocumentActiveWorkbookActiveSheet

  4. 我认为你找不到一个简单的方法来打电话:

    Tasks.Exists()
    
    在Excel中

    。但是,您可以欺骗并调用Word来为您完成这项工作:

    Dim wordApp As Object
    Set wordApp = CreateObject("Word.Application")
    ...
    wordApp.Tasks.Exists()
    ...
    Set wordApp = Nothing ' Call this when you're done with it.
    
  5. Excel的DDEExecute签名略有不同,但我自己从未使用过,所以我无法告诉您这是否有用。第二个参数在Excel中不称为Command,而是String。尝试将Command:=替换为String:=,看看是否有效。

  6. Selection.Copy将复制所选范围,类似于Word。但是您需要Selection.PasteSpecial来粘贴它。当然,在这种情况下,你会复制并粘贴相同的东西,所以你会发现没有什么不同。我建议查看Excel中Range对象的复制/粘贴功能。

  7. 无论如何,这就是我现在所能想到的。它不会涵盖Word和Excel之间的所有不同之处,但它只是一个开始。这里没有人能够完全帮助你,因为你依赖于特定程序的功能(&#34; FlukeView&#34;或者#34; FV90WIN.EXE&#34;)工作。