PDA

View Full Version : Save as bookmark



chris9104
11-11-2011, 04:27 AM
Hi guys, I am using the below code to try to save a word doc as certain bookmarks within the doc, however the bit highlighted in red will not work, please can you help. The reference will always be a number by the way

Thanks!




Call ErrHandler
Dim strfilename As String
Dim Varref As String
Dim Varname As String
Dim Var1 As String

Var1 = ActiveDocument.Bookmarks("Reference").Range.Text
If Var1 = "00000" Then
MsgBox "Please enter an account number."
ActiveDocument.FormFields(6).Select
Exit Sub
End If

Verref = ActiveDocument.Bookmarks("Reference").Range.Text
Varname = ActiveDocument.Bookmarks("Name").Range.Text

strfilename = "C:\Documents and Settings\Owner\Desktop\Bills" & "\" & Varref & "-" & Varname & ".doc"
ActiveDocument.SaveAs filename:=strfilename
Application.DisplayAlerts = True

MsgBox "A copy has been saved to the bills file"

Dave
11-11-2011, 09:30 AM
Don't know why that doesn't work? Why not..

strfilename = "C:\Documents and Settings\Owner\Desktop\Bills" & "\" & Var1 & "-" & Varname & ".doc"


Dave

chris9104
11-11-2011, 10:01 AM
Don't know why that doesn't work? Why not..

strfilename = "C:\Documents and Settings\Owner\Desktop\Bills" & "\" & Var1 & "-" & Varname & ".doc"


Dave

That works, thanks

Frosty
11-12-2011, 12:46 PM
Two things probably going on there...

1) You're not using Option Explicit at the top of your module. You should always use Option Explicit, because otherwise variables are created on the fly as needed, so you only get run-time errors, rather than compile-time errors.

2) If Option Explicit were on, then you would see that the reason it didn't work was because you dimmed a variable called "Varref" and reference it in the strFileName line, but you actually populated a variable called "Verref" (which, because option explicit isn't on, was created the line you first use it).

So the take away is to use Option Explicit.

Hope that helps.