Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-16-2014, 02:24 AM
tintin007 tintin007 is offline compile error in save code Windows XP compile error in save code Office 2007
Novice
compile error in save code
 
Join Date: Nov 2014
Posts: 4
tintin007 is on a distinguished road
Default compile error in save code

Hi Forum,

New on here so please help if you can. Have the following code to save file using text boxes in the document called tbpanelname and tbpanelnumber and possibly the date. code does not work, this version has compile error. no surprises there as i don't really use vba much. any thoughts of approaches to do this ??

cheers mark

Private Sub CommandButton1_Click()

Code:
With ActiveDocument
  .SaveAs2.FileName = "C:\mydocuments"
    .SelectContentControlsByTitle.SaveAs = ("TBpanelnumber" & "tbpanelname")
End With
End Sub

Reply With Quote
  #2  
Old 11-16-2014, 04:09 AM
gmayor's Avatar
gmayor gmayor is offline compile error in save code Windows 7 64bit compile error in save code Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Your syntax is incorrect. You need something like the following. The folder C:\mydocuments must exist!

Code:
Dim strName As String
Dim strNum As String
Dim oCC As ContentControl
Const strPath As String = "C:\mydocuments\" 'Which must exist!
        With ActiveDocument
        For Each oCC In .ContentControls
            If LCase(oCC.Title) = "tbpanelnumber" Then
                If Trim(oCC.Range.Text) = oCC.PlaceholderText Then
                    oCC.Range.Select
                    MsgBox "Enter TB Panel Number"
                    Exit Sub
                Else
                    strNum = Trim(oCC.Range.Text)
                End If
            End If
            If LCase(oCC.Title) = "tbpanelname" Then
                If Trim(oCC.Range.Text) = oCC.PlaceholderText Then
                    oCC.Range.Select
                    MsgBox "Enter TB Panel Name"
                    Exit Sub
                Else
                    strName = Trim(oCC.Range.Text)
                End If
            End If
        Next oCC
        .SaveAs2 Filename:=strPath & strNum & strName & ".docx"
    End With
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 11-16-2014, 12:54 PM
tintin007 tintin007 is offline compile error in save code Windows XP compile error in save code Office 2007
Novice
compile error in save code
 
Join Date: Nov 2014
Posts: 4
tintin007 is on a distinguished road
Default obejct not supported

Hi,
thanks for your reply. the line save as come back as object not supported.

Code:
.SaveAs2 FileName:=strPath & strNum & strName & ".docx"
what does this line do. is it checking for data in the text box tbpanelnumber ??

Code:
If LCase(oCC.Title) = "tbpanelnumber" Then
Reply With Quote
  #4  
Old 11-16-2014, 02:10 PM
macropod's Avatar
macropod macropod is offline compile error in save code Windows 7 64bit compile error in save code Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try using .SaveAs instead of .SaveAs2.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 11-16-2014, 04:33 PM
tintin007 tintin007 is offline compile error in save code Windows XP compile error in save code Office 2007
Novice
compile error in save code
 
Join Date: Nov 2014
Posts: 4
tintin007 is on a distinguished road
Default

Hi, Tried that and it works as in that it now saves the doc but just gives it a name of unitam installs. what i needed was to give it the name of the contents of text boxes tbpanelname and tbpanelnumber. ?? cheers mark
Reply With Quote
  #6  
Old 11-16-2014, 04:50 PM
macropod's Avatar
macropod macropod is offline compile error in save code Windows 7 64bit compile error in save code Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

In that case I suggest you check whether your content controls titles are the same as what you have in the code.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 11-16-2014, 10:18 PM
gmayor's Avatar
gmayor gmayor is offline compile error in save code Windows 7 64bit compile error in save code Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Quote:
Originally Posted by tintin007 View Post
Hi,
thanks for your reply. the line save as come back as object not supported.

Code:
.SaveAs2 FileName:=strPath & strNum & strName & ".docx"
what does this line do. is it checking for data in the text box tbpanelnumber ??

Code:
If LCase(oCC.Title) = "tbpanelnumber" Then
SaveAs2 is not supported by Word 2007. It was introduced with Office 2010, but as you included it in your original message, I assumed that the 2007 reference was perhaps wrong, and followed suit. As Paul suggests SaveAs without the '2' will work in 2007.

The second quoted line checks for a content control name (converted to lower case) called tbpanelnumber.

The macro reads the values of the two named content controls, checks whether you have entered text in them and if you have saves the file with those names. It will not work correctly if either the content control names you quoted are wrong, or if they are not content controls but some other kind of 'text box'.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #8  
Old 11-18-2014, 03:40 AM
tintin007 tintin007 is offline compile error in save code Windows XP compile error in save code Office 2007
Novice
compile error in save code
 
Join Date: Nov 2014
Posts: 4
tintin007 is on a distinguished road
Default changed text box to content control

Hi,

Sorted out problem with not saving filename, this was down to me using text boxes and not content control text. I have learned some new stuff here, so thanks for your assistance.

final code below.

Code:
Private Sub CommandButton1_Click()
 
Dim strName As String
Dim strNum As String
Dim oCC As ContentControl
Const strPath As String = "C:\Documents and Settings\msaunders\My Documents\Unitam Installs" 'Which must exist!
        With ActiveDocument
        For Each oCC In .ContentControls
            If LCase(oCC.Title) = "panel number" Then
                If Trim(oCC.Range.Text) = oCC.PlaceholderText Then
                    oCC.Range.Select
                    MsgBox "Enter Panel Number"
                    Exit Sub
                Else
                    strNum = Trim(oCC.Range.Text)
                End If
            End If
            If LCase(oCC.Title) = "panel name" Then
                If Trim(oCC.Range.Text) = oCC.PlaceholderText Then
                    oCC.Range.Select
                    MsgBox "Enter Panel Name"
                    Exit Sub
                Else
                    strName = Trim(oCC.Range.Text)
                End If
            End If
        Next oCC
        .SaveAs FileName:=strPath & strNum & strName & ".docx"
        
    End With
End Sub
Reply With Quote
  #9  
Old 11-18-2014, 03:49 AM
gmayor's Avatar
gmayor gmayor is offline compile error in save code Windows 7 64bit compile error in save code Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The line
"C:\Documents and Settings\msaunders\My Documents\Unitam Installs"
should end with "\"
i.e.
"C:\Documents and Settings\msaunders\My Documents\Unitam Installs\"
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
compile error in save code FileSystemObject Compile Error: User-Defined Type Not Defined gsrikanth Excel Programming 2 03-28-2022 06:32 AM
compile error in save code VBA code to compile one document based on multiple search terms Hoxton118 Word VBA 4 04-04-2021 06:02 AM
Compile error: sub or function not defined.. xena2305 Excel Programming 0 08-02-2011 10:17 AM
compile error in Word raco Word 0 09-28-2010 12:40 PM
Runtime error 5487 - Word cannot complete the save to to file permission error franferns Word 0 11-25-2009 05:35 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:34 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft