Consulting

Results 1 to 3 of 3

Thread: Converting dotx to docx file

  1. #1

    Converting dotx to docx file

    Wrote the code below to convert the dotx to docx file. Tried using both documents.open and documents.add but both doesn't output my new save files. Not sure where I went wrong and would appreciate any expert advise.

    Sub ConvertFile()
    Application.ScreenUpdating = False
    Dim wdApp As New Word.Application
    Dim wdDoc As Word.Document
    Dim strFolder As String, strFile As String
    Dim sDocName As String

    strFolder = GetFolder
    If strFolder = "" Then Exit Sub

    strFile = Dir(strFolder & "\*.dotx", vbNormal)
    wdApp.DisplayAlerts = False
    While strFile <> ""
    'Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, AddtoRecentFiles:=False, Visible:=False, ReadOnly:=True)
    Set wdDoc = wdApp.Documents.Add(Template:=strFolder & "\" & strFile, Visible:=False)
    sDocName = Left(strFile, Len(strFile) - 5)
    sDocName = sDocName & ".docx"
    wdDoc.SaveAs2 Filename:=sDocName, FileFormat:=wdFormatDocumentDefault, AddtoRecentFiles:=False
    wdDoc.Close SaveChanges:=False
    strFile = Dir()
    Wend
    wdApp.Quit
    Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
    Application.ScreenUpdating = True
    End Sub

    Function GetFolder() As String
    Dim oFolder As Object
    GetFolder = ""
    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder = Nothing
    End Function

  2. #2
    Silly mistake on my end.

    Made the below changes and all is solved.

    Sub ConvertFile()
    '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 strFolder As String, strFile As String
    Dim sDocName As String

    strFolder = GetFolder
    If strFolder = "" Then Exit Sub
    '.docx, dotx
    strFile = Dir(strFolder & "\*.dotx", vbNormal)
    wdApp.DisplayAlerts = False
    While strFile <> ""
    Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, AddtoRecentFiles:=False, Visible:=False, ReadOnly:=True)
    sDocName = Left(strFile, Len(strFile) - 5)
    sDocName = sDocName & ".docx"
    wdDoc.SaveAs2 Filename:=strFolder & "\" & sDocName, FileFormat:=wdFormatDocumentDefault, AddtoRecentFiles:=False
    wdDoc.Close SaveChanges:=False
    strFile = Dir()
    Wend
    wdApp.Quit
    Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
    Application.ScreenUpdating = True
    End Sub

  3. #3
    If this code is meant to be run from Word, why are you creating a new Word application. What's wrong with the one you are running the code from?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •