vba - एम्बेड की गई छवियों को लिंक करने के लिए कनवर्ट करें




ms-word ms-office (5)

मैं मैक्रो को ठीक करने का प्रयास कर रहा हूं, नीचे दिखाया गया है

यह एम्बेड की गई छवियों को लिंक करने के लिए (शामिल करें चित्र के माध्यम से) में परिवर्तित करना है। हालांकि, वर्तमान स्थिति में, दस्तावेज़ के नीचे छवियां जोड़ दी जाती हैं। जाहिर है, यह बिल्कुल सही नहीं है। इसके बजाय, मैक्रो को लिंक वाले लोगों के साथ एम्बेड किए गए छवियों को बदलना चाहिए, एक-एक करके, जैसा कि यहां दिखाया गया है:

इसे कैसे जोड़ेंगे?

साथ ही, नोट: मैक्रो को किसी अन्य फ़ाइल से लॉन्च किया जाना चाहिए। इसलिए, आपको दो दस्तावेजों की आवश्यकता है: एक मैक्रो और एक छवियों के साथ। यह अच्छा नहीं है, लेकिन यह वर्तमान में कैसे काम करता है

कोड:

Sub MakeDocMediaLinked()
    Application.ScreenUpdating = False
    Dim StrOutFold As String, Obj_App As Object, Doc As Document, Rng As Range
    Dim StrDocFile As String, StrZipFile As String, StrMediaFile As String
    With Application.Dialogs(wdDialogFileOpen)
        If .Show = -1 Then
            .Update
            Set Doc = ActiveDocument
        End If
    End With
    If Doc Is Nothing Then Exit Sub
    With Doc
        ' ID the document to process
        StrDocFile = .FullName
        StrOutFold = Split(StrDocFile, ".")(0) & "_Media"
        .Close SaveChanges:=False
    End With
    ' Test for existing output folder, create it if it doesn't already exist
    If Dir(StrOutFold, vbDirectory) = "" Then MkDir StrOutFold
    ' In case the output folder is not empty. Also, in case the file has no media
    On Error Resume Next
    ' Delete any files in the output folder
    Kill StrOutFold & "\*.*"
    ' Create a Shell App for accessing the zip archives
    Set Obj_App = CreateObject("Shell.Application")
    ' Define the zip name
    StrZipFile = Split(StrDocFile, ".")(0) & ".zip"
    ' Create the zip file, by simply copying to a new file with a zip extension
    FileCopy StrDocFile, StrZipFile
    ' Extract the zip archive's media files to the temporary folder
    Obj_App.NameSpace(StrOutFold & "\").CopyHere Obj_App.NameSpace(StrZipFile & "\word\media\").Items
    ' Delete the zip file - the loop takes care of timing issues
    Do While Dir(StrZipFile) <> ""
        Kill StrZipFile
    Loop
    ' Restore error trapping
    On Error GoTo 0
    ' Get the temporary folder's file listing
    StrMediaFile = Dir(StrOutFold & "\*.*", vbNormal)
    Documents.Open FileName:=StrDocFile
    With ActiveDocument
        ' Process the temporary folder's files
        While StrMediaFile <> ""
            .Range.InsertAfter vbCr
            Set Rng = .Paragraphs.Last.Range
            .Fields.Add Range:=Rng, Type:=wdFieldEmpty, PreserveFormatting:=False, _
                Text:="INCLUDEPICTURE """ & Replace(StrOutFold & "\" & StrMediaFile, "\", "\\") & """ \d"
            ' Get the next media file
            StrMediaFile = Dir()
        Wend
        .Fields.Update
    End With
    Application.ScreenUpdating = True
End Sub

यह वह जगह है जहां आपका कोड भटक रहा है: -

With ActiveDocument
            .Range.InsertAfter vbCr
            Set Rng = .Paragraphs.Last.Range

आप दस्तावेज़ के अंत में एक कैरिज रिटर्न जमा कर रहे हैं (जो वास्तव में एक नया खाली पैराग्राफ सम्मिलित करता है) और उसके बाद उस पैराग्राफ़ में फ़ील्ड जोड़ें जाहिर है, आप कहीं और क्षेत्र चाहते हैं।

इस बीच, यदि आप लिंक को हटाना चाहते हैं तो आपको अपना कोड देना चाहिए। मैं समझ नहीं पाया कि आपका कोड उस दिशा में एक प्रयास करता है या नहीं, लेकिन अनुमान लगाता है कि यह लिंक के चित्र का पथ निकाल देता है। इसलिए, लिंक को अपना रास्ता छोड़ने के बाद स्थित किया जाना चाहिए और हटा दिया जाना चाहिए, और इसके स्थान पर फ़ील्ड डाली गई।


एक तरह से छवि को क्लिपबोर्ड पर Selection.Copy साथ प्रतिलिपि करना होगा। कॉपी करें और उसे वहां से PNG रूप में सहेजने के लिए। फिर छवि को Document.InlineShapes.AddPicture साथ एक बाहरी लिंक के साथ बदलें। Document.InlineShapes.AddPicture

डुप्लिकेट छवियों को संभालने के लिए, प्रत्येक छवि को हैश करें और कंप्यूट किए गए कोड का ट्रैक रखें। मैं मूल संकल्प रखने के बाद एक आकार को भी पुन: आकार देगा।

Public Sub Example()
  SaveAsExternImages ActiveDocument, "c:\temp\myfile-no-img.docx"
End Sub

Public Sub SaveAsExternImages(doc As Document, fname As String)
  Dim sh As InlineShape, rg As Range, docDir, imgDir, imgPath, imgHash
  Dim hDib, scaleW, scaleH, i As Long
  Dim imgPaths As New Collection
  Dim imgs As New Collection

  ' create the  media folder and set the relative directory '
  docDir = Left(fname, InStrRev(fname, "\") - 1)
  imgDir = Left(fname, InStrRev(fname, ".") - 1) & "_media"
  MakeDir imgDir

  ' clean clipboard '
  Call OpenClipboard: Call EmptyClipboard: Call CloseClipboard

  ' select images '
  For Each sh In doc.InlineShapes
    Select Case sh.Type
      Case wdInlineShapeLinkedPicture, wdInlineShapePicture
        imgs.Add sh
    End Select
  Next

  ' handle each image '
  For Each sh In imgs

    ' store/reset the scale '
    scaleW = sh.ScaleWidth
    scaleH = sh.ScaleHeight
    sh.ScaleWidth = 100
    sh.ScaleHeight = 100

    ' copy shape to the clipboard '
    sh.Select
    doc.Application.Selection.Copy

    ' get clipboard as DIB (device independent bitmap) '
    If OpenClipboard() Then Else Err.Raise 9, , "OpenClipboard failed"
    hDib = GetClipboardData(8)  ' 8 = CF_DIB = BITMAPINFO '
    If hDib Then Else Err.Raise 9, , "GetClipboardData failed"

    ' get image hash code from DIB (CRC32) '
    imgHash = GetDIBHashCode(hDib)

    ' save as PNG if hash not already present in the collection '
    If TryGetValue(imgPaths, imgHash, imgPath) = False Then
      i = i + 1
      imgPath = SaveDIBtoPNG(hDib, imgDir & "\image" & i & ".png")
      imgPath = Mid(imgPath, Len(docDir) + 2) ' make relative '
      imgPaths.Add imgPath, CStr(imgHash)
    End If

    ' dispose clipboard '
    Call EmptyClipboard
    Call CloseClipboard

    ' replace the shape with a linked picture and restore the scale '
    Set rg = sh.Range
    sh.Delete
    doc.Application.ChangeFileOpenDirectory docDir ' set relative folder '
    Set sh = doc.InlineShapes.AddPicture(imgPath, True, False, rg)
    sh.ScaleWidth = scaleW
    sh.ScaleHeight = scaleH
  Next

  doc.SaveAs2 fname
End Sub

संबंधित कार्य / प्रक्रियाएं:

Private Declare PtrSafe Function CLSIDFromString Lib "ole32" (ByVal lpsz As LongPtr, pclsid As Byte) As Long
Private Declare PtrSafe Function RtlComputeCrc32 Lib "ntdll" (ByVal start As Long, ByRef data As Any, ByVal Size As Long) As Long
Private Declare PtrSafe Function GlobalLock Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function GlobalUnlock Lib "kernel32" (ByVal hMem As LongPtr) As Boolean
Private Declare PtrSafe Function GlobalSize Lib "kernel32" (ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function OpenClipboard Lib "user32" (Optional ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As LongPtr
Private Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Private Declare PtrSafe Function GdiplusStartup Lib "gdiplus" (token As LongPtr, cfg As Any, ByVal hook As LongPtr) As Long
Private Declare PtrSafe Function GdiplusShutdown Lib "gdiplus" (ByVal token As LongPtr) As Long
Private Declare PtrSafe Function GdipCreateBitmapFromGdiDib Lib "gdiplus" (ByVal hdr As LongPtr, ByVal data As LongPtr, img As LongPtr) As Long
Private Declare PtrSafe Function GdipSaveImageToFile Lib "gdiplus" (ByVal img As LongPtr, ByVal path As LongPtr, riid As Byte, ByVal cfg As LongPtr) As Long
Private Declare PtrSafe Function GdipDisposeImage Lib "gdiplus" (ByVal img As LongPtr) As Long

Private Function GetDIBHashCode(hDib) As Long
  Dim pDib As LongPtr, bmSize As Long, sz As Long
  pDib = GlobalLock(hDib)
  If pDib Then Else Err.Raise 9, , "GlobalLock failed"
  GetDIBHashCode = RtlComputeCrc32(0, ByVal pDib, GlobalSize(hDib))
  GlobalUnlock hDib
End Function

Private Function SaveDIBtoPNG(hDib, filePath As String) As String
  Dim cfg(0 To 7) As Long, clsid(0 To 15) As Byte, pDib As LongPtr, hGdi As LongPtr, hImg As LongPtr
  CLSIDFromString StrPtr("{557CF406-1A04-11D3-9A73-0000F81EF32E}"), clsid(0) ' PNG encoder '
  cfg(0) = 1&  ' GdiplusVersion '
  pDib = GlobalLock(hDib)   ' lock BITMAPINFOHEADER + image bytes '
  If pDib Then Else Err.Raise 9, , "GlobalLock failed"
  If GdiplusStartup(hGdi, cfg(0), 0) Then Err.Raise 9, , "GdiplusStartup failed"
  If GdipCreateBitmapFromGdiDib(pDib, pDib + 40, hImg) Then Err.Raise 9, , "GdipCreateBitmapFromGdiDib failed"
  If GdipSaveImageToFile(hImg, StrPtr(filePath), clsid(0), 0) Then Err.Raise 9, , "GdipSaveImageToFile failed"
  If GdipDisposeImage(hImg) Then Err.Raise 9, , "GdipDisposeImage failed"
  If GdiplusShutdown(hGdi) Then Err.Raise 9, , "GdiplusShutdown failed"
  GlobalUnlock hDib
  SaveDIBtoPNG = filePath
End Function

Private Function TryGetValue(obj As Collection, Key, outValue) As Boolean
  On Error Resume Next
  outValue = obj.Item(CStr(Key))
  TryGetValue = Err.Number = 0
End Function

Private Sub MakeDir(path)
  If Len(Dir(path, vbDirectory)) = False Then
    MkDir path
  ElseIf Len(Dir(path & "\")) Then
    Kill path & "\*"
  End If
End Sub

आप सभी छवियों को निकालने के लिए Document.Content.XML कंटेनेंट.एक्सएमएल द्वारा लौटाए गए XML को पार्स कर सकते हैं। फिर बाहरी स्रोत के पथ के साथ प्रत्येक स्रोत को अपडेट करें और XML.Content.InsertXML के साथ XML को वापस लिखें।

एक्सएमएल वापस लिखना एक लिंक क्षेत्र जोड़ता है जो आपकी आवश्यकता में से एक है। यह तेजी से है कि क्लिपबोर्ड के साथ काम कर रहा है और यह आकार की शैली को बदल नहीं करता है हालांकि, आपको विशिष्ट मामलों को नियंत्रित करने के लिए कोड को ज़ूम करने की आवश्यकता हो सकती है।

Private Declare PtrSafe Function CryptStringToBinaryW Lib "Crypt32" (ByVal pszString As LongPtr, ByVal cchString As Long, ByVal dwFlags As Long, ByRef pbBinary As Byte, ByRef cbBinary As Long, ByVal pdwSkip As LongPtr, ByVal pdwFlags As LongPtr) As Boolean

Public Sub Example()
  SaveAslinkedImages ActiveDocument, "c:\temp\myfile-no-img.docx"
End Sub

Public Sub SaveAslinkedImages(Doc As Document, fname As String)
  Dim objXml As Object, binData As Object, binName$, nodes, node
  Dim imgPath$, docDir$, imgDir$, i&, data() As Byte

  Set objXml = VBA.CreateObject("Msxml2.DOMDocument.6.0")
  objXml.Async = False
  objXml.validateOnparse = False

  ' parse xml document '
  objXml.LoadXML Doc.Content.XML

  ' add namespaces for SelectNodes '
  objXml.setProperty "SelectionNamespaces", _
    objXml.DocumentElement.getAttributeNode("xmlns:w").XML & " " & _
    objXml.DocumentElement.getAttributeNode("xmlns:v").XML

  ' create the  media folder '
  docDir = Left(fname, InStrRev(fname, "\") - 1)
  imgDir = Left(fname, InStrRev(fname, ".") - 1) & "_media"
  MakeDir imgDir

  ' iterate each image data '
  For Each binData In objXml.SelectNodes("//w:binData")
    binName = binData.getAttribute("w:name")

    ' get all the nodes referencing the image data '
    Set nodes = objXml.SelectNodes("//v:imagedata[@src='" & binName & "']")

    If nodes.Length Then ' if any '
      ' build image path '
      imgPath = imgDir & "\" & Mid(binName, InStrRev(binName, "/") + 1)

      ' save base64 data to file '
      DecodeBase64 binData.Text, data
      SaveBytesAs data, imgPath

      ' remove the data '
      binData.ParentNode.RemoveChild binData

      ' for each image '
      For Each node In nodes
        ' set id '
        node.ParentNode.setAttribute "id", node.ParentNode.getAttribute("o:spid")

        ' remove o namespace '
        node.ParentNode.Removeattribute "o:spid"
        node.Removeattribute "o:title"

        ' set external image source '
        node.setAttribute "src", imgPath
      Next
    End If
  Next

  ' write back the xml and save the document '
  Doc.Content.InsertXML objXml.XML
  Doc.SaveAs2 fname

End Sub

Public Sub SaveBytesAs(data() As Byte, path As String)
  Open path For Binary Access Write As #5
  Put #5, 1, data
  Close #5
End Sub

Public Sub MakeDir(path As String)
  If Len(Dir(path, vbDirectory)) Then Exit Sub
  MakeDir Left(path, InStrRev(path, "\") - 1)
  MkDir path
End Sub

Public Function DecodeBase64(str As String, out() As Byte) As Boolean
  Dim size As Long
  size = ((Len(str) + 3) \ 4) * 3
  ReDim out(0 To size - 1) As Byte
  DecodeBase64 = CryptStringToBinaryW(StrPtr(str), Len(str), 1, out(0), size, 0, 0)
  If size - 1 < UBound(out) Then ReDim Preserve out(0 To size - 1)
End Function

प्रत्येक छवि का पता लगाने और लिंक को अपनी स्थिति में डालकर यह कोड आपको जो चाहें प्राप्त करेगा। ध्यान दें कि यदि आप संशोधित दस्तावेज़ सहेजते हैं तो मूल फ़ाइल ओवरराइट हो जाएगी। अधिक जानकारी के लिए कोड में मेरी टिप्पणियां देखें। कोड अब भी डुप्लिकेट के लिए काम करता है

Option Explicit
Const IMAGEBASENAME = "image"
Const IMAGEEXTENSION = ".jpeg" 'Images in .zip file are all .jpg

Sub MakeDocMediaLinked()
    Dim StrOutFold As String
    Dim Obj_App As Object
    Dim Doc As Document
    Dim Rng As Range
    Dim StrDocFile As String
    Dim StrZipFile As String
    Dim StrMediaFile As String
    Dim objShape As InlineShape
    Dim imgNum As Integer
    Dim imgCount As Integer
    Dim imgName As String
    Dim imgNames As New Collection
    Dim i As Integer
    Dim doDir As Boolean

    Application.ScreenUpdating = False
    With Application.Dialogs(wdDialogFileOpen)
        If .Show = -1 Then
            .Update
            Set Doc = ActiveDocument
        End If
    End With
    If Doc Is Nothing Then Exit Sub
    With Doc
        StrDocFile = .FullName    ' ID the document to process
        StrOutFold = Split(StrDocFile, ".")(0) & "_Media"
        .Close SaveChanges:=False
    End With
    If Dir(StrOutFold, vbDirectory) = "" Then MkDir StrOutFold    ' Test for existing output folder, create it if it doesn't already exist
    '*
    '* Delete any files in the output folder. On Error Resume Next not used
    '*
    If Dir(StrOutFold & "\*.*", vbNormal) <> "" Then Kill StrOutFold & "\*.*"
    ' Create a Shell App for accessing the zip archives
    Set Obj_App = CreateObject("Shell.Application")
    ' Define the zip name
    StrZipFile = Split(StrDocFile, ".")(0) & ".zip"
    ' Create the zip file, by simply copying to a new file with a zip extension
    FileCopy StrDocFile, StrZipFile
    ' Extract the zip archive's media files to the temporary folder
    Obj_App.NameSpace(StrOutFold & "\").CopyHere Obj_App.NameSpace(StrZipFile & "\word\media\").Items
    Do While Dir(StrZipFile) <> ""     ' Delete the zip file - the loop takes care of timing issues
        Kill StrZipFile
    Loop
    StrMediaFile = Dir(StrOutFold & "\*.*", vbNormal)     ' Get the temporary folder's file listing
    Documents.Open FileName:=StrDocFile
    With ActiveDocument
        imgCount = .InlineShapes.Count
        For imgNum = 1 To imgCount
            '*
            '* Get the (next) image
            '*
            Set objShape = .InlineShapes(imgNum)
            '*
            '* Get the original full path of the image
            '*
            imgName = objShape.AlternativeText
            '*
            '* Look for possible duplicate
            '*
            '* Add the ordinal number as the item and the path as the key to avoid duplicates
            '* If we get an error here then the image is a duplicate of a previous one
            '* The ordinal number in imgNames identifies the image to use in the _Media folder
            '*
            i = imgNames.Count  'Current count
            doDir = True    ' Assume no duplicate
            On Error Resume Next
            imgNames.Add imgNum, imgName
            On Error GoTo 0    'Always reset error handling after Resume
            If i = imgNames.Count Then    'Duplicate found, build the duplicate's file name
                StrMediaFile = IMAGEBASENAME & imgNames(imgName) & IMAGEEXTENSION
                doDir = False    'Do not read a new file
            End If
            '*
            '* Get the range where we want the link to appear
            '*
            Set Rng = objShape.Range
            '*
            '* Delete the image from the document
            '*
            objShape.Delete
            '*
            '* Replace the image with a link to a saved disk image in the *_Media folder
            '*
            .Fields.Add Range:=Rng, Type:=wdFieldEmpty, PreserveFormatting:=False, _
                        Text:="INCLUDEPICTURE """ & Replace(StrOutFold & "\" & StrMediaFile, "\", "\\") & """ \d"
            If doDir Then StrMediaFile = Dir()    ' Get the next media file since we had no duplicate this time
        Next imgNum
        .Fields.Update
    End With
    Set imgNames = Nothing
    Application.ScreenUpdating = True
End Sub

नया समाधान

तरीका

प्रत्येक InlineShape (रिवर्स में काम करना), अगर यह एक wdInlineShapePicture

  1. इसे एक अस्थायी दस्तावेज़ में कॉपी करें
  2. अस्थायी दस्तावेज़ को .docx रूप में सहेजें
  3. अस्थायी दस्तावेज़ को .zip फ़ाइल के रूप में कॉपी करें
  4. *.zip/word/media फ़ोल्डर की सामग्री को अस्थायी फ़ोल्डर में निकालें
  5. गंतव्य फ़ोल्डर में उस फ़ोल्डर में एकमात्र फ़ाइल को ले जाएं और उसका नाम बदलें
  6. आकार हटाएं
  7. एक ऐसा क्षेत्र बनाएं जो नई संसाधित फ़ाइल से लिंक हो, जहां आकृति का उपयोग किया गया

कोड

Option Explicit

Sub Example()

    MakeDocMediaLinked ActiveDocument

End Sub

Sub MakeDocMediaLinked(ByRef Doc As Document)

' iterate through each image

Dim i As Long
Dim shapeCollection As InlineShapes
Dim tempDoc As Document
Dim fso As New FileSystemObject  ' early binding; add a reference to Microsoft Scripting Runtime (scrrun.dll)
Dim oShell As New Shell32.Shell  ' early binding; add a reference to Microsoft Shell Controls and Automation (shell32.dll)
Dim currentMediaFileNameSource As String
Dim currentMediaFileNameNew As String
Dim shp As InlineShape
Dim rngToRemove As Range, rngToInsertInto As Range

Const tempDocFilePathDoc As String = "C:\test\temp.docx"
Const tempDocFilePathZip As String = "C:\test\temp.zip"
Const tempMediaFolderPath As String = "C:\test\temp\"
Const destMediaFolderPath As String = "C:\test\images\"

MakePath tempMediaFolderPath ' make the temporary folder in which to store an image, if it doesn't already exist
MakePath destMediaFolderPath ' make the images folder in which to store the images, if it doesn't already exist

Set tempDoc = Application.Documents.Add(Visible:=False) ' create the temp doc, hide it
tempDoc.SaveAs2 FileName:=tempDocFilePathDoc ' save the temp doc

Set shapeCollection = Doc.InlineShapes

For i = shapeCollection.Count To 1 Step -1 ' working backwards through the collection
    Set shp = shapeCollection(i)
    If shp.Type = wdInlineShapePicture Then
        tempDoc.Range.Delete ' clear the temp doc
        tempDoc.Range.FormattedText = shp.Range.FormattedText ' copy the image into the temp doc
        tempDoc.Save ' save the temp doc
        fso.CopyFile tempDocFilePathDoc, tempDocFilePathZip ' copy the temp doc and rename to a temp zip file (will overwrite existing zip)
        oShell.NameSpace(tempMediaFolderPath).CopyHere oShell.NameSpace(tempDocFilePathZip & "\word\media\").Items ' copy the one media file to a destination
        currentMediaFileNameSource = Dir(tempMediaFolderPath) ' get the name of the media file
        currentMediaFileNameNew = "media-" & i & Mid(currentMediaFileNameSource, InStrRev(currentMediaFileNameSource, ".")) ' names the files media-4.jpeg, media-3.png, etc.
        fso.CopyFile tempMediaFolderPath & currentMediaFileNameSource, destMediaFolderPath & currentMediaFileNameNew ' copy and rename the file into the destination folder
        fso.DeleteFile tempMediaFolderPath & currentMediaFileNameSource, True ' delete the temporary file
        Set rngToRemove = shp.Range ' set the range that we will be removing, i.e. the shape range
        Set rngToInsertInto = shp.Range ' set the range that we will be inserting the field into, i.e. the start of the shape range (1)
        rngToInsertInto.Collapse wdCollapseStart ' set the range that we will be inserting the field into, i.e. the start of the shape range (2)
        rngToRemove.Delete ' remove the shape
        Doc.Fields.Add Range:=rngToInsertInto, Type:=wdFieldEmpty, PreserveFormatting:=False, _
        Text:="INCLUDEPICTURE """ & Replace(destMediaFolderPath & currentMediaFileNameNew, "\", "\\") & """ \d" ' 4. add the field, we refer to destMediaFolderPath & currentMediaFileNameNew in the field definition
    End If
Next i

tempDoc.Close SaveChanges:=False ' close the temp doc

fso.DeleteFile tempDocFilePathZip, True ' delete the temporary zip
fso.DeleteFile tempDocFilePathDoc, True ' delete the temporary doc
fso.DeleteFolder Left(tempMediaFolderPath, Len(tempMediaFolderPath) - 1), True ' delete the temporary folder

Set fso = Nothing
Set oShell = Nothing

End Sub

Sub MakePath(ByVal tempPath As String)

Dim fso As New FileSystemObject

Dim path() As String
Dim path2() As String
Dim i As Long

Do While Right(tempPath, 1) = "\" ' remove any ending slashes
    tempPath = Left(tempPath, Len(tempPath) - 1)
Loop

path = Split(tempPath, "\")
ReDim path2(LBound(path) To UBound(path))

i = LBound(path)
path2(i) = path(i)
If Not fso.FolderExists(path2(i) & "\") Then Exit Sub ' if the drive doesn't even exist, then exit
For i = LBound(path) + 1 To UBound(path)
    path2(i) = path2(i - 1) & "\" & CleanPath(path(i))
    If Not fso.FolderExists(path2(i) & "\") Then fso.CreateFolder path2(i)
Next i

Set fso = Nothing

End Sub

Function CleanPath(ByVal tempPath As String)

Dim i As Long
Dim invalidChars As Variant

invalidChars = Array("/", ":", "*", "?", """", "<", ">", "|")

For i = LBound(invalidChars) To UBound(invalidChars)
    tempPath = Replace(tempPath, invalidChars(i), " ")
Next i

CleanPath = tempPath

End Function

बाद

चित्र फ़ोल्डर

दस्तावेज़ (फ़ील्ड दिखा रहा है)