quyenpv
Thu nhặt kiến thức
- Tham gia
- 5/1/13
- Bài viết
- 719
- Được thích
- 97
- Giới tính
- Nam
- Nghề nghiệp
- Decode cuộc đời!
Em chào anh chị
Trước em có nhờ các anh chị giúp đỡ Code và được anh ThangCuAnh giúp đỡ tại Topic này. Code chạy rất tốt tuy nhiên muốn sử dụng nhiều em lại làm thêm 1 Sub tương tự như thế chỉ điều chỉnh 2 biến là thư mục lưu file và tên file
Mong muốn nhờ các anh viết giúp em 1 Sub chung cho phép lấy tên biến khai báo khi cần chạy Code thôi chung
Trước em có nhờ các anh chị giúp đỡ Code và được anh ThangCuAnh giúp đỡ tại Topic này. Code chạy rất tốt tuy nhiên muốn sử dụng nhiều em lại làm thêm 1 Sub tương tự như thế chỉ điều chỉnh 2 biến là thư mục lưu file và tên file
Mong muốn nhờ các anh viết giúp em 1 Sub chung cho phép lấy tên biến khai báo khi cần chạy Code thôi chung
Mã:
Sub Gop()
Dim sRootFolder As String, sFileName as String
sRootFolder ="ABC"
sFileName="Ten File"
Call Sub_PLy
msgbox "Hoàn thành"
End Sub
Mã:
Option Private Module
'Option Explicit
Public wdApp As Object
Sub Export_HSPly()
On Error Resume Next
Dim wDoc As Object, xlWB As Workbook, shtDanhMuc As Worksheet
Dim strFind As String, strReplace As String, strPath As String
Dim myArr As Variant
Dim LastRow As Long, i As Integer
Set xlWB = Workbooks(ActiveWorkbook.Name)
'Set shtDanhMuc = xlWB.Sheets("XHH")
Set shtDanhMuc = xlWB.ActiveSheet
shtDanhMuc.Activate
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'Remove all other filters
Call Application.FileDialog(msoFileDialogOpen).Filters.Clear
'Add a custom filter
Call Application.FileDialog(msoFileDialogOpen).Filters.Add("Word Files Only", "*.doc;*.docx")
'make the file dialog visible to the user
i = Application.FileDialog(msoFileDialogOpen).Show
If 0 = i Then Exit Sub
'determine what choice the user made
'get the file path selected by the user
strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
'print the file path to sheet 1
shtDanhMuc.[D4] = strPath
LastRow = shtDanhMuc.Cells(rows.count, "C").End(xlUp).Row ' Last row cua cot C
If (LastRow <= 2) Then Exit Sub ' Không có data
myArr = shtDanhMuc.Range("C5:D" & LastRow).value ' copy range values tu C6:D &lastRow to myArr
' Open word file for data merge
Set wdApp = CreateObject("Word.Application")
wdApp.visible = True
Set wDoc = wdApp.Documents.Open(strPath, ReadOnly:=True)
wDoc.Activate
For i = 1 To UBound(myArr)
strFind = myArr(i, 1) 'Tu khoa
If myArr(i, 2) <> "" Then
strReplace = myArr(i, 2) 'Noi dung thay the
Else
strReplace = "............."
End If
FindAndReplace "{-" & Trim(strFind) & "-}", strReplace
Next i
'''''Luu thanh file Bao cao voi ten File tai D5
wDoc.Content.Font.Color = wdColorAutomatic 'Chuyen tat ca noi dung chu ve mau den
wDoc.SaveAs FileName:=ThisWorkbook.Path & "\1. HDong Export\" & Range("D5") & ".doc" 'Luu ten file
wdApp.Documents(ThisWorkbook.Path & "\1. HDong Export\" & Range("D5") & ".doc").Close (True) 'Chay code xong dong file Word
wdApp.Quit
End Sub
Sub Run_Export_TTrQD()
On Error Resume Next
Dim wDoc As Object, xlWB As Workbook, shtDanhMuc As Worksheet
Dim strFind As String, strReplace As String, strPath As String
Dim myArr As Variant
Dim LastRow As Long, i As Integer
Set xlWB = Workbooks(ActiveWorkbook.Name)
'Set shtDanhMuc = xlWB.Sheets("XHH")
Set shtDanhMuc = xlWB.ActiveSheet
shtDanhMuc.Activate
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'Remove all other filters
Call Application.FileDialog(msoFileDialogOpen).Filters.Clear
'Add a custom filter
Call Application.FileDialog(msoFileDialogOpen).Filters.Add("Word Files Only", "*.doc;*.docx")
'make the file dialog visible to the user
i = Application.FileDialog(msoFileDialogOpen).Show
If 0 = i Then Exit Sub
'determine what choice the user made
'get the file path selected by the user
strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
'print the file path to sheet 1
shtDanhMuc.[D4] = strPath
LastRow = shtDanhMuc.Cells(rows.count, "C").End(xlUp).Row ' Last row cua cot C
If (LastRow <= 2) Then Exit Sub ' Không có data
myArr = shtDanhMuc.Range("C5:D" & LastRow).value ' copy range values tu C6:D &lastRow to myArr
' Open word file for data merge
Set wdApp = CreateObject("Word.Application")
wdApp.visible = True
Set wDoc = wdApp.Documents.Open(strPath, ReadOnly:=True)
wDoc.Activate
For i = 1 To UBound(myArr)
strFind = myArr(i, 1) 'Tu khoa
If myArr(i, 2) <> "" Then
strReplace = myArr(i, 2) 'Noi dung thay the
Else
strReplace = "............."
End If
FindAndReplace "{-" & Trim(strFind) & "-}", strReplace
Next i
'''''Luu thanh file Bao cao voi ten File tai D5
wDoc.Content.Font.Color = wdColorAutomatic 'Chuyen tat ca noi dung chu ve mau den
sRootFolder = ActiveWorkbook.Path
wDoc.SaveAs FileName:=sRootFolder & "\TTr_QD_" & Range("D27") & ".doc" 'Luu ten file
wdApp.Documents(sRootFolder & "\TTr_QD_" & Range("D27") & ".doc").Close (True) 'Chay code xong dong file Word
wdApp.Quit
End Sub
Sub Export_HSPly_BTS()
On Error Resume Next
Dim wDoc As Object, xlWB As Workbook, shtDanhMuc As Worksheet
Dim strFind As String, strReplace As String, strPath As String
Dim myArr As Variant
Dim LastRow As Long, i As Integer
Set xlWB = Workbooks(ActiveWorkbook.Name)
'Set shtDanhMuc = xlWB.Sheets("XHH")
Set shtDanhMuc = xlWB.ActiveSheet
shtDanhMuc.Activate
'only allow the user to select one file
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'Remove all other filters
Call Application.FileDialog(msoFileDialogOpen).Filters.Clear
'Add a custom filter
Call Application.FileDialog(msoFileDialogOpen).Filters.Add("Word Files Only", "*.doc;*.docx")
'make the file dialog visible to the user
i = Application.FileDialog(msoFileDialogOpen).Show
If 0 = i Then Exit Sub
'determine what choice the user made
'get the file path selected by the user
strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
'print the file path to sheet 1
shtDanhMuc.[D4] = strPath
LastRow = shtDanhMuc.Cells(rows.count, "C").End(xlUp).Row ' Last row cua cot C
If (LastRow <= 2) Then Exit Sub ' Không có data
myArr = shtDanhMuc.Range("C5:D" & LastRow).value ' copy range values tu C6:D &lastRow to myArr
' Open word file for data merge
Set wdApp = CreateObject("Word.Application")
wdApp.visible = True
Set wDoc = wdApp.Documents.Open(strPath, ReadOnly:=True)
wDoc.Activate
For i = 1 To UBound(myArr)
strFind = myArr(i, 1) 'Tu khoa
If myArr(i, 2) <> "" Then
strReplace = myArr(i, 2) 'Noi dung thay the
Else
strReplace = "............."
End If
FindAndReplace "{-" & Trim(strFind) & "-}", strReplace
Next i
'''''Luu thanh file Bao cao voi ten File tai D5
wDoc.Content.Font.Color = wdColorAutomatic 'Chuyen tat ca noi dung chu ve mau den
sRootFolder = "E:\DL QUYENPV\THIET KE 2021\18. To trinh XHH, tang gia cac thang\1. HDong Export"
wDoc.SaveAs FileName:=sRootFolder & "\" & Range("D5") & ".doc" 'Luu ten file
wdApp.Documents(sRootFolder & "\" & Range("D5") & ".doc").Close (True) 'Chay code xong dong file Word
wdApp.Quit
End Sub
Public Sub FindAndReplace(ByVal strFind As String, ByVal strReplace As String)
Dim dataObj As MSForms.DataObject
Set dataObj = New MSForms.DataObject
'Dim dataObj As New MSForms.DataObject
dataObj.SetText strReplace
dataObj.PutInClipboard
wdApp.Selection.GoTo what:=wdGoToSection, which:=wdGoToFirst ' Go to the start of the document
With wdApp.Selection.Find
.ClearFormatting
.MatchCase = True
.MatchWholeWord = True
.Text = strFind
.Replacement.ClearFormatting
.Replacement.Text = "^c"
.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
Set dataObj = Nothing
End Sub