VBA Excel Convert word to html

  • Thread starter thanhphong
  • Ngày gửi
T

thanhphong

Guest
1. File excel thiết định thư viện sau:
Bạn cần đăng nhập để thấy hình ảnh

2. Đoạn code sau sẽ chuyển file word thành html.
Mã:
Public Sub test()
    Dim objWord As Object

    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    objWord.Documents.Open "D:\VBA\File1.docx"

    objWord.ActiveDocument.SaveAs2 Filename:="D:\VBA\SaveAsHtml.htm", FileFormat:= _
        wdFormatFilteredHTML, LockComments:=False, Password:="", AddToRecentFiles _
        :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
        :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False, CompatibilityMode:=0
   
    objWord.ActiveDocument.Close
    objWord.Quit
End Sub
Hạn chế: Đường link không được có khoảng trống, tức là tên thư mục phải viết liền nhau.
Nguồn:
 
T

thanhphong

Guest
Thứ phát triển hơn công cụ bằng cách chọn File Word sẽ Save as thành HTML ở cùng thư mục
Bạn giúp mình làm như thế này:
Tool load hết file .doc* kể cả trong các thư mục con, hiển thị ra listbox. Rồi chọn vào file nào thì tạo ra file html trong cùng thư mục với file .doc* đó. Trước đó kiểm tra nếu file html tồn tại rồi thì xóa không cần hỏi.
 

tuhocvba

Administrator
Thành viên BQT
Nào, góp vui tí. Tặng các bạn bản thiết kế.
Bạn cần đăng nhập để thấy hình ảnh
 

Vu Van Hoang

Yêu THVBA
Bạn thử đoạn code này xem

Mã:
Sub Convert()


Dim filePath As String

With Application.FileDialog(msoFileDialogFolderPicker)

    .AllowMultiSelect = False

    .Show

    On Error Resume Next

    filePath = .SelectedItems(1)

End With


If filePath = "" Then Exit Sub

If Right(filePath, 1) <> "\" Then filePath = filePath & "\"


Application.ScreenUpdating = False


Dim currFile As String

currFile = Dir(filePath & "*.docx")


Do While currFile <> ""


    Documents.Open (filePath & currFile)

    Documents(currFile).SaveAs2 FileName:=filePath & Left(currFile, Len(currFile) - Len(".docx")) & ".html", FileFormat:=wdFormatHTML

    Documents(currFile).Close

      

    currFile = Dir()

Loop


Application.ScreenUpdating = True

  

End Sub
 
Top