didijaba
Well-known Member
- Joined
- Nov 26, 2006
- Messages
- 511
Hello,
I have trouble finding last saved .docm file in folder where Thisdocument is. It would have to be part of Word VBA. This is what I have so far.
I have trouble finding last saved .docm file in folder where Thisdocument is. It would have to be part of Word VBA. This is what I have so far.
Code:
Sub ExtractImages() 'gmaxey script from vbaexpress
Dim strFileNameAndPath As String, strFilename As String, strName As String
Dim strBasePath As String, strDate As String
Dim strExtractionFolder As String, strImageName As String
Dim lngIndex As Long
Dim oRng As Word.Range
Dim arrCaptions() As String
'Store image caption names. The paragraph immediately following the shape paragraph defines the image caption.
ReDim arrCaptions(ActiveDocument.InlineShapes.Count - 1)
For lngIndex = 1 To ActiveDocument.InlineShapes.Count
Set oRng = ActiveDocument.InlineShapes(lngIndex).Range
oRng.Collapse wdCollapseEnd
'Move range to start of caption paragraph text.
oRng.Move wdParagraph, 1
'Extend range to end of paragraph.
oRng.MoveEndUntil Cset:=Chr(13), Count:=wdForward
arrCaptions(lngIndex - 1) = oRng.Text
Next lngIndex
strFileNameAndPath = ActiveDocument.FullName
'Define folder for extracted images.
'strBasePath = SpecialFolderPath & Application.PathSeparator
strBasePath = ActiveDocument.Path & "\"
strDate = Format(Now, "yyyy-mm-dd")
strFilename = GetFileNameWithoutExtension(ActiveDocument.Name)
strExtractionFolder = strDate & "_" & strFilename
'Delete the folder if it exists.
On Error Resume Next
'Delete any files.
Kill strBasePath & strExtractionFolder & "_files\*"
RmDir strBasePath & strExtractionFolder & "_files"
On Error GoTo 0
'Save the current document.
ActiveDocument.Save
'Save document in HTML format. This creates the "_files\" folder in the Extraction Folder.
ActiveDocument.SaveAs2 Filename:=strBasePath & strExtractionFolder & ".html", FileFormat:=wdFormatFilteredHTML
ActiveDocument.Close
On Error Resume Next
'Get rid of extraneous data files. Keep only the images.
Kill strBasePath & strExtractionFolder & ".html"
Kill strBasePath & strExtractionFolder & "_files\*.xml"
Kill strBasePath & strExtractionFolder & "_files\*.html"
Kill strBasePath & strExtractionFolder & "_files\*.thmx"
On Error GoTo 0
'Rename image files.
lngIndex = 0
strName = Dir(strBasePath & strExtractionFolder & "_files\")
While strName <> ""
'Some characters are invalid in file names. A colon is invalid and could be used in the caption e.g., Screenshot 1: The Mona Lisa"
strImageName = Replace(arrCaptions(lngIndex), ":", "-")
Name strBasePath & strExtractionFolder & "_files\" & strName As strBasePath & strExtractionFolder & "_files\" & strImageName & ".png"
strName = Dir()
lngIndex = lngIndex + 1
Wend
Word.Documents.Open (strFileNameAndPath)
Word.Application.Visible = True
Word.Application.Activate
Exit Sub
lbl_Exit:
Exit Sub
End Sub
Function GetFileNameWithoutExtension(ByRef strFilename As String) As String
On Error GoTo Err_NoExtension
GetFileNameWithoutExtension = VBA.Left(strFilename, (InStrRev(strFilename, ".", -1, vbTextCompare) - 1))
lbl_Exit:
Exit Function
Err_NoExtension:
GetFileNameWithoutExtension = strFilename
Resume lbl_Exit
End Function
Function SpecialFolderPath() As String
Dim objWSHShell As Object
Dim strSpecialFolderPath
Set objWSHShell = CreateObject("WScript.Shell")
SpecialFolderPath = objWSHShell.SpecialFolders("Desktop")
Set objWSHShell = Nothing
lbl_Exit:
Exit Function
End Function