Excel VBA获取Word文档表单数据返回不正确的日期格式

时间:2018-01-23 07:13:16

标签: excel vba excel-vba date ms-word

我复制了此代码以从word文档表单中获取数据并将其放入excel电子表格中,除了日期以mm / dd / yyyy格式显示之外,它的效果很好。我需要它是dd / mm / yyyy格式。

我尝试在表单中添加日期选择器但是没有帮助。

通常我会将格式化为VBA代码,但我不确定如何在此代码中执行此操作。它可以插入,还是可以在进入电子表格后以某种方式重新格式化日期?

代码在Excel VBA中

Sub GetFormData()
 'Note: this code requires a reference to the Word object model
Application.ScreenUpdating = False
Dim wdApp As New Word.Application
Dim wdDoc As Word.Document
Dim CCtrl As Word.ContentControl
Dim strFolder As String, strFile As String
Dim WkSht As Worksheet, i As Long, j As Long
strFolder = GetFolder
If strFolder = "" Then Exit Sub
Set WkSht = Sheets("Data")
i = WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
    i = i + 1
    Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
        j = 0
        For Each CCtrl In .ContentControls
            j = j + 1
            WkSht.Cells(i, j) = CCtrl.Range.Text
        Next
    End With
    wdDoc.Close SaveChanges:=False
    strFile = Dir()
Wend
wdApp.Quit
Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
Application.ScreenUpdating = True

1 个答案:

答案 0 :(得分:0)

您可能想要替换

WkSht.Cells(i, j) = CCtrl.Range.Text

通过

WkSht.Cells(i, j).FormulaLocal = CCtrl.Range.Text