0

我希望为 Office 2016 开发一个基本的 com 加载项(可能是全球范围内的一些其他办公应用程序 - 最有可能是 Excel、Word、PowerPoint、Publisher 和 OneNote),但在这种情况下是为 Outlook 2016 开发的,特别是添加一个将“从扫描仪插入”功能添加到“插入”选项卡上自定义组(“扫描仪和相机”)中的“Microsoft.Outlook.Mail.Compose”检查器功能区。

这是我的第一个 VSTO com 插件项目,我是代码新手(但愿意学习!)。我的广泛研究在分步建议中几乎没有收集到,但我从微软 https://code.msdn.microsoft.com/office/VBOutlookRibbonXml-bc478854中确定了以下代码示例,我希望能适应它(也许利用以下“扫描”功能vb代码):

Private Declare PtrSafe Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Private Function TempPath() As String
    Const MaxPathLen = 256 ' Max length of the path, just as big as possible

    Dim FolderName As String ' Name of the folder
    Dim ReturnVar As Long ' Return Value

    FolderName = String(MaxPathLen, 0)
    ReturnVar = GetTempPath(MaxPathLen, FolderName)

    If ReturnVar <> 0 Then
        TempPath = Left(FolderName, InStr(FolderName, Chr(0)) - 1)
    Else
        TempPath = vbNullString
    End If
End Function

Sub Scan(control As IRibbonControl)
    Const olEditorWord = 4
    Dim objCommonDialog As WIA.CommonDialog
    Dim objImage As WIA.ImageFile
    Dim strDateiname As String
    Dim ActiveObject As Object, ActiveTarget As Object
    ' instantiate Scan WIA objects
    Set objCommonDialog = New WIA.CommonDialog
    Set objImage = objCommonDialog.ShowAcquireImage
    strDateiname = Environ$("TEMP") & "\Scan.jpg" ' set temporary file
    If Not objImage Is Nothing Then
        If Dir(strDateiname) <> "" Then Kill strDateiname
        objImage.SaveFile strDateiname 'save into temp file
        DoEvents

        'Insert the picture into the office application:
        Select Case Trim$(Replace$(Application.Name, "Microsoft", ""))
          Case "Excel"
            Set ActiveObject = CallByName(Application, "ActiveSheet", VbGet)
            Set ActiveTarget = CallByName(Application, "ActiveCell", VbGet)
            If ActiveTarget Is Nothing Then
                'Insert into a chart, etc.
                ActiveObject.Shapes.AddPicture _
                strDateiname, False, True, 0, 0, -1, -1
            Else
                'Insert into a sheet at the active cell
                ActiveObject.Shapes.AddPicture _
                strDateiname, False, True, ActiveTarget.Left, ActiveTarget.Top, -1, -1
            End If

          Case "Outlook"
            Set ActiveObject = CallByName(Application, "ActiveInspector", VbGet)
            If ActiveObject Is Nothing Then
                MsgBox "Create a new mail and try again"
                Exit Sub
            End If
            With ActiveObject
                If .IsWordMail And .EditorType = olEditorWord Then
                    .WordEditor.Application.Selection.InlineShapes.AddPicture strDateiname
                End If
            End With

          Case "PowerPoint"
            Set ActiveObject = CallByName(ActiveWindow, "View", VbGet)
            ActiveObject.Slide.Shapes.AddPicture strDateiname, False, True, 0, 0, -1, -1
          Case "Publisher"
            Set ActiveObject = CallByName(Application, "ActiveDocument", VbGet)
            ActiveObject.ActiveView.ActivePage.Shapes.AddPicture strDateiname, False, True, 0, 0, -1, -1
          Case "Word"
            Set ActiveObject = CallByName(Application, "Selection", VbGet)
            ActiveObject.InlineShapes.AddPicture strDateiname
        End Select
    End If
End Sub

不幸的是,上面的 Microsoft Office 开发中心示例代码适用于 Office 2010 和 VS 2010,因此无法访问。

  1. 如何调整示例以用于 Office (Outlook) 2016 和 VS 2015?

  2. 是否可以插入上述 VB 代码块(如所写)以替换示例上的测试按钮的代码,还是需要进一步调整?

4

1 回答 1

0

您可以简单地将示例项目中的类复制到您的 VS 2015 项目中。示例项目使用的是 VS 2015 不支持的 Office 版本的项目模板,并且互操作引用也会有所不同。

如果您要添加自定义功能区按钮,只需在“添加新项”对话框中的 Office/SharePoint 节点中添加功能区(可视化设计器)项(或功能区 (XML) 项)。

于 2016-06-27T19:41:56.937 回答