Office 2016 用の基本的な com アドインを開発しようとしています (おそらく他の Office アプリの一部 - おそらく Excel、Word、PowerPoint、Publisher & OneNote 用にグローバルに) ですが、この例では Outlook 2016 用であり、具体的には「挿入」タブのカスタム グループ (「スキャナーとカメラ」) の「Microsoft.Outlook.Mail.Compose」インスペクター リボンへの「スキャナーから挿入」機能。
これは私の最初の VSTO com アドイン プロジェクトであり、コードは初めてです (しかし、意欲的な学習者です!)。私の広範な調査では、段階的なアドバイスはほとんど得られませんでしたが、Microsoft 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 Development Center のサンプル コードは Office 2010 および VS 2010 用であるため、アクセスできません。
サンプルを Office (Outlook) 2016 および VS 2015 で使用できるようにするにはどうすればよいですか?
上記の VB コード ブロックを (記述どおりに) 挿入して、サンプルのテスト ボタンの 1 つのコードを置き換えることはできますか?