VB: Öffnet Office-Dokumente in Web Browser Control

Verwenden Sie die folgenden Schritte aus, um eine Visual Basic-Anwendung, die Office-Dokumente öffnen zu erstellen:

  1. Starten Sie Visual Basic, und erstellen Sie ein neues Standard-Projekt. Form1 wird standardmäßig erstellt.
  2. Aus dem Menü Projekt Komponenten zu wählen Öffnen Sie das Dialogfeld Komponenten.
  3. In dem Dialogfeld Komponenten fügen Verweise auf die Microsoft Common Dialog Control und den Microsoft Internet Controls.
  4. Klicken Sie auf OK, um die Elemente der Toolbox hinzufügen.
  5. Fügen Sie eine Instanz des WebBrowser-Steuerelement CommonDialog-Steuerelement und ein Befehlsschaltfläche zu Form1.
  6. Als Nächstes fügen Sie den folgenden Code in das Codefenster für Form1:
     Command1_Click() Dim sFileName As String ' Find an Office file... With CommonDialog1 .FileName = "" .ShowOpen sFileName = .FileName End With ' If the user didn't cancel, open the file... If Len(sFileName) Then Set oDocument = Nothing WebBrowser1.Navigate sFileName End If End Sub Private Sub Form_Load() Command1.Caption = "Browse" ' For the 2007 Microsoft Office documents, change the .Filter parameter of the ' With CommonDialog1 statement to: ' .Filter = "Office Documents " & _ '      "(*.docx, *.xlsx, *.pptx)|*.docx;*.xlsx;*.pptx" With CommonDialog1 .Filter = "Office Documents " & _ "(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt" .FilterIndex = 1 .Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly End With End Sub Private Sub Form_Unload(Cancel As Integer ) Set oDocument = Nothing End Sub Private Sub WebBrowser1_NavigateComplete2( ByVal pDisp As Object , _ URL As Variant ) On Error Resume Next Set oDocument = pDisp.Document MsgBox "File opened by: " & oDocument.Application.Name End Sub Option Explicit Dim oDocument As Object Private Sub Command1_Click () Dim sFileName As String 'Finden Sie eine Office-Datei ... Mit CommonDialog1. FileName = "". ShowOpen sFileName =. FileName End With' Wenn der Benutzer nicht zu kündigen, öffnen Sie die Datei ... If Len (sFileName) Then Set oDocument = Nothing WebBrowser1.Navigate sFileName End If End Sub Private Sub Form_Load () Command1.Caption = "Durchsuchen", "für das 2007 Microsoft Office-Dokumente, ändern Sie die. Filter-Parameter der 'Mit CommonDialog1 Aussage zu: ". Filter =" Office Documents "& _ '" (*. docx, *. xlsx, *. PPTX) | *. docx, *. xlsx, *. PPTX "Mit CommonDialog1. Filter =" Office Documents "& _" (*. doc, *. xls, *. ppt) | *. doc, *. xls, *. ppt ". FilterIndex = 1. Flags = cdlOFNFileMustExist Oder cdlOFNHideReadOnly End With End Sub Private Sub Form_Unload ( Cancel As Integer) Set oDocument = Nothing End Sub Private Sub WebBrowser1_NavigateComplete2 (ByVal pDisp As Object, _ URL As Variant) On Error Resume Next Set oDocument = pDisp.Document MsgBox "Datei öffnen von:" & oDocument.Application.Name End Sub 
  7. Drücken Sie F5, um das Projekt auszuführen. Wenn Sie wählen Sie die Schaltfläche Durchsuchen, Dialogfeld Öffnen erscheint die sodass Sie zu navigieren, ein Word-, Excel-oder PowerPoint-Datei.
  8. Wählen Sie Öffnen und das Dokument sollte die Kontrolle innerhalb des WebBrowser öffnen.
  9. Ein Meldungsfeld dann angezeigt, das den Namen des Office-Anwendung, dass die Datei geöffnet.

Post to Twitter Post zu Delicious Bei Facebook posten

One Response to "VB: Öffnet Office-Dokumente in Web Browser Control"

    Seek hier
    Übersetzer
    Archivieren