Entrades etiquetades 'VB'
VB: Obre documents d'Office en el navegador web Control
Utilitzeu els passos següents per crear una aplicació de Visual Basic que obre documents d'Office:
- Inicieu Visual Basic i creï un nou projecte Standard. Form1 es crea per defecte.
- Al menú Projecte, seleccioneu Components per obrir el quadre de diàleg Components.
- En el quadre de diàleg Components, afegiu referències a la Microsoft Common Dialog Control i Controls d'Internet de Microsoft.
- Premeu D'acord per afegir els elements al quadre d'eines.
- Afegiu una instància del control webBrowser, el control CommonDialog, i un CommandButton a Form1.
- A continuació, afegiu el següent codi a la finestra de codi per 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 'Cerca un arxiu d'Office ... Amb CommonDialog1. Filename = "". SFileName ShowOpen = nom de fitxer. End With' Si l'usuari no cancel.lar, obriu l'arxiu ... Si Len (sFileName) Establir oDocument Després = Nothing sFileName Fi WebBrowser1.Navigate If End Sub Private Sub Form_Load () Command1.Caption = "Examinar" 'Per als documents de Microsoft Office 2007, canviï el. filtre de paràmetre de la «Amb CommonDialog1 declaració: '. Filtre = "documents d'Office" & _' "(*. docx, *. xlsx, *. pptx) | *. docx, *. xlsx, *. pptx" Amb CommonDialog1. Filtre = "Oficina Documents "& _" (*. doc, *. xls, *. ppt) | *. doc, *. xls, *. ppt ". FilterIndex = 1. Banderes cdlOFNFileMustExist = O Fi cdlOFNHideReadOnly With End Sub Private Sub Form_Unload ( Cancel As Integer) setembre oDocument = Nothing End Sub Private Sub WebBrowser1_NavigateComplete2 (pDisp ByVal As Object, _ URL As Variant) On Error Resume oDocument Següent setembre = pDisp.Document msgbox "Arxiu obert per:" & oDocument.Application.Name End Sub
- Premeu F5 per executar el projecte. Si seleccioneu el botó Examinar, el quadre de diàleg Obrir que li permet navegar a un Word, Excel o PowerPoint.
- Trieu Obrir i el document ha d'obrir dins del control webBrowser.
- Un quadre de missatge apareix que mostra el nom de l'aplicació Office que va obrir l'arxiu.

