PDA

View Full Version : Saving and New Folders by string



thedrops
05-15-2012, 08:37 PM
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

fumei
05-15-2012, 09:44 PM
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 SubWhich uses:
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

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.

macropod
05-15-2012, 09:51 PM
Hi thedrops,

You don't really need a macro for most of this. See: http://gregmaxey.mvps.org/word_tip_pages/repeating_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.
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

fumei
05-15-2012, 10:18 PM
Just want to add that it could be done differently.

fumei
05-15-2012, 10:19 PM
...as Paul has of course demonstrated. Although not the additional instances of Firm_Name.

macropod
05-15-2012, 10:22 PM
Although not the additional instances of Firm_Name.
That's what the link's for...

fumei
05-15-2012, 10:33 PM
ah....sorry. Having a stupid moment.