PDA

View Full Version : Save as in word with multiple variables



Brett Desper
04-14-2010, 07:53 AM
I have need a code that will allow me to use the saveas function where the path and filename are variables. They variables are taken from the word document from content control fields. So the path name would look sort of like this
\\servername\quotes\variable1\nameoffile.docx
name of file needs to be datecontrolfieldsubject.docx

All the examples I have seen for saveas do not use variables so any explanation of what is going on in the line of code would be appreciated.

lucas
04-14-2010, 09:29 AM
Here's an example using a formfield result as part of the saveas name.

ActiveDocument.SaveAs FileName:="C:\Temp\" & ActiveDocument.FormFields(1).Result & "-" & ActiveDocument.Name

fumei
04-14-2010, 10:05 AM
Dim Variable_1 As String
Dim Variable_2 As String
Dim Variable_3 As String

Variable_1 = ActiveDocument.Paragraphs(2).Range.Text
Variable_2 = ActiveDocument.Variables("Whatever").Value
Variable_3 = ActiveDocument.Bookmarks("DocPath").Range.Text

ActiveDocument.SaveAs Filename:= Variable_3 & Variable_2 & Variable_1

could be valid...as long as the string content of those are valid.

SaveAs can take ANY combination of string variable(s), and will work as long as the completed string has a valid filename syntax.

SaveAs Filename is just a string.

Brett Desper
04-15-2010, 10:47 AM
I don't think I am describing this very well. If I can understand how the first one works, I can get apply this to all that I need to do. I have a word 2007 doc (example attached) with content control fields. The attached file only has one content control field. I need to be able to read that field into a save function. With the current example:
I would want to save the file as
\\whateverpath\thecontentcontrolfieldvalue
What I am having trouble with is getting that value into the macro in some form that I can use it.
The content control is the only item in the doc.
I am a complete newbie, so any description of what is going on would help.

fumei
04-15-2010, 11:03 AM
I am afraid someone else is going to have to help you. I do not use 2007, and the converter simply makes your "Click here to enter text." as...just text. There is no control.

Even so, I will reiterate. The parameter accepted by SaveAs is a string, and only a string. HOW you build that string is completly up to you, and you can use any string variable you like. As long as the final string is a valid filename format, it will work.


I have need a code that will allow me to use the saveas function where the path and filename are variables. Please see my previous post. SaveAs can use variables.

I can not help more than this, as your "content control" is 2007 specific, and does not convert into anything useable for me.

So....the issue is not how SaveAs can use a variable - it can, and does.

The issue (the real question) is how do you get the text content of your control content OUT, as a string.

I have no idea, as this is 2007 specific. I am sure there is someone here who uses 2007 that can help you.

lucas
04-15-2010, 11:27 AM
It's not a formfield and I don't have 2007 either.

could you save it as a 2003 file or maybe use a regular text formfield?

see attached. Let me know if it reads the formfield when you click the button in 2007.

Brett Desper
04-15-2010, 12:11 PM
Yes it did read the formfield. I have to have the word doc in a 2007 docx format however in order for the other macros that previous people have written to work.

fumei
04-16-2010, 09:22 AM
That may be so, but your original question has been repeatedly answered. File SaveAs can easily use variables.

Paul_Hossler
04-16-2010, 01:09 PM
this will retreive the contents and then you can construct the FileSaveAs


Option Explicit
Sub drv()
Dim oCC As ContentControl
For Each oCC In ActiveDocument.ContentControls
With oCC
If .Title = "Generic" Then
MsgBox .Range.Text
End If
End With
Next

End Sub


BTW, for some reason only known to MS Word, a ContentControl does not seem to be indexable by a Name. If there is, I haven't found it yet

Paul

Brett Desper
04-19-2010, 12:57 PM
Yes, the original question has been answered, and the secondary question as well. Thank you all for your help. Paul, could I define your code as a function and have it available throughout the additional macros that are will be added?

Paul_Hossler
04-19-2010, 03:19 PM
Sure


Option Explicit
Sub drv()

MsgBox "1 -- " & GetContentControlValue("generic")
MsgBox "2 -- " & GetContentControlValue("Generic")
MsgBox "3 -- " & GetContentControlValue("GENERIC")
MsgBox "4 -- " & GetContentControlValue("DoesNotExist")

End Sub

Private Function GetContentControlValue(sTitle As String) As String
Dim oCC As ContentControl

GetContentControlValue = vbNullString

On Error GoTo NiceExit

For Each oCC In ActiveDocument.ContentControls
With oCC
If UCase(.Title) = UCase(sTitle) Then
GetContentControlValue = .Range.Text
Exit Function
End If
End With
Next

NiceExit:
End Function


Paul

Brett Desper
04-28-2010, 03:12 PM
Thanks Paul, that worked and this one is solved. :)