เมื่อทำงานกับเอกสาร Office คุณอาจต้องการแสดงเอกสารเหล่านี้โดยตรงใน Visual Basic แต่ไม่ต้องการสร้างการฝังตัววัตถุ OLE ใช้คอนเทนเนอร์ของคอนโทรล OLE แทน คุณต้องการเชื่อมโยงไปยังเอกสารมีอยู่ และเปิดแฟ้มเป็นวัตถุ ActiveX เอกสารแบบแทนที่
fortunately การควบคุมเว็บเบราว์เซอร์ของ Microsoft เสนอวิธีแก้ไขปัญหา
บทความนี้อธิบายวิธีการนำทางไปยังเอกสาร Office มีอยู่ และแสดงอยู่ใน Visual Basic ที่ใช้การควบคุมเว็บเบราว์เซอร์
activex เอกสารเป็นวัตถุ OLE embeddable ที่ทำงานมากขึ้นเช่นเดียวกับตัวควบคุม activex ที่ไม่ใช่วัตถุ OLE แบบดั้งเดิม ออกแบบแตกต่างจากวัตถุฝังตัวแบบปกติ เพิ่มเอกสาร ActiveX ไม่มาเป็น วัตถุที่มีอยู่ในเอกสารที่มีขนาดใหญ่ แทน มันจะถือเป็นเอกสารเสร็จสมบูรณ์ ด้วยตัวเอง ซึ่งเป็นแค่จะถูกแสดงตามตัวแสดง (เช่น Internet Explorer) หรือจะถูกเก็บรวบรวมไว้ในทรัพยากรเดียวกับเอกสารอื่น (เช่น Binder แบบแฟ้ม)
ในขณะที่ Microsoft Visual Basic ไม่อยู่ในขณะนี้สนับสนุนการโฮสต์ ActiveX เอกสารโดยตรง คุณสามารถหลีกเลี่ยงข้อจำกัดนี้ โดยใช้ความสามารถของ Internet Explorer และการควบคุมของเว็บเบราว์เซอร์ การควบคุมเว็บเบราว์เซอร์ (Shdocvw.dll) เป็นส่วนหนึ่งของ Internet Explorer และสามารถใช้กับระบบที่มีการติดตั้ง Internet Explorer เท่านั้น
การสร้างแอพลิเคชันของ Visual Basic ที่เปิดเอกสาร Office
ใช้ขั้นตอนต่อไปนี้เพื่อสร้างโปรแกรมประยุกต์ Visual Basic ที่เปิดเอกสาร Office:
- เริ่มการทำงานของ Visual Basic และสร้างโครงการใหม่มาตรฐาน Form1 จะถูกสร้างขึ้นตามค่าเริ่มต้น
- จากนั้นProjectเมนู การเลือกคอมโพเนนต์เมื่อต้องการเปิดการคอมโพเนนต์กล่องโต้ตอบ ในกล่องโต้ตอบการส่วนประกอบ เพิ่มการอ้างอิงไปยัง Microsoftตัวควบคุมกล่องโต้ตอบที่พบโดยทั่วไปและยัง Microsoftการควบคุมอินเทอร์เน็ต. คลิกตกลงเพื่อเพิ่มรายการลงในกล่องเครื่องมือ
- เพิ่มอินสแตนซ์ของตัวควบคุมเว็บเบราว์เซอร์ CommonDialog ควบคุม และแบบ CommandButton ไป Form1
- ขั้นตอนถัดไป เพิ่มรหัสต่อไปนี้ลงในหน้าต่างรหัสสำหรับ Form1:
Option Explicit
Dim oDocument As Object
Private Sub 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
- กดf5เมื่อต้องการเรียกใช้โครงการ เมื่อคุณเลือกนั้นเรียกดูปุ่มOPENกล่องโต้ตอบปรากฏขึ้นให้คุณสามารถนำทางไปยังแฟ้ม Word, Excel หรือ PowerPoint เลือกOPENและเอกสารควรเปิดอยู่ภายในตัวควบคุมเว็บเบราว์เซอร์ กล่องข้อความแล้วแสดงว่าจะแสดงชื่อของโปรแกรมประยุกต์ของ Office ที่เปิดแฟ้ม
สิ่งที่ควรพิจารณาเมื่อคุณใช้การควบคุมเว็บเบราว์เซอร์ในโปรแกรม Microsoft Office 2007
ต่อไปนี้ควรพิจารณาเมื่อคุณใช้เว็บเบราว์เซอร์ที่ตัวควบคุม:
- การควบคุมเว็บเบราว์เซอร์ asynchronously navigates กับเอกสาร ซึ่งหมายความ ว่า การเรียกกลับการควบคุมไปยังโปรแกรมประยุกต์ Visual Basic ของคุณก่อนที่เอกสารได้ถูกโหลดโดยสมบูรณ์ เกิดขึ้นเมื่อคุณเรียกใช้ WebBrowser1.Navigate ถ้าคุณมีแผนที่จะทำให้เอกสารที่มีอยู่ ใช้NavigateComplete2เหตุการณ์ดังนั้นคุณจะทราบเมื่อเอกสารได้เสร็จสิ้นการโหลด ใช้แบบเอกสารคุณสมบัตินี้เว็บเบราเซอร์วัตถุการอ้างอิงถึงออบเจ็กต์เอกสาร Office ที่ได้รับ ในรหัสก่อนหน้า การอ้างอิงถูกกำหนดเป็นoDocument.
- หลายทราบว่า ปัญหาที่เกิดขึ้นได้เมื่อคุณมีการควบคุมเว็บเบราว์เซอร์มากกว่าหนึ่งในโครงการ และแต่ละตัวควบคุมถูกโหลด ด้วยเอกสาร Office ชนิดเดียวกัน นั่นคือ แต่ละตัวควบคุมประกอบด้วยเอกสาร Word ทั้งหมดหรือกระดาษคำนวณของ Excel ทั้งหมด
We recommend that you use only one control per project and that you move to one document at a time.
- To clear the current content from the WebBrowser, use the following code to navigate to the default blank page in the Click event of another command button. Or, do this in some other appropriate place in your code.
WebBrowser1.Navigate "about:blank"
What to consider when you use the WebBrowser control in the Microsoft Office 2003 programs and in earlier versions of Office
The following should be considered when you use the WebBrowser
control:
- The WebBrowser control navigates to documents
asynchronously. This means that when you call WebBrowser1.Navigate, the call
returns control to your Visual Basic applicationก่อนthe document has been completely loaded. If you plan on
Automating the contained document, you need to use theNavigateComplete2event to know when the document has finished loading. ใช้แบบDocumentproperty of the WebBrowser object passed in to get a reference to
the Office document object, which, in the preceding code, is set to
oDocument.
- The WebBrowser control does not support menu merging. If
you need the document's menu items to appear with your Visual Basic menu, you
must use the OLE container control instead.
- The WebBrowser control generally hides any docked toolbars
before displaying an Office document. You can use Automation to show a floating
toolbar using code such as this:
With oDocument.Application.CommandBars("Standard")
.Position = 4 '[msoBarFloating]
.Visible = True
End With
Newer versions of Internet Explorer (5.0 and greater) also allow you to
display docked toolbars using the following code:
' This is a toggle option, so call it once to show the
' toolbars and once to hide them. This works with Internet Explorer 5
' but often fails to work properly with earlier versions...
WebBrowser1.ExecWB OLECMDID_HIDETOOLBARS, OLECMDEXECOPT_DONTPROMPTUSER
- There are several known issues with having more than one
WebBrowser control in a project, and having each control loaded with the same
type of Office document (that is, all Word documents, or all Excel
spreadsheets). It is recommended that you only use one control per project, and
navigate to one document at a time.
The most common problem is with
Office Command Bars, which appear disabled. If you have two WebBrowser controls
on the same form, both loaded with Word documents, and you have displayed
toolbars using one of the preceding techniques, only one set of toolbars will
be active and work correctly. The other will be disabled and cannot be
used. - To clear the WebBrowser of its current contents, in theคลิกevent of another command button (or in some other appropriate
place in your code), navigate to the default blank page using this code:
WebBrowser1.Navigate "about:blank"
For more information about the WebBrowser control in Visual Basic, click the following article numbers to view the articles in the Microsoft Knowledge Base:
162719
(http://support.microsoft.com/kb/162719/
)
How to use the WebBrowser control from Visual Basic 5.0
188271
(http://support.microsoft.com/kb/188271/
)
How to print contents of the Web Browser control from VB
191692
(http://support.microsoft.com/kb/191692/
)
Shdocvw.dll is not included in PDW setup package
238313
(http://support.microsoft.com/kb/238313/
)
Accessing the Internet Explorer Document Object Model from Visual Basic
927009
(http://support.microsoft.com/kb/927009/
)
A new window opens when you try to view a 2007 Microsoft Office program document in Windows Internet Explorer 7
หมายเลขบทความ (Article ID): 243058 - รีวิวครั้งสุดท้าย: 8 มกราคม 2554 - Revision: 4.0
ใช้กับ
- Microsoft Office Excel 2007
- Microsoft Excel 2002 Standard Edition
- Microsoft Excel 2000 Standard Edition
- Microsoft Excel 97 Standard Edition
- Microsoft Office PowerPoint 2007
- Microsoft Office PowerPoint 2003
- Microsoft PowerPoint 2002 Standard Edition
- Microsoft PowerPoint 2000 Standard Edition
- Microsoft PowerPoint 97 Standard Edition
- Microsoft Office Word 2007
- Microsoft Word 2002 Standard Edition
- Microsoft Word 2000 Standard Edition
- Microsoft Word 97 Standard Edition
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic 5.0 Professional Edition
- Microsoft Visual Basic 5.0 Enterprise Edition
| kbexpertiseinter kbhowto kbmt KB243058 KbMtth |
แปลโดยคอมพิวเตอร์ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ
ต่อไปนี้เป็นฉบับภาษาอังกฤษของบทความนี้:
243058
(http://support.microsoft.com/kb/243058/en-us/
)