Mở file *.doc từ Excel (2 người xem)

  • Thread starter Thread starter chibi
  • Ngày gửi Ngày gửi
Liên hệ QC

Người dùng đang xem chủ đề này

chibi

Thành viên tích cực
Thành viên danh dự
Tham gia
10/1/07
Bài viết
1,120
Được thích
623
Diễn đàn đã giới thiệu cách gọi MS Word từ Excel. Giúp tôi mở file MS Word (C:\vidu.doc) từ Excel.
 
Đây là một cách:
Mở MS Excel, vào VBA rồi Insert/ Module
Nhấn tiếp Tools/ References, chọn Microsoft Word 11.0 Object Library (số 11.0 có thể khác tuỳ thuộc vào phiên bản Microsoft Office bạn cài trên máy).
Thêm đoạn code sau vào Module:
Mã:
Option Explicit
Public Sub Run_Word()
Dim x1 As Object
Dim x2 As Object
Set x1 = CreateObject("Word.Application")
Set x2 = x1.Documents.Open("C:\vidu.doc")
x1.Visible = True
End Sub
 
Upvote 0
Dear all,
--------
Một mẹo để mở bất kỳ chương trình có thể thực thi (executable programe - *.exe) nào mà bạn biết về nó là sử dụng hàm shell.
Runs an executable program and returns a Variant (Double) representing the program's task ID if successful, otherwise it returns zero.
Syntax
Shell(pathname[,windowstyle])
The Shell function syntax has these named arguments:
pathname: Required; Variant (String). Name of the program to execute and any required arguments or command-line switches; may include directory or folder and drive. On the Macintosh, you can use the MacID function to specify an application's signature instead of its name. The following example uses the signature for Microsoft Word:
Shell MacID("MSWD")
windowstyle: Optional. Variant (Integer) corresponding to the style of the window in which the program is to be run. If windowstyle is omitted, the program is started minimized with focus. On the Macintosh (System 7.0 or later), windowstyle only determines whether or not the application gets the focus when it is run.
Bạn hiểu rằng mỗi tập tin đều do một chương trình quản lý nhất định (*.doc là sản phẩm của MS Word, *.xls là sản phẩm của MS Excel). Điều quan trọng là bạn phải biết executable programe nào có thể khởi động được các tập tin này. Dễ thôi, hãy nghĩ tới Task Manager - trình quản lý bộ nhớ và các chương trình đang thi hành. Trong thẻ Processes bạn sẽ biết được EXE nào bạn vừa kích hoạt. Đây chính là mấu chốt để bạn sử dụng hàm Shell:
Shell(pathname[,windowstyle])
trong đó: pathname là một string bao gồm 2 thành phần:
Application - executable programe để mở tập tin mà bạn đã biết thông qua Task Manager
Fullname Path application and file name (chắc không cần dịch)

Bây giờ, hãy mở ví dụ của chibi sau khi bạn nắm được mấu chốt:
Mã:
Dim strApplication as String, strFullname As String, objVidu As Object
strApplication= "WinWord"
strFullname = "C:\Vidu.doc"
Shell strApplication & " " & strFullname
Set objVidu = ActiveWindow
[COLOR=darkgreen]'Bạn lưu ý sau khi làm việc xong với objVidu thì cần Set objVidu = Nothing để giải phóng bộ nhớ.[/COLOR]
Với Tip này, bạn có thể giải quyết được hầu hết các trường hợp và khắc phục được không biết phải Create Object gì, cần References nào!

Bạn đoán xem code sau làm gì:
Mã:
Dim strApplication as String, strFullname As String, objhh As Object
strApplication= "hh"
strFullname = "C:\PROGRA~1\COMMON~1\MICROS~1\VBA\VBA6\1033\VbLR6.chm::/html/vafctShell.htm"
Shell strApplication & " " & strFullname
Set objhh = ActiveWindow
 
Lần chỉnh sửa cuối:
Upvote 0
Cách của Đào Việt Cường không phụ thuộc vào phiên bản Word. Nhưng không hiểu sao cửa sổ của thằng vidu.doc không Active ngay mà nằm ở khay hệ thống.
 
Upvote 0
Dear chibi,
----------
Phải nói là Windows chỉ hiện trên thanh tác vụ (Task bar) thì mới đúng
Shell(pathname[,windowstyle])
Bạn đọc lại hướng dẫn về Shell, tham số thứ 2 [,windowstyle] (không bắt buộc) giải đáp thắc mắc cho bạn!
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom