Consulting

Results 1 to 7 of 7

Thread: Saving and New Folders by string

  1. #1
    VBAX Newbie
    Joined
    May 2012
    Posts
    1
    Location

    Saving and New Folders by string

    Okay, so what I'd like to do is threefold. I am using Word 2010, and for this example lets say I'm working with a resume:

    Here's a snippet:

    "I am applying to Firm_Name, not only for the great experience and opportunity that Firm_Name provides, but because I believe that I have the neccessary skills to be successful at Firm_Name"

    Notice the repition of Firm_Name; this is the bit that I'd like to replace.

    The three things I'd like to do are:

    1. Replace Firm_Name with StringA
    2. Create a new folder on the desktop called StringA (C:\Users\User_Name\Desktop\StringA)
    3. Create a file within the folder from step 2 called Resume_StringA.html

    To further complicate things, I would like StringA to be determined by an inputbox

    Can any keen coders provide me some guidance here?

    Cheers

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    [vba]Sub Yadda()
    Dim fso As Scripting.FileSystemObject
    Dim Firm As String
    Dim MyNewFolder As String
    ' get string for new firm name
    Firm = InputBox("Type StringA ..whatever and press Enter.")
    ' create FSO
    Set fso = CreateObject("Scripting.filesystemobject")
    ' update bookmark (first Firm_Name)
    Call updateBm("Firm_Name", Firm)
    ' update fields - the other Firm_Names
    ActiveDocument.Fields.Update
    ' make string of new folder name using Environ
    MyNewFolder = "C:\Users\" & Environ("username") & "\Desktop\" & Firm
    ' create new folder
    fso.CreateFolder (MyNewFolder)
    ' save as HTML
    ActiveDocument.SaveAs FileName:=MyNewFolder & "\" & Firm & ".htm", FileFormat:=wdFormatHTML, _
    LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
    :="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
    SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
    False
    End Sub[/vba]Which uses:
    [vba]Sub UpdateBM(strBM As String, strText As String)
    Dim r As Range
    Set r = ActiveDocument.Bookmarks(strBM).Range
    r.Text = strText
    ActiveDocument.Bookmarks.Add Name:=strBM, Range:=r
    End Sub[/vba]

    This could very easily be done using another route. This route has ONE bookmark (Firm_Name) - this is the first instance of your Firm_Name.

    The OTHER Firm_Name occurences are reference fields. So whatever is in the bookmark is duplicated.

    The code also use a Reference to Microsoft Scripting Runtime to create the filesystemobject to make the new folder. Here it is step by step

    1. declare variables
    2. get your StringA from an inputbox (set into variable Firm)
    3. set the filesystemobject
    4. update the bookmark, making it the string from the inputbox
    5. update the fields, making the OTHER fields the same as the bookmark
    6. create new folder using environ("username") - which gets the system UserName - assuming this is what you want to use...
    7. save as html in new folder.

  3. #3
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi thedrops,

    You don't really need a macro for most of this. See: http://gregmaxey.mvps.org/word_tip_p...ting_data.html

    If you use a formfield with the bookmark name 'Name' and the following 'on exit' macro, the user will be prompted to confirm whether the document should be saved with the name in the formfield and, if so, what folder to save in.
    [VBA]Sub SaveWithFmFldName()
    Dim StrName As String, strFolder As String, Rslt
    With ActiveDocument
    StrName = Trim(.FormFields("Name").Result)
    If StrName = "" Then
    .FormFields("Name").Select
    Exit Sub
    End If
    StrName = .FormFields("Name").Result
    Rslt = MsgBox("Save this document as: " & StrName, vbOKCancel, "File Save Name")
    If Rslt = vbOK Then
    While strFolder = ""
    strFolder = GetFolder
    Wend
    .SaveAs strFolder & "\" & StrName & ".doc"
    Else
    .FormFields("Name").Select
    End If
    End With
    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[/VBA]
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Just want to add that it could be done differently.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    ...as Paul has of course demonstrated. Although not the additional instances of Firm_Name.

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by fumei
    Although not the additional instances of Firm_Name.
    That's what the link's for...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    ah....sorry. Having a stupid moment.

Posting Permissions

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